Programming For Problem Solving Lecture Number :- 1 “Introduction to C” 1
2 Learning Objectives Introduction Uses of C Programming Structure of a C Program Common used files in C Compiling and Executing process C Program Data Types Input and Output statements
3 INT R ODUCTION C is one of the most popular programming language . C programming language was developed by Dennis Ritchie at Bell Laboratories in 1970 by the UNIX Operating system. At initially C was designed for implementing System software later it was widely used developing Application software. It is called middle level language because it reduces the gap b/w High level language and Low level language.
4 CHARACTERISTIC OF C Its popularity as a programming language as follows: Modularity Structured Middle Level Language Case Sensitive Free Form Extensibility Portability Flexibility Core Language
5 USES OF C PROGRAMMING It is primarily used for System software (Ex. Operating system ) and Embedded System application. C Language widely accepted by the Professional groups and researcher due to most of the libraries, compiler and interpreter of other programming are implemented in C. Due to Portability and convenience, C is also used as intermediate language for implementing other languages. C is widely used in end user application.
STRUCTURE OF C PROGRAM C Program has Different Sections: #include<stdio.h> Preprocessor Directives Global Declaration Function main void main( ) { Local Declaration Statements; } function 1( ) { Local Declaration Statements; } Declaration Section User defined Function 6
THE FIRST C PROGRAM To write a C program look like: Method-I #include<stdio.h> #include<conio.h> void main( ) { printf(“Welcome to the first C program”); getch(); } Method-II #include<stdio.h> #include<conio.h> int main( ) { printf(“Welcome to the first C program”); getch(); return (0); } Output: Welcome to the first C Program Output: Welcome to the first C Program 7
EXAMPLE OF C PROGRAM Example:-1 #in c lud e <std i o.h> #in c lud e < con i o.h> void main( ) { printf(“Welcome to the first C program”); printf(“Hello World”); getch(); } Example:-2 #in c lud e <std i o.h> #in c lud e< con i o. h > int main( ) { printf(“Welcome to the first C program\n”); printf(“Hello World”); getch(); return (0); } Output: Welcome to the first C program Hello World Output: Welcome to the first C programHello World Note:- ‘\n’ is an escape sequence and represents a newline character. 8
9 COMMON USED FILES IN C Every C Program has four kinds of files these are: Source File (Extension .c or . cpp ) Header File (Extension . h) Object File (Extension . O or . obj ) Executable File (Extension . exe )
COMPILING AND EXECUTING PROCESS Source File (.c) Compiler Linker Object File (.o or .obj) Library File (.h ) E x ecu t able File (.exe) 10
11 COMPILING AND EXECUTING PROCESS Assuming that you are using a Turbo C or Turbo C++ compiler here are the steps that you need to follow to Compile and Execute your first C program… Start the compiler at C > prompt. The compiler ( TC.EXE is usually present in C:\TC\BIN directory). Select New from the File menu. Type the program. Save the program using F2 under a proper name (say Program1.c ). Use Ctrl + F9 to Compile and Execute the program. Use Alt + F5 to view the output on the screen(Monitor).
12 D A T A TYPES Basic Data Types: Derived Data Types: Array, function, pointer and string. User Defined Data Types: Structure, union and enum. Data Type Keyword Used Size in bytes Format Specifi e r Use Character char 1 %c T o s t ore c h ar a c t er t ype data. Integer int 2 %d To store integer data. F l oa t i ng point float 4 %f T o s t ore f l oa t i ng po i nt number. Double double 8 %lf To store big floating point number. Valueless void - To use in functions.
13 INPUT AND OUTPUT STATEMENTS Input d ata t o the p rogram fr o m the standard i n put de v ice is called keyboard or mouse . Output produced by the program to a standard output device is called monitor . These func t ions are d e fined i n s tandard lib r a r y i s ca l led stdio.h (standard input/output header file).
14 INPUT FUNCTION scanf ( ): Reads input data from the standard input device. Syntax of scanf( ) function in c program. scanf(“Format_specifier”, Address_list); Format_specifier decides the type of values that need to be provided to the variable and these format specifiers are preceded by a % (is called percentage) sign. Address_list determines the address of memory locations where the input variable should be stored and sign & (is called ampersand) is used before the name of variable..
15 INPUT FUNCTION(Cont.) Example of scanf( ) function scanf(“%d”, &a); // To read a single variable a is integer scanf(“%d%d”, &a, &b); // Multiple variables a and b integer scanf(“%d%c”, &a, &b); // Multiple variables a is integer and b is character scanf(“%f%d”, &a, &b); // Multiple variables a is float and b is integer scanf(“%f%c”, &a, &b); // Multiple variables a is float and b is character scanf(“%f%f”, &a, &b); // Multiple variables a and b are float type Note: Ampersand( & ) symbol followed by the input variable .
16 OUTPUT FUNCTION printf( ): Display output data on the screen (monitor). Syntax of printf( ) function in c program. printf(“Format_specifier”, Variables_list); Format_specifier decides the type of values that need to be provided to the variable and these format specifiers are preceded by a % (is called percentage) sign. Variables_list represents the name of variables where the input data should be stored.
17 OUTPUT FUNCTION(Cont.) Let ’s see some examples of printf( ) function: printf(“Hello world”); printf(“%d”,a); printf(“%d%d”, a, b); printf( “%d%c”, a, b); printf( “%f%d”, a, b); printf( “%f%f”, a, b); printf( “%f%c”, a, b); // Display Hello world // Display integer value of a // Display integer values of a and b // Display integer a and character b // Display float a and integer b // Display float a and float b // Display float a and character b Note: Ampersand ( & ) symbol does not use by the output variable name .
PRO G RAMS The following programs help to more understand about scanf( ) and printf ( ) functions. Write a C program to calculate sum of two numbers. Outpu t: 30 #include<stdio.h> #includ e <con i o.h> void main( ) { int a, b, c; a = 10; b = 20; c = a + b; printf(“%d”, c); getch(); } 18
PROGRAMS(Cont.) Write a C program to calculate sum of two numbers using input function scanf ( ). Output: Enter a and b 10 20 30 #include<stdio.h> # inc l ude<conio.h> void main( ) { int a, b, c; printf( “Enter a and b”) ; scanf (“%d%d”, &a,&b); c = a + b; printf(“%d”, c); getch (); } 19
20 PRACTICE PROGRAMS Write a C program to print your resume with following details Personal like Name, Address, Email-id, Qualifications and Skills and others. Write a C program to calculate Simple interest. Write a C program to calculate sum and average of three integer numbers using scanf() function. Write a C program to swap two numbers.