C ~ Language
C ~ Language Introduction The Language is a software which is used to communicate with the computer for doing certain process. The language must have a compiler and do an operation. Origin  C – Language was created by Denise Ritchie and Brain Kernighan in Bell Laboratories  The Language available before the invention of C is B – Language, created by Ken Thompson. B leds to the development of C in 1970’s. The ANSI(American National Standard Institute) C standard was finally adopted in December 1989. C is a middle level language, because it combines the best elements of high level language with control and flexibility of assembly language. It is a case sensitive language.
C ~ Structure Documentation Section Link Section Definition Section Global Declaration return_type main(argumentlist) { variable declarations; Statements; } Sub Function Function 1 Function 2 : : Function n
Explanation  Documentation Section – This is used to provide some meaningful information about the program.  Link Section - In this section we include the header files whose functions are used in the program. Eg : #include<stdio.h>  Definition Section – In this section we use the Symbolic Constants Eg : # PI 3.14  Global Declaration – In this section we declare the variable which is going to used are accessed by any where in the program.  Main – This contains the coding (variable, statements) which are going to be executed. Eg: printf().  Function 1 … Function n – Sub functions which are used in the programs.
Steps to do a Program. Going to the C Editor.  Click Start Run (Type) Command. Then the command prompt will be appear. In that  Type as follows cd cd tc tc
In the Editor  To create a new file. Click File  New  To save a file Click File  Save(F2) Type the file name with the extension as .c Eg: Test.c  To open a file Click File Open(F3)  To Quit Click File Quit  To Compile file to see the errors Click Compile Compile(Alt+F9)  To run or execute a file to see the output Click Run Run(ctrl+F9)
Escape Sequence The escape sequence are used to format the statements which are going to be displayed on the screen. The most frequently used escape sequence are:  n – This is used to produce new line while display  t – This is used to produce tab while display.
Simple Program /* Simple Program */ Document section #include<stdio.h> Link Section #include<conio.h> void main() { clrscr(); printf("*"); printf("n Hai"); getch(); }
Comment Line The Comment Line Symbols are used to make the compiler not to execute. When a line is declared as comments, It will not be executed by Compiler. The types of Comments are :  Single Line Comment – ( //… ) It used to make a single line as a comment  Multi Line Comment – (/* … */) It is used to make more than one line as a comment
Variables & Constants Variables  These are the locations in the memory in which the given values are stored.  The values which can be changed during the execution of the program. Constants:  The values which cannot be changed during the execution of the program.
Data types This will indicate the type of the value which is going to be stored in the variable. Data type Delimiter ====== ====== char %s,%c int %d long int %ld float %f double %lf boolean --
Operators These are used to do certain operations. The types of operators are as follows.  Arithmetic Operators: These are used to do arithmetic operations. It consist of two operands. + This is used to do addition operation. - This is used to do subtraction operation. * This is used to do Multiplication Operation. / This is used to do Division operation and return “quotient” as result % This is used to do division operation and return “Remainder” as result.
 Relational Operators: These are used to do the relational operation. It can be the combination of two arithmetic expression. > Greater than < Less than >= Greater than or Equal to <= Less than or Equal to != Not Equal to  Logical Operators: These are used to do the logical operation. It can contain two relational expression. & And || or ! not
Example Program #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); a=5; b=5; c=a+b; printf("C is : %d",c); getch(); }
Control Statements These are used to control the execution of the program. Control Statements Conditional Unconditional Two Way Branching Multi Way Branching Go to Simple … If If … Else If … Else If Switch Case  Conditional Control Statement: Based on the given the execution of the program is controlled.  Uncnditional Control Statement: It is independent to control the execution. That means, it may or may not dependent on the condition.
Simple If: Syntax: ======= if(Cond) { statement; } #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); printf("Enter the Value For A : "); scanf("%d",&a); if(a>=0) { printf("The Value is Accepted."); } getch(); }
If...Else Syntax: ======== if(cond) { statement 1; } else { statement 2; }
#include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("Enter the Value for A : "); scanf("%d",&a); printf("Enter the value for B : "); scanf("%d",&b); if(a>b) { printf("A is Big."); } else { printf("B is Big."); } getch(); }
If … Else If Syntax: ======= if(Cond 1) { Statement 1; } else if(cond 2) { statement 2; } : : else if(cond n) { statement n; } else { else_Statement; }
#include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter A,B,C : "); scanf("%d%d%d",&a,&b,&c); if(a>b && a>c) { printf("t%d is Big.",a); } else if(b>c) { printf("t%d is Big.",b); } else { printf("t%d is Big.",c); } getch(); }
Nested If #include<stdio.h> #include<conio.h> main() { int a,b,c; clrscr(); printf("Enter the A,B,C : "); scanf("%d%d%d",&a,&b,&c); if(a>b) { if(a>c) { printf("A is Big"); } else { printf("C is Big"); } } else { if(b>c) { printf("B is Big"); } else { printf("C is Big"); } } getch(); }
Switch Case Syntax: ====== switch(exp) { case value_1: Statement 1; break; case value_2: Statemnt 2; break; case Value_3: Statement 3; break; : : case value_n: Statement n; break; default: default statement; break; }
#include<stdio.h> #include<conio.h> void main() { int a; clrscr(); printf("Enter the Value for A : "); scanf("%d",&a); switch(a) { case 1: printf("One"); break; case 2: printf("Two"); break; case 3: printf("Three"); break; default: printf("The Value is not between 1 and 3."); break; } getch(); }
Nested Case #include<stdio.h> #include<conio.h> void main() { char c; clrscr(); printf("Enter the Character : "); scanf("%c",&c); switch(c) { case 'a': case 'e': case 'i': case 'o': case 'u': printf("The Vowel in Small Letter."); break; case 'A': case 'E': case 'I': case 'O': case 'U': printf("The Vowel in Capital Letter."); break; default: printf("Invalid Value."); break; } getch(); }
Go to Syntax: ======= goto label #include<stdio.h> #include<conio.h> main() { int i=0; clrscr(); st: i=i+1; printf("i=%d",i); if(i<10) { goto st; } getch(); }
Loop: Set of Statements reapeted continously till the given condition is satisfied. Types ===== 1)Entry Control Loop 2)Exit Control Loop Entry Control Loop: =================== In the entry control loop, the given condition is checked first, if the condition is true then only the statements are executed. Example: ======== 1)For 2)while Exit Control Loop: ================== In the exit control loop, the statements are executed once, then the condition is verified. Example: ======== Do...while
Unary Operators  These operators are frequently in loops. ++ Increment operator: eg : i++(i=i+1) Pre Increment: At first the value of variable is incremented and then the action is carried out. Post Increment: At first the action is carried out then the value is Incremented. -- Increment operator: eg : i--(i=i-1) Pre Decrement At first the value of variable is decremented and then the action is carried out. Post Decrement At first the action is carried out then the value is decremented.
For  It is an entry control loop. Syntax: ===== for(initialization;test_condition;increment/decrement) { Statements; } Initialization: In this part, we initialize the value for the variable. Test_condition: In this part, the condition until which the loop is to be executed. Increment / decrement: Here, the variable is incremented or decremented to reach the condition
#include<stdio.h> #include<conio.h> void main() { int i; clrscr(); for(i=0;i<5;i++) { printf("i=%d",i); } getch(); }
while  It is an entry control loop Syntax: ==== While(condition) { Statements; }
#include<stdio.h> #include<conio.h> void main() { int i; clrscr(); i=0; while(i<5) { printf("i=%d",i); i=i+1; } getch(); }
Do … While  It is an exit control loop Syntax: ==== do { Statements; } while(cond);
#include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); do { printf("i=%d",i); } while(i<0); getch(); }
Arrays  Group of elements stored under a common name. Types: ===== 1)Single Dimensional Array 2)Double Dimensional Array
Single Dimensional Array: Syntax: ====== datatype variable_Name[Size]; initialization: =============== 1) During Declaration 2)Using Assignment Operator 2) In Runtime Through Scanf using loop
During Declaration  datatype variable_name[size]={value}; #include<stdio.h> #include<conio.h> main() { int a[5]={23,43,45,34,23}; char c[5]={‘a’,’e’,’i’,’o’,’u’}; clrscr(); printf(“The value in the cell a[2]=%d",a[2]); Printf(“nThe Character in the cell c[2]=%c”,c[2]); getch(); }
Using Assignment(=) Operator variable_name[cell]=value; #include<stdio.h> #include<conio.h> void main() { int a[3]; char c[3]; a[0]=1; a[1]=2; a[2]=3; c[0]='a'; c[1]='b'; c[2]='c'; clrscr(); printf("The Value in the Cell a[2]=%d",a[2]); printf("The Character in the cell a[2]=%c",a[2]); getch(); }
In Runtime Through Scanf using loop #include<stdio.h> #include<conio.h> main() { int a[5],i; clrscr(); for(i=0;i<5;i++) { printf("nEnter the Value for Cell - %d :",i); scanf("%d",&a[i]); } printf("nOutputn======"); for(i=0;i<5;i++) { printf("na[%d]=%d",i,a[i]); } printf("na[2]=%d",a[2]); getch(); }
Practical Example #include<stdio.h> #include<conio.h> main() { int reg[10],m1[10],m2[10],n; int tot[10],i; float avg[10]; clrscr(); printf("nEnter the No of Students:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("nEnter the Reg.No of Student - %d : ",i+1); scanf("%d",&reg[i]); printf("Enter the Mark1 of Student - %d : ",i+1); scanf("%d",&m1[i]); printf("Enter the Mark2 of Student - %d : ",i+1); scanf("%d",&m2[i]); } printf("nnOutputn======"); for(i=0;i<n;i++) { printf("nnReg.No of Student - %d is : %d",i+1,reg[i]); printf("nMark 1 of Student - %d is : %d",i+1,m1[i]); printf("nMark 2 of Student - %d is : %d",i+1,m2[i]); tot[i]=m1[i]+m2[i]; avg[i]=tot[i]/2; printf("nTotal of Student - %d is : %d",i+1,tot[i]); printf("nAvverage of Student - %d is : %f",i+1,avg[i]); } getch(); }
Two Dimensional Array  Syntax: ====== datatype variable_name[size][size]; #include<stdio.h> #include<conio.h> main() { int a[25][25],b[25][25],c[25][25]; int i,j,row,clm; clrscr(); printf("ntttMatrix Addition"); printf("nttt====== ========"); printf("nEnter the Number of Rows & Columns : "); scanf("%d %d",&row,&clm); printf("nnEnter the Matrix-1 Values.n");
for(i=0;i<row;i++) { for(j=0;j<clm;j++) { printf("Enter the Value for the Cell a[%d][%d] : ",i,j); scanf("%d",&a[i][j]); } } printf("nnEnter the Matrix-2 Values.n"); for(i=0;i<row;i++) { for(j=0;j<clm;j++) { printf("Enter the Value for the Cell b[%d][%d] : ",i,j); scanf("%d",&b[i][j]); } } printf("nOutputn======"); printf("nMatrix-1n"); for(i=0;i<row;i++) { for(j=0;j<clm;j++) { printf("t%dt",a[i][j]); } printf("n"); }
printf("nMatrix-2n"); for(i=0;i<row;i++) { for(j=0;j<clm;j++) { printf("t%dt",b[i][j]); } printf("n"); } printf("nMatrix Addition isn"); for(i=0;i<row;i++) { for(j=0;j<clm;j++) { c[i][j]=a[i][j]+b[i][j]; printf("t%dt",c[i][j]); } printf("n"); } getch(); }
Pointers  It is a variable which is used to access the address of other variable. Syntax: ===== Datatype *variable_name Example: ===== int *ptr;
#include<stdio.h> #include<conio.h> void main() { int *ptr,a; clrscr(); printf("Enter value for a :"); scanf("%d",&a); ptr=&a; printf("nThe Value of ptr : %u",ptr); printf("nThe Value of *ptr : %d",*ptr); getch(); }
Character Pointer #include<stdio.h> #include<conio.h> void main() { char nam[]={"hello"}; char *ptr={"hello"}; clrscr(); printf("nName = %s",nam); printf("n*ptr =%s",*ptr); getch(); }
Pointer as Array #include<stdio.h> #include<conio.h> void main() { int a[5],*ptr[5],i; clrscr(); for(i=0;i<5;i++) { printf("nEnter the Value for the cell a[%d] : ",i); scanf("%d",&a[i]); ptr[i]=&a[i]; } printf("nOutputn======nn"); for(i=0;i<5;i++) { printf("nThe value of ptr : %u the value of *ptr : %d",ptr[i],*ptr[i]); } getch(); }
Functions It is a sub program in which set of executable statements are stored under a name. Whenever the statements are needed, instead of repeating the statements the 'name' is called. The name in which the set of statements are stored is called "Function". Calling Function: ======= ========= The function which calls another function is called "Calling function" Called Function: ====== ========= The function which is called by another function is called "Called Function" Arguments: ========== These are the Values passed to the called function from calling function. The arguments in calling function is called "Actual Argument". The argument in called function is caled "Formal Argument". Return Value: ======= ===== Values return by the called function to the calling function.
Types  Without Argument Without Return Value  Without Argument With Return Value  With Argument Without Return Value  With Argument With Return Value
Without Argument Without Return Value #include<stdio.h> #include<conio.h> void func()//called function { printf("nControl is inside the function, "); printf("Hai, I am function"); } void main() { clrscr(); printf("nThe Control is going inside the function."); func(); //calling function prnitf("nThe Control Came out of the function and Enter main."); getch(); }
Without Argument With Return Value #include<stdio.h> #include<conio.h> int func()//called function { int x, y; int z; printf("Enter the value for x : "); scanf("%d",&x); printf("Enter the value for Y : "); scanf("%d".&y); z=x+y; return z; } void main() { int c; clrscr(); c=func(); //calling function prnitf("nThe result is : %d",c); getch(); }
With Argument Without Return Value #include<stdio.h> #include<conio.h> void func(int x,int y)//called function { int z; z=x+y; prnitf("nThe result is : %d",z); } void main() { int a,b,c; clrscr(); printf("Enter the value for a : "); scanf("%d",&a); printf("Enter the value for b : "); scanf("%d".&b); c=func(a,b); //calling function getch(); }
With Argument With Return Value #include<stdio.h> #include<conio.h> int func(int x,int y)//called function { int z; z=x+y; return z; } void main() { int a,b,c; clrscr(); printf("Enter the value for a : "); scanf("%d",&a); printf("Enter the value for b : "); scanf("%d".&b); c=func(a,b); //calling function prnitf("nThe result is : %d",c); getch(); }
Global Variable It is a variable which is commonly used by all the functions. #include<stdio.h> #include<conio.h> int g=10; void func()//called function { printf("nThe Control is inside functionn"); printf("ng=%d",g); } void main() { printf("nThe Control is in side the main"); printf("ng=%d",g); func(); g=g+10; func(); getch(); }
Recursive Function When a function calls itself then it is said to be recursive function. #include<stdio.h> #include<conio.h> rec(int n)//Called function { if(n>=0) { printf(“nn=%d”,n); rec(n-1); } else return 0; } void main() { int a=5; clrscr(); rec(a);//Calling function getch(); }
String Functions #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s1[20],s2[20]; clrscr(); printf("Enter S1 : "); scanf("%s",&s1); printf("Enter S2 : "); scanf("%s",&s2); printf("strrev(s1) : %s",strrev(s1)); //To reverse a string printf("nStrlen(s1) : %d",strlen(s1));//to find the length of the string printf("nStrupr(s1) : %s",strupr(s1));//to convert the string to upper case printf("nStrlwr(s1) : %s",strlwr(s1));//to convert the string to lower case printf("nstrcmp(s1,s2) : %d",strcmp(s1,s2));//to compare the string s2 with s1 and retun the result printf("nstrcpmi(s1,s2) : %d",strcmpi(s1,s2));//to compare the string s2 with s1 by not taking the case and retun the result printf("nstrcat(s1,s2) : %s",strcat(s1,s2));//to join the two strings getch(); }
Structure It is a collection of different items. Syntax: ======= struct structure_name { datatype 1 member 1; datatype 2 member 2; : : datatype n member n; }; struct structure_name structure_variable; structure_variable.member;
#include<stdio.h> #include<conio.h> struct stu { int rno; char name[25]; int eng,tam,mat,sci,cs; int tot; float avg; }; void main() { struct stu s; clrscr(); printf("Enter the Rno :"); scanf("%d",&s.rno); printf("Enter the Name : "); scanf("%s",&s.name); printf("Enter the English Mark : "); scanf("%d",&s.eng); printf("Enter the Tamil Mark : "); scanf("%d",&s.tam); printf("Enter the maths Mark : "); scanf("%d",&s.mat); printf("Enter the Science Mark : "); scanf("%d",&s.sci); printf("Enter the CS Mark :"); scanf("%d",&s.cs);
s.tot=s.eng+s.tam+s.mat+s.sci+s.cs; s.avg=(float)s.tot/5; clrscr(); printf("nOutputn======"); printf("nRno is : %d",s.rno); printf("nName is : %s",s.name); printf("nEnglish : %d",s.eng); printf("nTamil : %d",s.tam); printf("nMaths : %d",s.mat); printf("nSCience : %d",s.sci); printf("nCS : %d",s.cs); printf("nTotal : %d",s.tot); printf("nAverage : %f",s.avg); getch(); }
Structure as array #include<stdio.h> #include<conio.h> struct stu { int rno; char name[25]; int eng,tam,mat,sci,cs; int tot; float avg; }; void main() { struct stu s[3]; int i; clrscr(); for(i=0;i<2;i++) { printf("nnEnter the Details of Student - %dn",i+1) printf("Enter the Rno :"); scanf("%d",&s[i].rno); printf("Enter the Name : "); scanf("%s",&s[i].name); printf("Enter the English Mark : "); scanf("%d",&s[i].eng); printf("Enter the Tamil Mark : "); scanf("%d",&s[i].tam); printf("Enter the maths Mark : "); scanf("%d",&s[i].mat); printf("Enter the Science Mark : "); scanf("%d",&s[i].sci); printf("Enter the CS Mark :"); scanf("%d",&s[i].cs); s[i].tot=s[i].eng+s[i].tam+s[i].mat+s[i].sci+s[i].cs; s[i].avg=(float)s[i].tot/5; } clrscr();
printf("nOutputn======"); for(i=0;i<3;i++) { printf("nnThe Details of student - %d",i+1) printf("nRno is : %d",s[i].rno); printf("nName is : %s",s[i].name); printf("nEnglish : %d",s[i].eng); printf("nTamil : %d",s[i].tam); printf("nMaths : %d",s[i].mat); printf("nSCience : %d",s[i].sci); printf("nCS : %d",s[i].cs); printf("nTotal : %d",s[i].tot); printf("nAverage : %f",s[i].avg); } getch(); }
File Handlings  File is a place where we can store the data permanently  There are many ways to handle a file, in that we are going to see about 1) How to write the data to the file 2) How to read the data from the file
Writing to File #include<stdio.h> #include<conio.h> void main() { char str; FILE *fptr; clrscr(); fptr=fopen("test1.txt","w"); printf("Enter The Text and Press ^ to stop."); do { str=getchar(); putc(str,fptr); } while(str!='^'); getch(); }
Reading From File #include<stdio.h> #include<conio.h> void main() { char str; FILE *fptr; clrscr(); fptr=fopen("arun.txt","r"); str=getc(fptr); while(str!=EOF) { putchar(str); str=getc(fptr); } getch(); }

