You are on page 1of 8

C Programming C programming was developed by Denish Ritchie in 1972 at USA in AT and T- Bell lab.

C is programming language used to develop OS, Games etc. There are three types of programming languages. Low level programming: - Difficult, machine dependent. It is also called machine level language. Middle level programming(Assembly Language):- Easy to understand some are machine dependent or some are independent. High level language: - Easy to understand, every program are machine independent. Data types in C Program Char :- Store only one character at a time. Its size is in bit. Int = Store only number value without any Decimal point. Float = Store only numeric value with decimal point. Double = Store only numeric value with decimal but its size is very large or large than float. Derived Data Types Long Int = Store numeric value without any decimal. Its size is larger than int. short Int = Store numeric value without any decimal. Its size is smaller than int. unsigned int= Store only positive numeric value

Header Files #include:-It is a preprocessor directives. stdio:-Standard input output. conio:-console input output. Input output Statements: void main():-This line of the program indicates the point at which the working of the program begins. getch( ) / getche( ):- wait for a keypress and the return . clrscr( ):- Clear the screen. printf( ):-printf is used to print or display data on the console in a formatted form. It returns the number of characters written or a negative value if an error occurs. scanf( ):- scanf returns the number of data items which have successfully been assigned a value. Program-1 #include<stdio.h> #include<conio.h> void main() { printf(Welcome to C programming);

getch(); }

User Input and Output commands: %d :- For integer input and output. %c :- For single character. %f :- For floating point value. %s :- For string. %ld :- For long integer. %lf :- For double. Escape Characters: Escape Characters are used with the printf( ). Escape Characters are specified in the first argument of the printf( ) and are used mainly for screen formatting of the output. The escape characters are always preceded with a backspace (\). \n =For new line \t = For horizontal tab. & =ampersand is address of operator or the location of the variable where the variable is stored. Program-2 The use of: \t:-tab, \n:-new line: #include<stdio.h> #include<conio.h> void main() { printf(My\t name\t is\t Mr. Sam); printf(I Live in Nepal\n); } Operator in C: 1) Arithmetic Operator : + Plus Minus * Multiply / Divide % Modulus (To get the remainder) 2) Assignment Operator : = Equal to += Plus and Equal to -= Minus and Equal to *= Multiply and Equal to /= Divide and Equal to

%= Modulus and Equal to 3) Comparison and logical operator > Greater than < Less than >= Greater and Equal to <= Less and Equal to 4) Equality Operator == Equal to != Not Equal to 5) Logical Operator && And || Or ! Not #include<stdio.h> #include<conio.h> void main() { int n1,n2,res; n1=4; n2=6; res=n1+n2; printf(%d,res); //to show the value of n1 and n2,then printf(the addition of %d %d,n1n2,res); }




#include<stdio.h> #include<conio.h> void main() { float n1,n2,res; N1=4.5; N2=5.5; Res=n1+n2; printf(%f,res); //To keep only 2 digits after decimal type printf(%2f,res); //To get the result after executing type getch(); //after printf. } Program-2

#include<stdio.h> #include<conio.h> void main() { int a,b,c; printf(Enter the first number : ); scanf(%d, &a); printf(Enter the second number: ); scanf(%d, &b); c=a+b; printf(The sum of numbers is %d ,c); getch(); } Program-3 #include<stdio.h> #include<conio.h> void main() { int a,b; a=5; b=a++; printf(value of b : %d,b); b=a; printf(value of b : %d,b); getch(); }

Control statements: (1) Conditional statements. a) If else statement: - In this conditional statement we compare the two or more in a single or multiple statement. If the condition is true it goes to the very first output statement otherwise it goes to else statement. #include<stdio.h> #include<conio.h> void main() { int num,i; clrscr(); printf("Enter a number"); scanf("%d",&num); i=num%2; if(i==0) {

printf(Even number!); } getch(); } Example of if-else:#include<stdio.h> #include<conio.h> void main() { int num,i; clrscr(); printf("Enter a number"); scanf("%d",&num); i=num%2; if(i==0) { printf(Even number!); } else { printf(Odd number!); } getch(); } b) Switch statement: - Switch is a multiple secession maker. In switch the conditions are applied in case statement. The case will be integer or character type not a float integer string type. Every case statement is terminate with break statement, Break also terminate the whole program. Example: #include<stdio.h> #include<conio.h> void main() { int day; clrscr(); printf("Enter the days of a week:"); scanf("%d",&day); switch(day) { case 1: printf("Sunday"); break; case 2: printf("Monday"); break;

case 3: printf("Tuesday"); break; case 4: printf("Wednesday"); break; case 5: printf("Thursday"); break; case 6: printf("Friday"); break; case 7: printf("Saturday"); break; default: printf("Invalid day number!"); break; } getch(); } Looping Statement: A. For Loop:The For Loop continues to execute as long as the conditional test evaluates to true. When the condition becomes false the program comes out of the loop. There are three expressions in for loop. 1) Initialization 2) Condition 3) Increment or decrement operator. Increment 1) Postfix Increment: - First initialization than increment. 2) Prefix Increment: - First increment than initialization. Nested For Loop (1) Outer Loop: - First Loop. (2) Inner Loop: - Under First Loop. Example-1 #include<stdio.h> #include<conio.h> void main() { int i; clrscr(); for(i=1;i<=10;i++) { printf("%d\n",i); }

getch(); } Example-2 #include<stdio.h> #include<conio.h> void main() { int a,b; for(a=1;a<=5;a++) { for(b=1;b<=a;b++) { printf("*"); } printf("\n"); } getch(); } B. While Loop:The while loop repeats a statement or a set of statements while a certain specified condition is true. Example: #include<stdio.h> #include<conio.h> void main() { int i; clrscr(); i=0; while(i<10) { i=i+1; printf("%d\n",i); } getch(); } C. Do-while loop:The do-while statement will execute a block of code at least once, then will repeat the loop as long as a condition is true. Example loop: #include<stdio.h> #include<conio.h> void main()

{ int i; clrscr(); i=0; do { i=i+1; printf("%d\n",i); } while(i<10); getch(); }