Sei sulla pagina 1di 91

C LANGUAGE SYLLABUS 1)Introduction to C 2)Data types 3)Operators 4)Escape sequences 5)If condition 6)For loop 7)While

8)Do While

9)goto 10) Breaks** 11) Continue** 12)Gotoxy Concept* 12) Switch** 13) Array 14) String function** 15) Pointer 16) Function 17) Input Output Function 18) File 19) Structure 20) Scope variable declaration

C LANGUAGE
1

What is program? A program is set of instruction or commands used to solve any task or problems program is stored in file. Introduction of C language: 1) C is a structure programming language 2) "C" seems a strange name for a programming language was an offspring of Basic Combined Programming Language. (BCPL) called B. 3) Developed in 1960`s at Cambridge University. Dennis Ritchie modified B language. 4) It was implemented at Telephone Bell Laboratories in 1972. The new language name C. Since it was developed along with the Unix Operating System. 5) FORTRAN used for Engineering and Scientific Applications. 6) COBOL used for commercial application 7) Easily interact with hardware 8) C is a case sensitive 9) BCPL -> basic combined programming language is called B language. FEATURES OF C LANGUAGE: 1) C is a middle level Language 2) C is highly portable. That means C program written for one computer can be run on another. 3) C is also support for both high and low level languages.

4) C has an ability to extend itself. A "C" program is basically a collection of function that is supported by the library (Header files). 5) Extension of C file is <file name>.C Compiler and Interpreter: 1) Computer can understand only machine level coding. Computer cant understand our high level programming. So must translate our high level coding into machine level coding. 2) Both compiler and interpreter convert our high level program into machine low level code. 3) It also help to us debug the error in our program. 4) The only difference is compiler compiles entire program at a time and finally list all the errors whereas the execution of an interpreter is line by line. 5) The compiler is faster than the interpreter. C TOKENS: In a passage of text, Individual words and punctuation marks are called tokens. Similarly in c language the smallest individual units are known as TOKENS. 1) Keywords 2) Identifier 3) Constants 4) Strings 5) Operators 6) Special Symbols. To Enter into C language: Start Programs Turbo C++ IDE Click
3

Or

Click the icon (Turbo C++ IDE) on the desktop. Or Click the My Computer icon C Colon TC Folder Bin Folder Tc File click. Shortcut Keys: Meaning Window Full screen Working area full screen Close old program Close C software window Compile Run Output Save Open Trace Shortcut key ALT+Enter F5 ALT+F3 ALT+X Alt + F9 (Error Checking) Ctrl + F9 Alt + F5 F2 F3 F7

Structure of C program: <Comment Line> <Directives> main( )

{ <Statements>; . . . } Comment Line: * User understandable code * They are two types 1) Single line 2) Multi line Single line: * Your program titles have single line that time we use single Line comment line. Syntax: //<Statements> EG: //Addition of two numbers 2) Multiline: * Your program titles have more than one line that time We can use multi line.

Syntax: /* <Statements>.... ................... .........................*/


5

EG: /* Addition of two numbers Created by saranya*/ Directives: * Directive means all header files. 1) #include<stdio.h> standard input output .header 2) #include<conio.h> Conson input output.header 3) #include<math.h> Mathematical.header 4) #include<graphics.h> Graphical.header 5) #include<string.h> String.header here string means more than one character. main( ) * main is one of the functions. * used to display program output result. 2It doesnt end with semicolon. Statements: 3All statements are end with semicolon. printf():* It prints the message or output scanf():-

* Gets the data from user at run time.


Program: 1

//To display some text #include<stdio.h> #include<conio.h> void main( ) { clrscr(); printf("Welcome to C language "); getch(); } Execution of c program: 1) Write a c program in c software 2) Compile a program --> ALT+F9 3) Link to library or Run -> CTRL+F9 4) To see output --> ALT+F5 4To see output with out using ALT+ F9 that time we can use one header file that name is #include<conio.h> 5clrscr( ) is used to clear previous output . 6getch( ) is used to see output without using ALT+F5

Data types 8 bits 1 byte

Data type Int Float Char Long int Double Short int Unsigned int

Byte and Range 2 byte [-32,768 to 32,769] 4 byte 1 byte 4 byte 10 byte 2 byte 2 byte -128 to 127

VARIABLE: 7The combination of operand and operator is called variable. Data types: 8Data types are types of data. 9Data means collection of information. 10They are several types of data types 1) Integer 2) Floating points 3) Character 4) Double

Integer: 11It accepts only integer or decimal values only. Syntax:

int <variable name>; Eg: int a; a=2345; Float: 12It accepts only floating or point numbers.

Syntax: float <variable name>; Eg: float a; Eg: a=45.56; Character: 13Used to accept only single character only. Syntax: char <variable name>; Eg: char a; c='z';
9

Double: 14Used to accept both integer and also floating points. Syntax: double <variable name>; Eg: double a; a=10.45; (or) a=123; Printf ( ): 15Used to display output to user. a= 15; Data type Integer Float Char Double String Hexa Octal Printf types printf("%d",a); 10 printf("%f",a); 10.00 printf("%c",a); c printf("%d",a); 10.00 printf("%s",a); s printf("%x",a); printf("%o",a);

Scanf( ): 16Used to get some value or text from user at run time. & Addressof or ampersand Data type Integer Float Char Double String Hexa Octal Scanf types scanf("%d ",&a); scanf("%f ",&a); scanf("%c ",&a); scanf("%d ",&a); scanf("%s ",&a); scanf("%x ",&a); scanf("%o ",&a);

Program: 2

//Eg for data types(printf) #include<stdio.h> #include<conio.h> void main() { int i=32000; float f=32.89; double d=10.5; char c='z'; unsigned int u=65000; signed int s=-100; clrscr(); printf("int value is:%d",i); printf("\nfloat value is:%f",f);
11

printf("\ndouble value is:%d",d); printf("\nchar value is:%c",c); printf("\nunsigned int is:%u",u); printf("\nsigned int is:%d",s); getch(); }
Program: 3

//program for data types(scanf) #include<stdio.h> #include<conio.h> void main() { int a; char n[100]; clrscr(); printf("enter the a value:"); scanf("%d",&a); printf("enter any name:"); scanf("%s",&n); printf("hexa number is:%x",a); printf("\noctal number is:%o",a); printf("\ngiven name is:%s",n); getch(); } OPERATOR: Operator is a Symbol, Which is used to do some arithmetic Operations. 1)Arithmetic Operator: Operator + * Meaning Addition Subtraction Multiplication

/ % 2)Relational Operator: Operator > < >= <= == < >, !=

Division Modulus

Meaning Greater Than Lesser than Greater than or equal to Less than or equal to Equal to Not Equal to

3)Logical Operator: Operator && || ! Meaning AND (Both condition True) OR (Any one condition True) NOT

4)Assignment Operator: Operator += -= *= Meaning

13

/= %=

5)Unary Operator: Operator A++ ++a a---a Meaning Post increment Pre increment Post Decrement Pre Decrement

6)Conditional Operator: *Conditional operator is used to check condition. Syntax: (condition)? <true statement> : <false statement>;

7)Special Operator: Operator { } ( ) Meaning Left Brace Right Brace Left Parenthesis Right Parenthesis

[ ]

Left Square Bracket Right Square Bracket

8) Conversion function:

Conversion type %d %f %c %bakiya

Meaning Integer Float Single Character String

9) Escape Sequence:

\n \t \a \r \b \\ \/ \" \'

Next line Horizontal tab Bell Alert Carriage return Back space Front slash Back slash Double Quote Single Quote

15

Program: 4

//eg for escape sequence #include<stdio.h> #include<conio.h> void main() { clrscr(); printf("New line is=david\nbakthakumar"); printf("\nHorizontal tab is=david\tbakthakumar"); printf("\nBellalert is=david\abakthakumar"); getch(); }
Program: 5

//eg for arithmetic operator #include<stdio.h> #include<conio.h> void main() { .int a,b; clrscr(); printf("enter the a value:"); scanf("%d",&a); printf("enter the b value:"); scanf("%d",&b); printf("\naddition is:%d",(a+b)); printf("\nsubtraction is:%d",(a-b)); printf("\nmultiplication is:%d:",(a*b)); printf("\ndivision is:%d",(a/b)); printf("\nmodulus is:%d",(a%b));

getch(); }

Program:6 //eg for arithmetic operator #include<stdio.h> #include<conio.h> void main() { int days,month; printf(Enter the number of days); scanf(%d,&days); month=days/30; days=days%30; printf(month=%divya,days=%divya,month,days); getch(); }
Program:7

//eg for Relational Logical operator #include<stdio.h> #include<conio.h> void main() { int mon; clrscr(); printrf(Enter the value of month); scanf(%d,&mon); if(mon==12||mon==1||mon==2) { printf(Winter); } else if(mon==3||mon==4||mon==5) {
17

pirntf(Summer); } else if(mon==6||mon==7||mon==8) { printf(Spring); } else if(mon==9||mon==10||mon==11) { printf(Autumn); } else { printf(Please enter the no between 1 to 12); } getch(); }

CONDITIONS STATEMENTS: 1If is one of the condition statements. 2In If statements if condition is true then true statements are executed else false statements executed 2At a time only one statements are executed either true or false 3Types of If condition 1. If condition 2. Else if condition 3. Ladder If condition 4. Nested If condition

SYNTAX FOR IF CONDITION:

If (<condition>) { < True statements>;

}
SYNTAX FOR ELSE IF CONDITION:

If (<condition>) { < True statements>; } else { < False statements>; }

SYNTAX FOR LADDER IF CONDITION:

If (<condition>) {+ < True statements>; } else if (condition>) { < True statements>; } else if(<condition>) { < True statements>; } . . . else { < False statements>; }
SYNTAX FOR NESTED IF CONDITION:

19

If (<condition>) { < True statements>; if (condition>) { < True statements>; } else { < False statements>; } } else { <False statements>; }

PROGRAM: 8 //To check given number is odd or even #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); printf("Enter any value:"); scanf("%d",&a); if((a%2)==0) { printf("Even number"); } else { printf("Odd number");

} getch(); }
PROGRAM: 9

//To check given year is leap year or not #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); printf("Enter any year:"); scanf("%d",&a); if((a%4)==0)

{ printf("leap year"); } else { printf("not leap year"); } getch(); }


PROGRAM: 10

//To check given number positive or negative #include<stdio.h> #include<conio.h> void main() { int a; clrscr();
21

printf("Enter the a value:"); scanf("%d",&a); if(a<0) { printf("Negative"); } else if(a==0) { printf("Zero"); } else { printf("Positive"); } getch(); }
PROGRAM: 11

//To check which number is greater a or b #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("Enter the a value:"); scanf("%d",&a); printf("Enter the b value:"); scanf("%d",&b); if(a>b) { printf("A is greater"); } else { printf("B is greater"); } getch();

PROGRAM:12 //To check which number Pass or fail#include<stdio.h> #include<conio.h> void main() { int mark; clrscr(); printf(Enter the mark); scanf(%d,&mark); if(mark>=80) { printf(Honour); } else if(mark<80 && mark>=70) { printf(First class); } else if(mark<60 && mark>=50); { printf(Second class); } else { pirntf(Fail); } getch(); }

PROGRAM: 13

//To check given number is single or double or triple or four digits

23

#include<stdio.h> #include<conio.h> void main() { int a; clrscr(); printf("Enter the a value:"); scanf("%d",&a); if(a<10) { printf("single digit"); } else if(a<100) { printf("double digit"); } else if(a<1000) { printf("Triple digit"); } else if(a<10000) { printf("four digit"); } else { printf("Above four digit"); } getch(); }

PROGRAM: 14

//To check given two number are equal or not

#include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("Enter the a value:"); scanf("%d",&a); printf("Enter the b value:"); scanf("%d",&b); if(a==b) { printf("Equal"); } else { printf("Not Equal"); } getch(); }
PROGRAM: 15

//Eg for military selection #include<stdio.h> #include<conio.h> void main() { char n[100]; int h,w; clrscr(); printf("Enter the name:"); scanf("%s",&n); printf("Enter the height:"); scanf("%d",&h); printf("Enter the weight:"); scanf("%d",&w);
25

if((h>=175)&&(w>=75)) { printf("%s is selected",n); } else { printf("%s is rejected",n); } getch(); }

PROGRAM: 16

//To prepare student mark sheet #include<stdio.h> #include<conio.h> void main() { char n[100]; float e,t,m,s,so,tot,avg; clrscr(); printf("Enter the name:"); scanf("%s",&n); printf("Enter the English mark:"); scanf("%f",&e); printf("Enter the Tamil mark:"); scanf("%f",&t); printf("Enter the Maths mark:"); scanf("%f",&m); printf("Enter the Social science mark:"); scanf("%f",&so); printf("Enter the science mark:"); scanf("%f",&s); tot=e+t+m+s+so; printf("\nTotal is:%f",tot);

avg=tot/5; printf("\nAvg is:%f",avg); if(e>=40&&t>=40&&m>=40&&s>=40&&so>=40) { printf("\n%s is pass",n); if(avg>=75) { printf("\nGrade is:Dist"); } else if(avg>=60) { printf("\nGrade is:First"); } else if(avg>=50) { printf("\nGrade is:Second"); } else { printf("\nGrade is:Third"); } } else { printf("\n%s is fail",n); printf("\nGrade is:Nil"); } getch(); }

FOR LOOP: 4Loop is used to execute block of statements continuously until the condition is true. 5It is mainly used to avoid the reputation of coding. 6For is one of the loop statements. 7In for loop they have three functions.
27

1) Assign Starting value 2) Condition 3) Increment or Decrement 8They are two types 4) Normal For loop 5) Nested For loop
SYNTAX FOR NORMAL FOR LOOP:

For (<variable name>=<starting >; <condition>; <Inc / Dec >) { <Statements>; }


SYNTAX FOR NESTED FOR LOOP:

For (<variable name>=<starting >; <condition>; <Inc / Dec>) { <Statements>; For (<variable name>=<starting >; <condition>; <Inc / Dec>) { <Statements>; } }
Program: 17

//To display 1 to 10 by using for loop #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); for(a=1;a<=10;a++) { printf("%d\n",a);

} getch(); }

Program: 18

//To display my name as 100 times #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); for(a=1;a<=100;a++) { printf("saranya\t",a); } getch(); }
Program: 19

//To display #include<stdio.h> #include<conio.h> void main() { int i,j; clrscr(); for(i=10,j=1;i>=1,j<=10;i--,j++) { printf("%d,%d,",i,j); } getch(); }

29

Program: 20 //To find the factorial of given n number #include<stdio.h> #include<conio.h> void main() { int n,i,s=0; clrscr(); printf("Enter the n value:"); scanf("%d",&n); for(i=0;i<=n;i++) { s=s+i; } printf("\nsum of n number is:%d",s); getch(); }

/* N=5 s=0 s=1 s=3 s=6 s=10 s=15 */

i=1 i=2 i=3 i=4 i=5

Program: 21 //To find the factorial of given n number

#include<stdio.h> #include<conio.h> void main() { int n,i,s=1; clrscr(); printf("Enter the n value:"); scanf("%d",&n); for(i=1;i<=n;i++) { s=s*i; } printf("\nsum of n number is:%d",s); getch(); } N=5 s=1 s=2 s=6 s=24 s=120 */ WHILE: 9While is one of the condition statements. 10In while first they check condition after given true statements are executed. SYNTAX FOR WHILE CONDITION: While (<condition>) { < True statements>; } i=1 i=2 i=3 i=4 i=5 i=6

31

Program: 22

//To display odd number only 0 to 50 #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); while(i<=50) { printf("%d\t",i); i=i+2; } getch(); }
Program: 23

//to check how many odd and even numbers #include<stdio.h> #include<conio.h> void main() { int i=0,n,e=0,od=0; clrscr(); printf("Enter the n value:"); scanf("%d",&n); while(i<=n) { if((i%2)==0) { e=e+1; } else { od=od+1;

i++; } printf("\ntotal even number is:%d",e); printf("\ntotal odd number is:%d",od); getch(); }
Program: 24

//To check given number is prime or not #include<stdio.h> #include<conio.h> void main() { int n,i,p=0; clrscr(); printf("Enter the n value:"); scanf("%d",&n); for(i=2;i<n;i++) { if((i%n)==0) { p=1; } } if(p==1) { printf("Given number is Not prime number"); } else { printf("given number is prime number"); } getch();
33

DO WHILE: 11DO While is one of the Loop statements. 12In Do While first given statements are executed after they check condition. 13Do while brace end with semicolon. SYNTAX FOR DO WHILE CONDITION: do { < True statements>; } While (<condition>);

GOTO STATEMENTS: 14To transfer our execution program from one place to another place by using GOTO statements 15They are two types 6) Before GOTO 7) After GOTO SYNTAX FOR BEFORE GOTO: goto <label>; <statements>;

<label>: Program:25 //Eg for before goto #include<stdio.h> #include<conio.h> void main() { clrscr(); f: printf("\nsaranya"); goto f; getch(); }

SYNTAX FOR AFTER GOTO: <label>: <statements>; goto <label>; Program:26 //Eg for after goto #include<stdio.h> #include<conio.h> void main() { clrscr(); goto f; printf("saranya");
35

f: printf("\nnandhini"); getch(); } Gotoxy: It is used to move the cursor to particular position, which is given, by row and column at execution time.

Syntax: gotoxy(< column >,< row > ); Program:27 //EG for gotoxy concept #include <dos.h> #include <stdio.h> #include<conio.h> struct date d; struct time t; void main() { clrscr(); getdate(&d); gettime(&t); printf("\nNow date is:%d/%d/%d",d.da_day,d.da_mon,d.da_year); printf("\nNow time is:%d:%d:%d",t.ti_hour,t.ti_min,t.ti_sec); getch(); }

BREAK STATEMENTS: 16Break is one of the statements. 17Used to break the loop. SYNTAX FOR BREAK STATEMENTS: break; Program:28 //EG for break statements #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); for(a=1;a<=10;a++) { if(a==5) { break; } printf("%d\n",a); } getch(); }

CONTINUE STATEMENTS: 18Continue is one of the statements. 19Used to continue the loop SYNTAX FOR CONTINUE STATEMENTS:

37

continue;

Program:29 //EG for continue statements #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); for(a=1;a<=10;a++) { if(a==5) { continue; } printf("%d\n",a); } getch(); }

SWITCH: 20Switch also one of the condition statement 21In can check a condition by case format SYNTAX FOR SWITCH: switch( <Exp>) { case <Exp1>: <statements>;

break; case <Exp2>: <statements>; break; case <Exp3>: <statements>; break; . . . default: <default statements>; }

Program:30 //TO DISPLAY SWITCH CASE #include<stdio.h> #include<conio.h> void main() { int nday; clrscr(); printf("Enter the number between 1 to 7"); scanf("%d",&nday); switch(nday) { case 1: printf("\nSunday"); break; case 2: printf("\nMonday"); break; case 3: printf("\nTuesday"); break; case 4: printf("\nWednesday"); break; case 5: printf("\nThursday");break; case 6: printf("\Friday"); break; case 7: printf("\nSaturday"); break; default: printf("Invalid input"); } getch();
39

Array : 22Array means set of homogeneous function. 23Declare one variable store more than one value in it. 24Data type as in same variable name is called array. 25Here array start with zero. 26End with before the number. 27Array can classified into two types 1) single dimensional array 2) double dimensional array Syntax for single dimensional array: <data type> <variable name>[<size>]; Eg: int a[5];

Eg: int a[5];

Values: int a[0]; int a[1]; int a[2]; int a[3]; int a[4];

Syntax for initialization single dimensional array: <data type> <variable name>[<size>]={<values>}; Eg: int a[5]={34,46,2,23,56}; 2) Double dimensional array Syntax for double dimensional array: <Data type> <variable name>[<size>][<size>];

Eg: int a[3][3]; Syntax for initialization double dimensional array: <data type> <variable name>[<size>][<size>]={<values>}{<values>};

Eg:
41

int a[3][3]={34,46,2}{35,23,56}; Eg: int arr[3][4]; VALUE: arr[0][0]; arr[0][1]; arr[0][2]; arr[0][3]; arr[1][0]; arr[1][1]; arr[1][2]; arr[1][3]; arr[2][0]; arr[2][1]; arr[2][2]; arr[2][3];

Transpose of matrix: Matrix order= row * col A= T A= 1 2 3 4 1 4 7 1 2 3 2 5 8 4 5 6 3 6 9 7 8 9

5 6 7 8 9 1 4 7 2 5 8 3 6 9

A=

2 2 2 2 2 2

2 2 2 2 2 2

2 2 2 2 2 2

B=

C=A+B 4 C= 4 4

4 4 4

4 4 4

C=A-B 0 C= 0 0

0 0 0

0 0 0
43

Program:31 \\Sum of array #include<stdio.h> #include<conio.h> void main() { int i; int a[5]; int sum=0; clrscr(); printf("Enter Array elements"); for(i=0;i<5;i++) { scanf("%d",&a[i]); } printf("Array elements are:"); for(i=0;i<5;i++) { sum=sum+a[i]; } printf("sum is:%d",sum); getch(); }

Program:32

//Example for Matrix multiplication using double dimension array #include<stdio.h> #include<conio.h> void main()

{ int a[3][3],b[3][3],c[3][3]; int i,j,k; clrscr(); printf("\n Enter values for Array A:"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } printf("\n Enter values for Array B:"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&b[i][j]); } } printf("\n Multiplication Result is "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) { c[i][j]=c[i][j]+(a[i][k]*b[k][j]); } } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("\n C[%d][%d] value is =%d",i,j,c[i][j]); } } getch();
45

Program:33

//Matrix subtraction #include<stdio.h> #include<conio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j; clrscr(); printf("Enter the A matrix value:"); for(i=0;i<3;i++) for(j=0;j<3;j++) { printf("\nEnter the value for a[%d][%d]:",i,j); scanf("%d",&a[i][j]); } printf("Enter the B matrix value:"); for(i=0;i<3;i++) for(j=0;j<3;j++) { printf("\nEnter the value for b[%d][%d]:",i,j); scanf("%d",&b[i][j]); } for(i=0;i<3;i++) for(j=0;j<3;j++) { c[i][j]=a[i][j]-b[i][j]; } printf("Matrix subtraction is:\n"); for(i=0;i<3;i++) { printf("\n\n"); for(j=0;j<3;j++) {

printf("\t\t"); printf("%d",c[i][j]); } } getch(); }

Program:34 //Transpose of matrix #include<stdio.h> #include<conio.h> void main() { int i,j,a[3][3]; clrscr(); printf("Enter the matrix values:"); for(i=0;i<3;i++) for(j=0;j<3;j++) { printf("\nEnter the values for a[%d][%d]:",i,j); scanf("%d",&a[i][j]); } printf("Given A matrix values are:\n"); for(i=0;i<3;i++) { printf("\n\n"); for(j=0;j<3;j++) { printf("\t\t"); printf("%d",a[j][i]); } } getch(); }

47

Program:35 //to display one diemsional array: #include <stdio.h> #include<conio.h> void main() { int a[5],sum=0; clrscr(); printf("Enter teh fice number"); for(i=1;i<=5;i++) { scanf("%d",&a[i]); sum=sum+a[i]; } printf("The sum of given number %d\n",sum); getch(); } Program:36 //Eg for array concept #include<stdio.h> void main() { int n[5],i; printf("Enter the five values:\n"); for(i=0;i<5;i++) { scanf("%d",&n[i]); } printf("Given five values are:\n"); for(i=0;i<5;i++) { printf("%d\n",n[i]); }

Program:37 //To display Ascending order //bubble sort #include<stdio.h> #include<conio.h> void main() { int n,a[100],i,j,t; clrscr(); printf("Enter how many number you check:"); scanf("%d",&n); printf("Enter the %d values\n",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) for(j=0;j<n;j++) { if(a[i]<a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } printf("\nAscending values are:\n"); for(i=0;i<n;i++) { printf("%d\n",a[i]); } getch(); }

49

Program:38 //To display Descending order #include<stdio.h> #include<conio.h> void main() { int n,a[100],i,j,t; clrscr(); printf("Enter how many number you check:"); scanf("%d",&n); printf("Enter the %d values\n",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) for(j=0;j<n;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } printf("\nDescending values are:\n"); for(i=0;i<n;i++) { printf("%d\n",a[i]); } getch(); }

Program:39 \\Array rows of columns:#include<stdio.h> #include<conio.h> void main() { int a[10][[10],n,m,i,j; clrscr(); printf("\nEnter rows"); scanf("%d",&n); printf("\nEnter columns"); scanf("%d",&m); printf("Get the input array"); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%d",&a[i][j]); } } printf("\nDisplay array\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) { printf("%d\t",a[i][j]); } printf("\n"); } getch(); }

51

Program:40

\\String of array: #include<stdio.h> #include<conio.h> void main() { char a[5][20]={"kkk","hhh","fff","uuu","ddd"}; int i,j; clrscr(); printf("\nEnter name"); for(i=0;i<5;i++) { printf("%s",a[i][j]); } getch(); } Program:41 //TO display Two diemsional array: #include<stido.h> #include<conio.h> void main() { int i,j,arr[3][4]; clrscr(); for(i=0;i<3;i++) for(j=0;j<4;i++) arr[i][j]=(i*4)+j+1; printf("printing the array contents"); for(i=0;i<3;i++) { for(j=0;j<4;i++) printf("%4d",arr[i][j]); printf("\n");

} getch(); } D STRING FUNCTIONS: 28String means more than one character or set of character or bulk of character. 29It has one header file #include<string.h>

30They are several types of functions. 1)Strlen (String Length) 2) Strupr (String Upper) 3) Strlwr (String Lower) 4) Strrev (String Reverse) 5) Strcpy (String Copy) 6) Strcat (String Concatenation) 7) Strcmp(String Compare) 8) Stricmp(String Compare Ignore case) 1) Strlen ( ): 31It is used to find out the length of the string. Syntax: ====== <int variable >=strlen(<str1>); Program:42 //To find the length of the string #include<stdio.h>
53

#include<conio.h> #include<string.h> void main() { char n[100]; int i; clrscr(); printf("\nEnter any string:"); scanf("%s",&n); i=strlen(n); printf("\nLength of the string is:%d",i); getch(); }

2) Strlwr ( ): 5Used to convert a given string as upper case to lower case. Syntax: Strlwr(<str1>); Program:43 //To convert lower case to upper case #include<stdio.h> #include<conio.h> #include<string.h> void main() { char n[100];

clrscr(); printf("Enter any string as lower case:"); scanf("%s",&n); strupr(n); printf("upper case string is:%s",n); getch(); }

3) Strupr(): 6Used to convert a given string as lower case to upper case. Syntax: Strupr(<str1>);

Program:44 //To convert upper case to lower case #include<stdio.h> #include<conio.h> #include<string.h> void main() { char n[100]; clrscr(); printf("Enter any string as upper case:"); scanf("%s",&n); strlwr(n); printf("Lower case string is:%s",n);
55

getch(); }

Program:45 //Eg for string reverse #include<stdio.h> #include<conio.h> #include<string.h> void main() { char n[100]; clrscr(); printf("Enter any string:"); scanf("%s",&n); strrev(n); printf("String reverse is:%s",n); getch(); } 5) Strcpy(): 7Used to copy a string from last variable to front variable. Syntax: strcpy(<str1>,<str2>); Eg: strcpy(b,a); Program:46

//EG for string copy #include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[100],b[100]; clrscr(); printf("Enter the string for a:"); scanf("%s",&a); strcpy(b,a); printf("B string is:%s",b); getch(); }

6) Strcat(): 8strcat() is used to join two strings and store in the first character variable. Syntax: Strcat(<str1>,<str2>); Program:47 //Eg for string cat #include<string.h> #include<conio.h> #include<stdio.h> void main() { char a[100],b[100]; clrscr(); printf("Enter the a string:");
57

scanf("%s",&a); printf("Enter the b string:"); scanf("%s",&b); strcat(a,b); printf("Attach string is:%s",a); getch(); }

7) Strcmp(): 9Strcmp() is used to compare two strings with case sensitive. Program:48 //Eg for stringcompare #include<stdio.h> #include<string.h> #include<conio.h> void main() { char a[100],b[100]; clrscr(); printf("Enter the string for a:"); scanf("%s",&a); printf("Enter the string for b:"); scanf("%s",&b); if(strcmp(a,b)==0) { printf("Given two string are equal"); } else { printf("Given two string are not equal"); } getch(); }

Syntax: 1) It returns zero when two strings are equal. 2) It returns (-1) when st1 is less than st2. 3) It returns (+1) when st1 is greater than st2. Syntax: strcmp(<Strl>, <str2>) 8)Stricmp(): 10Stricmp() is used to compare two strings without case sensitive. Syntax: Stricmp(<str1>,<str2>);

Program:49 //Eg for stringignorecompare #include<stdio.h> #include<string.h> #include<conio.h> void main() { char a[100],b[100]; clrscr(); printf("Enter the string for a:"); scanf("%s",&a); printf("Enter the string for b:");
59

scanf("%s",&b); if(stricmp(a,b)==0) { printf("Given two string are equal"); } else { printf("Given two string are not equal"); } getch(); } Pointer: 2Pointer is a variable, which is used to store the address of another variable. 3Pointer is one of the variables. 4Used to find out the address of another variable value. 5Here & (address of) find the address. 6Here *(Asterisk) display the given value Advantages of pointer: (i) Easy any link of variable. (ii) Execution time is less.

* Print a value & Print an address. The lifetime of a variable will starts when a function is called and it destroys when the function is completed. Syntax for declaration: <data type> *<variable name>; Eg:

int *a; Program:50 #include<stdio.h> #include<conio.h> void main() { char a; int x; float p,q; clrscr(); a='A'; x=125; p=10.25; q=18.75; printf("%c is stored address %u\n",a & a); printf("%d is stored address %u\n",x, &x); printf("%f %f is stored address %u\n", p,q,&p,&q); getch(); } Program:51 //Pointer connecting with array #include<stdio.h> #include<conio.h> void main() { int i,*pt; int x[5]={4,6,7,12,10}; clrscr(); pt=x; for(i=0;i<5;i++) { *pt=*pt+5; printf("\n Value is=%d",*pt); pt++; } getch();
61

Program:52 #include<stdio.h> #include<conio.h> void main() { int x=10,y=10; int a,b,c,d; int *p1=&x, *p2=&y; a=(*p1)++; b=--(*p2); c=*p1+(*p2)--; d=++(*p2)--; printf("\n %d\n %d \n %d\n %d",a,b,c,d); getch(); }

Program:53 //to display given value but we use pointer #include<stdio.h> #include<conio.h> void main() { int a; int *p; clrscr(); printf("Enter the a value:"); scanf("%d",&a); printf("\nA value is:%d",a);

p=&a; printf("\nAddress of a value is:%d",*p); getch(); } Program:54 //Example for pointer with character array #include<stdio.h> #include<conio.h> void main() { int i; char *name[5]; clrscr(); printf("\n Enter five names"); for(i=0;i<5;i++) { gets(name[i]); } printf("\n Output"); for(i=0;i<5;i++) { printf("\n %d name is =%s",i+1,name[i]); } getch(); } Program:55 //Example for pointer connecting with function #include<stdio.h> #include<conio.h> void main() { int x,y; clrscr();
63

printf("\n Enter value for x"); scanf("%d",&x); printf("\n Enter value for Y"); scanf("%d",&y); printf("\n Before calling function"); printf("\n x value is =%d",x); printf("\n y value is =%d",y); swap(&x,&y); printf("\n After Calling Function"); printf("\n X value is =%d",x); printf("\n Y value is =%d",y); getch(); } swap(int *a,int *b) { int t; t=*a; *a=*b; *b=t; } Program:56 //Example for Pointer #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); printf("\nEnter a number"); scanf("%d",&a); printf("\n A value is=%d",a); printf("\n Address of A is =%u",&a); getch(); }

Program:57 //Example2 for pointer #include<stdio.h> #include<conio.h> void main() { int a=10; int *p; clrscr(); printf("\n \n A =%d",a); printf("\n \nAddress of A =%u",&a); p=&a; printf("\n \n P address=%u",p); printf("\n \n Value of P=%d",*p); printf("\n \n A value=%d",*&a); *p=*p+30; printf("\n \n Value of P=%d",*p); printf("\n \n A value is =%d",a); getch(); } Program:58 //Example3 for pointer to different datatypes #include<stdio.h> #include<conio.h> void main() { int a; char b; float c; int *pt1;
65

char *pt2; float *pt3; clrscr(); a=10; b='s'; c=25.16; pt1=&a; pt2=&b; pt3=&c; printf("\n value \t Address"); printf("\n %d \t %u",a,pt1); printf("\n %c \t %u",b,pt2); printf("\n %f \t %u",c,pt3); *pt1=40; *pt3=c+20.22; printf("\n Value of a=%d",a); printf("\n Value of pt3 is =%f",*pt3); getch(); }

Functions: 1Functions are sub programs, which are having repetative statements. Advantages of functions: 1Less memories space 2Less execution time 3Simplifies of coding 4Easy to display Types of function: 5Function declaration

6Function calling 7Function definition Function declaration: Syntax:

<Return type> <function name>( ); Function calling: Syntax: <Function name>(<arguments>); Function definition: Syntax: <Return type> <function name>(<argument>) { <Body of the statement>; }

Important factors when you are using function: Declaration of function optional when a function declared with argument that data type only should be used in calling and definition. Function should return a value A function can be any number of times. Number of argument specified in calling is equal to the definition.
67

The order in which a function has called in the same way there is no rule to definition the function to same way. Function should have a return a value. If the return is not mentioned the default definition is integer. The variables, which are used in function, get to be "argument or parameter. Program:59 //To find the fibonacci series by using function #include<stdio.h> #include<conio.h> void get(); int n,i=0,j=1,k,c; void main() { clrscr(); printf("Enter how many fibonacci series you want:"); scanf("%d",&n); get(); getch(); } void get() { printf("Fibonacci series value is:\n"); printf("%d\n",i); printf("%d\n",j); for(c=1;c<=n-2;c++) { k=i+j; printf("%d\n",k);

i=j; j=k; } }

Program:60 //To find the address of another variable value #include<stdio.h> #include<conio.h> void main() { int a; int *p; clrscr(); printf("Enter the a value:"); scanf("%d",&a); printf("\nA value is:%d",a); p=&a; printf("\nAddress of a value is:%d",p); getch(); }

Program:61 //To swapping three numbers by using function #include<stdio.h> #include<conio.h>


69

void main() { void disp();//Function declaration clrscr(); disp();//To invoke or call function getch(); } void disp()//Function definition { int a,b,c,t; printf("Enter the a value:"); scanf("%d",&a); printf("Enter the b value:"); scanf("%d",&b); printf("Enter the c value:"); scanf("%d",&c); t=a; a=c; c=t; printf("\nA value is:%d",a); printf("\nB value is:%d",b); printf("\nC value is:%d",c); } Arguments are of two types: (i) Actual arguments: 15Arguments, which are, function calling. (ii) Formal arguments: 16Arguments, which are, function definition.

Return statement: 17the function return a value through return statement. 18It is four ways (i) return; (ii) return(variable); (iii) return(number); (iv) return(expression); Parameter passing: 19They are two types: i)Call by value: 20If a value passed from an actual argument, formal argument then if any change in formal argument, will not affect in actual argument. if concept is called are call by value. 21C permits only "call by value method of parameter passing. Program:62 //Eg for call by value function #include<stdio.h> #include<conio.h> void saran(int *c,int*d); int a,b; void main() { clrscr(); printf("Enter the a value:");
71

scanf("%d",&a); printf("Enter the b value:"); scanf("%d",&b); saran(&a,&b); getch(); } void saran(int *c,int *d) { printf("\nA value is:%d",*c); printf("\nB value is:%d",*d); } ii)Call by reference: 22Instead passing a actual argument value to a function. If the address of the actual argument is than any change in formal argument it also changes the value in actual argument.

Program:63 //Eg for call by reference function #include<stdio.h> #include<conio.h> void kumar(int *c,int*d); int a,b; void main() { clrscr();

printf("Enter the a value:"); scanf("%d",&a); printf("Enter the b value:"); scanf("%d",&b); kumar(&a,&b); getch(); } void kumar(int *c,int *d) { printf("\nA value is:%d",c); printf("\nB value is:%d",d); } 3Single character input function: (i) Getchar ( ): 23It accepts a character value upto you enter but it will store only one character. Syntax: <Character variable>=getchar(); Program:64 #include<stdio.h> #include<conio.h> void main() { char ch; clrscr(); printf(Enter the character\digit);
73

ch=getchar();
if(isalpha(ch)>0); printf(It is a alpha); else if(isdigit(ch)>0) printf(It is a digit); else printf(It is a alphanumeric); getch(); } (ii) Getche( ): 24It accepts only one character. Syntax: <Character variable>=getche(); (iii) Getch ( ): 25It accepts one character without display. Syntax: <Character variable>=getch(); single character output function: (i) putchar(): 26It is used to print a single character. Syntax: putchar(<character variable>); Program:65 #include<stdio.h> #include<conio.h> void main()

{ char ch(); printf(Etner the lowercase\uppercase); ch=getchar(); if(islower(ch)) putchar(toupper(ch)) else putchar(tolower(ch)) getch(); } String input function: (i)gets(): Gets is used to get string with space (like sentence). syntax: <string variable>=gets();

Program:62 #include<stdio.h> #include<conio.h> void main() { char s[40]; printf(Enter the name); gets(s); puts(print the name); puts(s); getch(); }

Sting output function:


75

(i) puts(): 27puts is used to print string value. Syntax: puts(<string variable>);

Program:63 #include<stdio.h> #include<conio.h> void main() { int sno,m1,m2,m3; char name[20]; float total, avg; printf(Enter the sno,name,m1,m2,m3); scanf (%d%d%d,&sno,name,&m1,&m2,&m3); total=m1+m2+m3; avg=total/3; printf(Sno%d,sno); printf(Name %c,Name); printf(Marks%d\t%d\t%d\t,m1,m2,m3); printf(Total %f,total); printf(Avg %f,avg); getch(); }

FILE 28File is a collection of data (text & numbers). In order to store the data permanently, the data should be stored in the hard disk. For that data are stored in a file.

Files are two types: 1) Text file 2) Binary file Opening a file: 29The function fopen() is used to open a file. Syntax: FILE * <file pointer name>; <file pointer name>=fopen (<file name>, <file mode>); 30The file, which is opened that, should be assigned to a file pointer. 31Using that pointer only we can able to do any process in the file. Closing a file: 32This function fclose () is used to close a file. Every file should be closed which is already opened. FILE MODES: 33Three major types of file modes are present. 1) r => Which is used to open the file in read mode. 2) w => Which is used to open the file in write mode. 3) a => Which is used to open the file in append mode.( Adding new content to old file) Reading and Writing the data from a file:
77

1) fgetc => Reads a character from the files 2) fputc=> print only

Syntax: fgetc(File pointer); Program:64 //To Display file fgetc() #include<stdio.h> #include<conio.h> void main() { FILE *fp; char c; clrscr(); fp=fopen("e.txt","w"); do { scanf("%c",&c); putc(c,fp); } while(c!='q'); fclose(fp); fp=fopen("e.txt","r"); do { c=getc(fp); printf("%c",c); } while(c!=EOF); getch();

} Program:65 //To Display file getc() #include<stdio.h> #include<conio.h> void main() { FILE * fp; char ch; char fn[20]; clrscr(); printf("Enter the file name"); gets(fn); fp=fopen(fn,"r"); ch=fgetc(fp); while(ch!=EOF) { putchar(ch); ch=fgetc(fp); }

fclose(fp); getch(); } 2) fputc => Writes a single character in the file. Syntax: fputc(<variable name>,<file pointer>);

79

Program:66 //To Display file fputc() #include<stdio.h> #include<conio.h> void main() { FILE * fp; char str; char fn[20]; clrscr(); printf("Enter the file name"); gets(fn); fp=fopen(fn,"w"); clrscr(); printf("Enter ^ for end of the file"); do { str=getchar(); fputc(str,fp); } while(str!='^'); fclose(fp); getch(); }

Program:67 //To write into another file #include<stdio.h> #include<conio.h> void main() { char c,s[100]; FILE *f;

clrscr(); printf("Enter the file name with directory:"); scanf("%s",s); f=fopen(s,"w"); while(c!='$') { fputc(c,f); scanf("%c",&c); } }

Program:69 //To Display file putc() #include<stdio.h> #include<conio.h> void main() { FILE * fp; char ch; clrscr(); printf("Data input"); fp=fopen("lakshmi","w"); while((ch=getchar())!=EOF) putc(ch,fp); fclose(fp); printf("Data output"); fp=fopen("lakshmi","r"); while((ch=getc(fp))!=EOF) printf("%c",ch); fclose(fp); getch(); }

81

void main() Program:70 //To Display file emp() #include<stdio.h> #include<conio.h> { char name[10]; char ans='y'; float basic; int age; FILE *fp; clrscr(); fp=fopen("emp.txt","w"); if(fp==NULL) { printf("cannot open a file\n"); exti(); } while((ans=='y') || (ans=='y')) { printf("Enter employee details\n"); scanf("%s%d%f",name,&age,&basic); fprintf(fp,"%s%d%f",name,age,basic); printf("Enter another employee details\n"); fflush(stdin); scanf("%c",&ans); } fclose(fp); getch(); } Random Access Files: 1) fseek()

This function is used to move the file pointer position to the desired location. Syntax: fseek(<file pointer>,<offset>, <Position>); Note: 34Offset specifies the number of position to be moved.

Position Values: Value 0 1 2

Meaning Beginning of file Current position End of file

2) Rewind ( ): 35Rewind() is used to set the file pointer to the starting position.

Syntax: rewind(); 3) ftell(): 36ftell() is used to know the current position of the file pointer.
83

4) fwrite(): 37fwrite() is used to write the content in the file. 5) fread(): 38fread() is used to read the content from the file.

STRUCTURE: 39Structure is a compound data type. Structure is a collection of different data items. Declaration of structure: struct <structure name> { <data type> <variable1>; <data type> <variable1>; <data type> <variable1>; ................ ................. ............... }; 40Struct keyword is used to declare a structure. Eg: Struct exam { int no; char na[20]; float mark; };

85

Program:72 //Example for structure #include<stdio.h> #include<conio.h> struct player { int plno; char plname[20]; float btavg; }; void main() { struct player p; clrscr(); printf("\n Enter player Number:"); scanf("%d",&p.plno); printf("\n Enter player Name:"); scanf("%s",&p.plname); printf("\n Enter Batting Average:"); scanf("%f",&p.btavg); printf("\n Players Details"); printf("\n Player Number :%d",p.plno); printf("\n Player Name :%s",p.plname); printf("\n Batting Average:%f",p.btavg); getch(); }

Program:73

Program:74

//Example for Structure with Array #include<stdio.h> #include<conio.h> struct product { int pno; char *pname[20]; int price; }; void main() { int no,i; struct product p1[4]; clrscr(); for(i=0;i<4;i++) { printf("Enter product Number:"); scanf("%d",&p1[i].pno); printf("Enter product Name:"); scanf("%s",&p1[i].pname); printf("Enter Product price:"); scanf("%d",&p1[i].price); } printf("\nProduct Details"); printf("\nNumber\t Name \t Price\n"); for(i=0;i<4;i++) { printf("\n%d\t%s\t%d\n",p1[i].pno,p1[i].pname,p1[i].price);
87

} getch(); }

Structure Variable: 41To access the structure members from outside it, we have to create a structure variable. Using structure variable, we can access the structure members. Syntax: Struct <Structure name> var1, var2, var3,...; Nested Structure: 42A Structure that is declared within another structure is called nested structure. Lifetime and scope of a variable: 43The lifetime of a variable will starts when the function is called and it destroys when the function is completed. 44The scope of a variable means that where we can able to store the value of a variable. Storage classes: (i) Automatic (auto) (ii) External (global) (iii) Static (iv) Register (i) Automatic:

The memory for the variable will create when the function is called and it destroys when it comes out of function. This type is called automatic variable. In c, all variables, which are defined under a function, is said to be automatic variable. The keyword auto can define it.

(ii) External: 45If the variable is declared outside the main function, then the variable is said to be external variable. The value of external variable can be used in any of the function in the program and any function can change the value of 1. (iii) Static: 46If the variable is declared as static. Its value is persistent that is remains unchanged. The value of static variable is 0 by default. (iv) Register: 47Instead of storing a value. In memory, we can also store it in register. If the variable is stored in register. It is accessed very fastly comparing to the variable present in memory.

Input a Input b A=10 B=5 C=a+b Print c End //Example for structure with function

89

#include<stdio.h> #include<conio.h> struct emp { int eno; float bs; int years; }e1; void main() { void get(); clrscr(); printf("\n Enter employee Number:"); scanf("%d",&e1.eno); printf("\n Enter Basic Salary:"); scanf("%f",&e1.bs); printf("\n Enter Number of years Experiance:"); scanf("%d",&e1.years); get(e1); getch(); } void get(struct emp e2) { float netsal; netsal=e2.bs*e2.years; printf("\n Employee Number=%d",e2.eno); printf("\n Basic Salary =%f",e2.bs); printf("\n Years of Experiance=%d",e2.years); printf("\n Net Salary =%f",netsal); }

Lineary //Eg for Linear Search program #include<conio.h> #include<stdio.h>

void main() { int n,a[100],i,k,t; clrscr(); printf("\nEnter How many number checking:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter the value of %d:",i); scanf("%d",&a[i]); } printf("\nEnter the Key Position:"); scanf("%d",&k); for(i=0;i<n;i++) { if("%d",i+1==k) { printf("\nGiven position is:%d of key value is:%d",i+1,a[i]); t=0; getch(); exit(0); } else { t=1; } } if(t==1) { printf("\nNot found in the list"); } getch(); }

91

Potrebbero piacerti anche