C_Language_PS&PC_Notes.ppt

  • 1.
  • 2.
    C ~ Language Introduction TheLanguage is a software which is used to communicate with the computer for doing certain process. The language must have a compiler and do an operation. Origin  C – Language was created by Denise Ritchie and Brain Kernighan in Bell Laboratories  The Language available before the invention of C is B – Language, created by Ken Thompson. B leds to the development of C in 1970’s. The ANSI(American National Standard Institute) C standard was finally adopted in December 1989. C is a middle level language, because it combines the best elements of high level language with control and flexibility of assembly language. It is a case sensitive language.
  • 3.
    C ~ Structure DocumentationSection Link Section Definition Section Global Declaration return_type main(argumentlist) { variable declarations; Statements; } Sub Function Function 1 Function 2 : : Function n
  • 4.
    Explanation  Documentation Section– This is used to provide some meaningful information about the program.  Link Section - In this section we include the header files whose functions are used in the program. Eg : #include<stdio.h>  Definition Section – In this section we use the Symbolic Constants Eg : # PI 3.14  Global Declaration – In this section we declare the variable which is going to used are accessed by any where in the program.  Main – This contains the coding (variable, statements) which are going to be executed. Eg: printf().  Function 1 … Function n – Sub functions which are used in the programs.
  • 5.
    Steps to doa Program. Going to the C Editor.  Click Start Run (Type) Command. Then the command prompt will be appear. In that  Type as follows cd cd tc tc
  • 6.
    In the Editor To create a new file. Click File  New  To save a file Click File  Save(F2) Type the file name with the extension as .c Eg: Test.c  To open a file Click File Open(F3)  To Quit Click File Quit  To Compile file to see the errors Click Compile Compile(Alt+F9)  To run or execute a file to see the output Click Run Run(ctrl+F9)
  • 7.
    Escape Sequence The escapesequence are used to format the statements which are going to be displayed on the screen. The most frequently used escape sequence are:  n – This is used to produce new line while display  t – This is used to produce tab while display.
  • 8.
    Simple Program /* SimpleProgram */ Document section #include<stdio.h> Link Section #include<conio.h> void main() { clrscr(); printf("*"); printf("n Hai"); getch(); }
  • 9.
    Comment Line The CommentLine Symbols are used to make the compiler not to execute. When a line is declared as comments, It will not be executed by Compiler. The types of Comments are :  Single Line Comment – ( //… ) It used to make a single line as a comment  Multi Line Comment – (/* … */) It is used to make more than one line as a comment
  • 10.
    Variables & Constants Variables These are the locations in the memory in which the given values are stored.  The values which can be changed during the execution of the program. Constants:  The values which cannot be changed during the execution of the program.
  • 11.
    Data types This willindicate the type of the value which is going to be stored in the variable. Data type Delimiter ====== ====== char %s,%c int %d long int %ld float %f double %lf boolean --
  • 12.
    Operators These are usedto do certain operations. The types of operators are as follows.  Arithmetic Operators: These are used to do arithmetic operations. It consist of two operands. + This is used to do addition operation. - This is used to do subtraction operation. * This is used to do Multiplication Operation. / This is used to do Division operation and return “quotient” as result % This is used to do division operation and return “Remainder” as result.
  • 13.
     Relational Operators: Theseare used to do the relational operation. It can be the combination of two arithmetic expression. > Greater than < Less than >= Greater than or Equal to <= Less than or Equal to != Not Equal to  Logical Operators: These are used to do the logical operation. It can contain two relational expression. & And || or ! not
  • 14.
    Example Program #include<stdio.h> #include<conio.h> void main() { inta,b,c; clrscr(); a=5; b=5; c=a+b; printf("C is : %d",c); getch(); }
  • 16.
    Control Statements These areused to control the execution of the program. Control Statements Conditional Unconditional Two Way Branching Multi Way Branching Go to Simple … If If … Else If … Else If Switch Case  Conditional Control Statement: Based on the given the execution of the program is controlled.  Uncnditional Control Statement: It is independent to control the execution. That means, it may or may not dependent on the condition.
  • 17.
    Simple If: Syntax: ======= if(Cond) { statement; } #include<stdio.h> #include<conio.h> void main() { inta; clrscr(); printf("Enter the Value For A : "); scanf("%d",&a); if(a>=0) { printf("The Value is Accepted."); } getch(); }
  • 18.
  • 19.
    #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("Enterthe Value for A : "); scanf("%d",&a); printf("Enter the value for B : "); scanf("%d",&b); if(a>b) { printf("A is Big."); } else { printf("B is Big."); } getch(); }
  • 20.
    If … ElseIf Syntax: ======= if(Cond 1) { Statement 1; } else if(cond 2) { statement 2; } : : else if(cond n) { statement n; } else { else_Statement; }
  • 21.
    #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("EnterA,B,C : "); scanf("%d%d%d",&a,&b,&c); if(a>b && a>c) { printf("t%d is Big.",a); } else if(b>c) { printf("t%d is Big.",b); } else { printf("t%d is Big.",c); } getch(); }
  • 22.
    Nested If #include<stdio.h> #include<conio.h> main() { int a,b,c; clrscr(); printf("Enterthe A,B,C : "); scanf("%d%d%d",&a,&b,&c); if(a>b) { if(a>c) { printf("A is Big"); } else { printf("C is Big"); } } else { if(b>c) { printf("B is Big"); } else { printf("C is Big"); } } getch(); }
  • 23.
    Switch Case Syntax: ====== switch(exp) { case value_1: Statement1; break; case value_2: Statemnt 2; break; case Value_3: Statement 3; break; : : case value_n: Statement n; break; default: default statement; break; }
  • 24.
    #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); printf("Enterthe Value for A : "); scanf("%d",&a); switch(a) { case 1: printf("One"); break; case 2: printf("Two"); break; case 3: printf("Three"); break; default: printf("The Value is not between 1 and 3."); break; } getch(); }
  • 25.
    Nested Case #include<stdio.h> #include<conio.h> void main() { charc; clrscr(); printf("Enter the Character : "); scanf("%c",&c); switch(c) { case 'a': case 'e': case 'i': case 'o': case 'u': printf("The Vowel in Small Letter."); break; case 'A': case 'E': case 'I': case 'O': case 'U': printf("The Vowel in Capital Letter."); break; default: printf("Invalid Value."); break; } getch(); }
  • 26.
    Go to Syntax: ======= goto label #include<stdio.h> #include<conio.h> main() { inti=0; clrscr(); st: i=i+1; printf("i=%d",i); if(i<10) { goto st; } getch(); }
  • 28.
    Loop: Set of Statementsreapeted continously till the given condition is satisfied. Types ===== 1)Entry Control Loop 2)Exit Control Loop Entry Control Loop: =================== In the entry control loop, the given condition is checked first, if the condition is true then only the statements are executed. Example: ======== 1)For 2)while Exit Control Loop: ================== In the exit control loop, the statements are executed once, then the condition is verified. Example: ======== Do...while
  • 29.
    Unary Operators  Theseoperators are frequently in loops. ++ Increment operator: eg : i++(i=i+1) Pre Increment: At first the value of variable is incremented and then the action is carried out. Post Increment: At first the action is carried out then the value is Incremented. -- Increment operator: eg : i--(i=i-1) Pre Decrement At first the value of variable is decremented and then the action is carried out. Post Decrement At first the action is carried out then the value is decremented.
  • 30.
    For  It isan entry control loop. Syntax: ===== for(initialization;test_condition;increment/decrement) { Statements; } Initialization: In this part, we initialize the value for the variable. Test_condition: In this part, the condition until which the loop is to be executed. Increment / decrement: Here, the variable is incremented or decremented to reach the condition
  • 31.
  • 32.
    while  It isan entry control loop Syntax: ==== While(condition) { Statements; }
  • 33.
  • 34.
    Do … While It is an exit control loop Syntax: ==== do { Statements; } while(cond);
  • 35.
  • 37.
    Arrays  Group ofelements stored under a common name. Types: ===== 1)Single Dimensional Array 2)Double Dimensional Array
  • 38.
    Single Dimensional Array: Syntax: ====== datatypevariable_Name[Size]; initialization: =============== 1) During Declaration 2)Using Assignment Operator 2) In Runtime Through Scanf using loop
  • 39.
    During Declaration  datatypevariable_name[size]={value}; #include<stdio.h> #include<conio.h> main() { int a[5]={23,43,45,34,23}; char c[5]={‘a’,’e’,’i’,’o’,’u’}; clrscr(); printf(“The value in the cell a[2]=%d",a[2]); Printf(“nThe Character in the cell c[2]=%c”,c[2]); getch(); }
  • 40.
    Using Assignment(=) Operator variable_name[cell]=value; #include<stdio.h> #include<conio.h> voidmain() { int a[3]; char c[3]; a[0]=1; a[1]=2; a[2]=3; c[0]='a'; c[1]='b'; c[2]='c'; clrscr(); printf("The Value in the Cell a[2]=%d",a[2]); printf("The Character in the cell a[2]=%c",a[2]); getch(); }
  • 41.
    In Runtime ThroughScanf using loop #include<stdio.h> #include<conio.h> main() { int a[5],i; clrscr(); for(i=0;i<5;i++) { printf("nEnter the Value for Cell - %d :",i); scanf("%d",&a[i]); } printf("nOutputn======"); for(i=0;i<5;i++) { printf("na[%d]=%d",i,a[i]); } printf("na[2]=%d",a[2]); getch(); }
  • 42.
    Practical Example #include<stdio.h> #include<conio.h> main() { int reg[10],m1[10],m2[10],n; inttot[10],i; float avg[10]; clrscr(); printf("nEnter the No of Students:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("nEnter the Reg.No of Student - %d : ",i+1); scanf("%d",&reg[i]); printf("Enter the Mark1 of Student - %d : ",i+1); scanf("%d",&m1[i]); printf("Enter the Mark2 of Student - %d : ",i+1); scanf("%d",&m2[i]); } printf("nnOutputn======"); for(i=0;i<n;i++) { printf("nnReg.No of Student - %d is : %d",i+1,reg[i]); printf("nMark 1 of Student - %d is : %d",i+1,m1[i]); printf("nMark 2 of Student - %d is : %d",i+1,m2[i]); tot[i]=m1[i]+m2[i]; avg[i]=tot[i]/2; printf("nTotal of Student - %d is : %d",i+1,tot[i]); printf("nAvverage of Student - %d is : %f",i+1,avg[i]); } getch(); }
  • 43.
    Two Dimensional Array Syntax: ====== datatype variable_name[size][size]; #include<stdio.h> #include<conio.h> main() { int a[25][25],b[25][25],c[25][25]; int i,j,row,clm; clrscr(); printf("ntttMatrix Addition"); printf("nttt====== ========"); printf("nEnter the Number of Rows & Columns : "); scanf("%d %d",&row,&clm); printf("nnEnter the Matrix-1 Values.n");
  • 44.
    for(i=0;i<row;i++) { for(j=0;j<clm;j++) { printf("Enter the Valuefor the Cell a[%d][%d] : ",i,j); scanf("%d",&a[i][j]); } } printf("nnEnter the Matrix-2 Values.n"); for(i=0;i<row;i++) { for(j=0;j<clm;j++) { printf("Enter the Value for the Cell b[%d][%d] : ",i,j); scanf("%d",&b[i][j]); } } printf("nOutputn======"); printf("nMatrix-1n"); for(i=0;i<row;i++) { for(j=0;j<clm;j++) { printf("t%dt",a[i][j]); } printf("n"); }
  • 45.
  • 47.
    Pointers  It isa variable which is used to access the address of other variable. Syntax: ===== Datatype *variable_name Example: ===== int *ptr;
  • 48.
    #include<stdio.h> #include<conio.h> void main() { int *ptr,a; clrscr(); printf("Entervalue for a :"); scanf("%d",&a); ptr=&a; printf("nThe Value of ptr : %u",ptr); printf("nThe Value of *ptr : %d",*ptr); getch(); }
  • 49.
    Character Pointer #include<stdio.h> #include<conio.h> void main() { charnam[]={"hello"}; char *ptr={"hello"}; clrscr(); printf("nName = %s",nam); printf("n*ptr =%s",*ptr); getch(); }
  • 50.
    Pointer as Array #include<stdio.h> #include<conio.h> voidmain() { int a[5],*ptr[5],i; clrscr(); for(i=0;i<5;i++) { printf("nEnter the Value for the cell a[%d] : ",i); scanf("%d",&a[i]); ptr[i]=&a[i]; } printf("nOutputn======nn"); for(i=0;i<5;i++) { printf("nThe value of ptr : %u the value of *ptr : %d",ptr[i],*ptr[i]); } getch(); }
  • 52.
    Functions It is asub program in which set of executable statements are stored under a name. Whenever the statements are needed, instead of repeating the statements the 'name' is called. The name in which the set of statements are stored is called "Function". Calling Function: ======= ========= The function which calls another function is called "Calling function" Called Function: ====== ========= The function which is called by another function is called "Called Function" Arguments: ========== These are the Values passed to the called function from calling function. The arguments in calling function is called "Actual Argument". The argument in called function is caled "Formal Argument". Return Value: ======= ===== Values return by the called function to the calling function.
  • 53.
    Types  Without ArgumentWithout Return Value  Without Argument With Return Value  With Argument Without Return Value  With Argument With Return Value
  • 54.
    Without Argument WithoutReturn Value #include<stdio.h> #include<conio.h> void func()//called function { printf("nControl is inside the function, "); printf("Hai, I am function"); } void main() { clrscr(); printf("nThe Control is going inside the function."); func(); //calling function prnitf("nThe Control Came out of the function and Enter main."); getch(); }
  • 55.
    Without Argument WithReturn Value #include<stdio.h> #include<conio.h> int func()//called function { int x, y; int z; printf("Enter the value for x : "); scanf("%d",&x); printf("Enter the value for Y : "); scanf("%d".&y); z=x+y; return z; } void main() { int c; clrscr(); c=func(); //calling function prnitf("nThe result is : %d",c); getch(); }
  • 56.
    With Argument WithoutReturn Value #include<stdio.h> #include<conio.h> void func(int x,int y)//called function { int z; z=x+y; prnitf("nThe result is : %d",z); } void main() { int a,b,c; clrscr(); printf("Enter the value for a : "); scanf("%d",&a); printf("Enter the value for b : "); scanf("%d".&b); c=func(a,b); //calling function getch(); }
  • 57.
    With Argument WithReturn Value #include<stdio.h> #include<conio.h> int func(int x,int y)//called function { int z; z=x+y; return z; } void main() { int a,b,c; clrscr(); printf("Enter the value for a : "); scanf("%d",&a); printf("Enter the value for b : "); scanf("%d".&b); c=func(a,b); //calling function prnitf("nThe result is : %d",c); getch(); }
  • 58.
    Global Variable It isa variable which is commonly used by all the functions. #include<stdio.h> #include<conio.h> int g=10; void func()//called function { printf("nThe Control is inside functionn"); printf("ng=%d",g); } void main() { printf("nThe Control is in side the main"); printf("ng=%d",g); func(); g=g+10; func(); getch(); }
  • 59.
    Recursive Function When afunction calls itself then it is said to be recursive function. #include<stdio.h> #include<conio.h> rec(int n)//Called function { if(n>=0) { printf(“nn=%d”,n); rec(n-1); } else return 0; } void main() { int a=5; clrscr(); rec(a);//Calling function getch(); }
  • 60.
    String Functions #include<stdio.h> #include<conio.h> #include<string.h> void main() { chars1[20],s2[20]; clrscr(); printf("Enter S1 : "); scanf("%s",&s1); printf("Enter S2 : "); scanf("%s",&s2); printf("strrev(s1) : %s",strrev(s1)); //To reverse a string printf("nStrlen(s1) : %d",strlen(s1));//to find the length of the string printf("nStrupr(s1) : %s",strupr(s1));//to convert the string to upper case printf("nStrlwr(s1) : %s",strlwr(s1));//to convert the string to lower case printf("nstrcmp(s1,s2) : %d",strcmp(s1,s2));//to compare the string s2 with s1 and retun the result printf("nstrcpmi(s1,s2) : %d",strcmpi(s1,s2));//to compare the string s2 with s1 by not taking the case and retun the result printf("nstrcat(s1,s2) : %s",strcat(s1,s2));//to join the two strings getch(); }
  • 62.
    Structure It is acollection of different items. Syntax: ======= struct structure_name { datatype 1 member 1; datatype 2 member 2; : : datatype n member n; }; struct structure_name structure_variable; structure_variable.member;
  • 63.
    #include<stdio.h> #include<conio.h> struct stu { int rno; charname[25]; int eng,tam,mat,sci,cs; int tot; float avg; }; void main() { struct stu s; clrscr(); printf("Enter the Rno :"); scanf("%d",&s.rno); printf("Enter the Name : "); scanf("%s",&s.name); printf("Enter the English Mark : "); scanf("%d",&s.eng); printf("Enter the Tamil Mark : "); scanf("%d",&s.tam); printf("Enter the maths Mark : "); scanf("%d",&s.mat); printf("Enter the Science Mark : "); scanf("%d",&s.sci); printf("Enter the CS Mark :"); scanf("%d",&s.cs);
  • 64.
    s.tot=s.eng+s.tam+s.mat+s.sci+s.cs; s.avg=(float)s.tot/5; clrscr(); printf("nOutputn======"); printf("nRno is :%d",s.rno); printf("nName is : %s",s.name); printf("nEnglish : %d",s.eng); printf("nTamil : %d",s.tam); printf("nMaths : %d",s.mat); printf("nSCience : %d",s.sci); printf("nCS : %d",s.cs); printf("nTotal : %d",s.tot); printf("nAverage : %f",s.avg); getch(); }
  • 65.
    Structure as array #include<stdio.h> #include<conio.h> structstu { int rno; char name[25]; int eng,tam,mat,sci,cs; int tot; float avg; }; void main() { struct stu s[3]; int i; clrscr(); for(i=0;i<2;i++) { printf("nnEnter the Details of Student - %dn",i+1) printf("Enter the Rno :"); scanf("%d",&s[i].rno); printf("Enter the Name : "); scanf("%s",&s[i].name); printf("Enter the English Mark : "); scanf("%d",&s[i].eng); printf("Enter the Tamil Mark : "); scanf("%d",&s[i].tam); printf("Enter the maths Mark : "); scanf("%d",&s[i].mat); printf("Enter the Science Mark : "); scanf("%d",&s[i].sci); printf("Enter the CS Mark :"); scanf("%d",&s[i].cs); s[i].tot=s[i].eng+s[i].tam+s[i].mat+s[i].sci+s[i].cs; s[i].avg=(float)s[i].tot/5; } clrscr();
  • 66.
    printf("nOutputn======"); for(i=0;i<3;i++) { printf("nnThe Details ofstudent - %d",i+1) printf("nRno is : %d",s[i].rno); printf("nName is : %s",s[i].name); printf("nEnglish : %d",s[i].eng); printf("nTamil : %d",s[i].tam); printf("nMaths : %d",s[i].mat); printf("nSCience : %d",s[i].sci); printf("nCS : %d",s[i].cs); printf("nTotal : %d",s[i].tot); printf("nAverage : %f",s[i].avg); } getch(); }
  • 68.
    File Handlings  Fileis a place where we can store the data permanently  There are many ways to handle a file, in that we are going to see about 1) How to write the data to the file 2) How to read the data from the file
  • 69.
    Writing to File #include<stdio.h> #include<conio.h> voidmain() { char str; FILE *fptr; clrscr(); fptr=fopen("test1.txt","w"); printf("Enter The Text and Press ^ to stop."); do { str=getchar(); putc(str,fptr); } while(str!='^'); getch(); }
  • 70.
    Reading From File #include<stdio.h> #include<conio.h> voidmain() { char str; FILE *fptr; clrscr(); fptr=fopen("arun.txt","r"); str=getc(fptr); while(str!=EOF) { putchar(str); str=getc(fptr); } getch(); }