Top Banner
Example 1
259
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: c Language

Example 1

Page 2: c Language
Page 3: c Language

Structure of C Program

Page 4: c Language

Output

Page 5: c Language

Keywords In C

Keyword in C are the reserved words in C which have some fixed job and cannot be used as identifier.

List of Keywords are:

Page 6: c Language

Use of Control strings

Page 7: c Language

Output

Page 8: c Language

Use of Input Function (Scanf)

Page 9: c Language

Output

Page 10: c Language

Output String’s

#include<stdio.h>

void main()

{char ch=66;

char ch1=74,ch2=103,ch3='c',ch4[]="civil";

int f=5.67;

clrscr();

printf("%c,%d,%o,%x,%ld,%ld,%d,\n",ch,72,72,72,32770,32887,32769);

printf("%f,\n",3.24);

printf("%d,\n",f);

printf("%c,\n%c,\n",ch1,ch2);

printf("%d,\n",'\n');

printf("%d,\n",'A');

printf("%c,\n",'B');

printf("%c,\n",ch3);

printf("%s",ch4);

getch();}

Page 11: c Language
Page 12: c Language

//Operators and their precedence.#include<stdio.h>void main(){char ch;int x,z,b=3;float a=1.5;long int j;double d;long double e;clrscr();printf("%d\n",sizeof(ch));printf("%d\n",sizeof(x));printf("%d\n",sizeof(a));printf("%d\n",sizeof(j));printf("%d\n",sizeof(d));printf("%d\n",sizeof(e));printf("%ld\n",&x);printf("%d\n",b/2);printf("%f",a/2);x=3+4-7*8/5%10;printf("%d\n",x);printf("%d\n",+x);printf("%d\n",-x);z=++x + ++x + ++x;printf("%d\n",z);printf("%d\n",++z);printf("%d\n",z++);printf("%d\n",z);a=b/2+b*8/b-b+a/3;printf("%f",a);getch();}

Page 13: c Language

Output

Page 14: c Language

Example1:(Operators)

#include<stdio.h>void main(){int x,y,z;x=1;y=2;z=4;clrscr();z=x+y*z/4%2-1;printf("%d%d%d",x,y,z);getch();}

Page 15: c Language

Output of Example1:

Page 16: c Language

Example 2;(operators)

#include<stdio.h>

void main()

{

int a=10,b=10;

clrscr();

printf("ans=%d",a>b?a*a:b/b);

getch();

}

Page 17: c Language

Example 3(Operators):

#include<stdio.h>void main(){int c=0,d=5,e=10,a;clrscr();a=(c>1?(d>1|| e>1? 100:200):300);printf("a=%d",a);getch();}

Page 18: c Language

Output (Example 3):

Page 19: c Language

Example4(operators)

#include<stdio.h>void main(){int x=3,y=4,z=4;clrscr();printf("ans=%d",(z>=y>=x?100:200));getch();}

Page 20: c Language

Output(Example4):

Page 21: c Language

Example 5:#include<stdio.h>void main(){int x=5;printf("x=%d\n",x++);printf("%d\n",x);printf("x=%d\n",++x);printf("%d\n",x);getch();}

Page 22: c Language

Output Example5:

Page 23: c Language

Operators(Ex:1)

#include<stdio.h>void main(){int x=3,y,z;clrscr();z=y=x;z*=y=x*x;printf("x=%d,y=%d,z=%d",x,y,z);getch();}

Page 24: c Language

Output

Page 25: c Language

Operators(Ex:2)

#include<stdio.h>

void main()

{

int a=30,b=40,x;

clrscr();

x=(a!=10) && (b=50);

printf("x=%d",x);

getch();

}

Page 26: c Language

Output

Page 27: c Language

Operators(Ex3)

#include<stdio.h>void main(){int x=10,y=x,z=x,t;clrscr();y-=x;z=-x;t=-x;printf("y=%d,z=%d,t=%d",y,z,t);getch();}

Page 28: c Language

Output

Page 29: c Language

Operators

#include<stdio.h>void main(){int x,y,z;clrscr();x=y=z=1;z=++x || ++y && ++z;printf("x=%d,y=%d,z=%d",x,y,z);getch();}

Page 30: c Language

Output

Page 31: c Language

Control Statements

• Decision Control Statements -The If Statement

-The If - Else Statement

-The switch Statement

• Looping Control Statements

• Breaking Control Statements

• Jump Statements

Page 32: c Language

The If Statement

Syntax:

If(condition)

{

statement 1;

statement 2;

}

Page 33: c Language

The if-else statement

Syntax:if(condition) // no semicolon{statement 1;statement 2; }else{statement 3;statement 4;}

Page 34: c Language

Example 1:

#include<stdio.h>void main(){int m,n;clrscr();scanf("%d%d",&m,&n);if (m-n==0)printf("two numbers are equal");elseprintf("xyz");getch();}

Page 35: c Language

Output

Page 36: c Language

Example 2:

#include<stdio.h>void main(){int a=300,b=10,c=20;clrscr();if (!(a>=400)){b=300;c=200;printf("b=%d c=%d",b,c);}elseprintf("civil");getch();}

Page 37: c Language

Output:

Page 38: c Language

Example 3:#include<stdio.h>void main(){int a=10,b=100%90;clrscr();if (a!=b)printf("a=%d b=%d",a,b);elseprintf("thapar");getch();}

Page 39: c Language

Output

Page 40: c Language

Nested if statementsSyntax:if(condition 1){ if(condition 2) statement 1; else statement 2;}else statement 3;

Page 41: c Language

Example 1:

#include<stdio.h>void main(){int a,b;clrscr();scanf("%d%d",&a,&b);if(a<=b){if(a<b)printf("%d < %d",a,b);elseprintf("%d == %d",a,b);}elseprintf("%d > %d",a,b);getch();}

Page 42: c Language

Output:

Page 43: c Language

Dangling Else Problem

if(condition 1)

if(condition 2)

statement 1;

else

statement 2;

// This problem is created when there is no matching else for every if. In C to overcome this problem else is always paired with most recent unpaired if.

Page 44: c Language

Multiway selection:switch statement Syntax:switch(variable or expression){case constant A:Statement 1;break;case constant B:Statement 2;break;……default:Statement;}

Page 45: c Language

Example 1:#include<stdio.h>void main(){int a;clrscr();scanf("%d",&a);switch (a){case 1:printf("........");break;case 2:printf("********");break;default:printf("invalid option");}getch();}

Page 46: c Language

Output

Page 47: c Language

Example 2:#include<stdio.h>void main(){int k=-2,j=4;clrscr();switch (k/=j/k){default:printf("all are same\n");case 0:printf("abc\n");case 1:printf("xyz\n");case 2:printf("civil");}getch();}

Page 48: c Language

Output

Page 49: c Language

Looping Control Statements(Used for Iterations)

• for loop.

Nested for loops(for with in for)

• while loop (Entry Controlled Loop).

• do-while loop (Exit Controlled Loop).

Page 50: c Language

Example 1:#include<stdio.h>void main(){int i;clrscr();for(i=0;i<3;i++){printf("%d\n",i);}getch();}

Page 51: c Language

Output 1:

Page 52: c Language

Example 2:#include<stdio.h>void main(){int i;clrscr();for(i=0;i<=4;i++){printf("%d\n",i);}getch();}

Page 53: c Language

Output 2:

Page 54: c Language

Example 3:#include<stdio.h>void main(){int i;clrscr();for(i=0;i<=3;i++){printf("civil\n");printf("%d\n",i);}getch();}

Page 55: c Language

Output 3:

Page 56: c Language

Example 4:#include<stdio.h>void main(){int j,x=0;clrscr();for(j=0;j<=5;j++){switch(j-1){case 0:case -1:x+=1;break;case 1:case 2: case 3:x+=2;break;default:x+=3;}printf("%d\n",x);}getch();}

Page 57: c Language

Output:

Page 58: c Language

Example 1:(Nested for)

#include<stdio.h>void main(){int i,j;clrscr();for(i=1;i<=3;i++){for(j=1;j<=2;j++){printf("i=%d,j=%d\n",i,j);printf("i*j=%d\n",i*j);}}getch();}

Page 59: c Language

Output:

Page 60: c Language

Example 2:#include<stdio.h>void main(){int a,b,sub;clrscr();for(a=3;a>=1;a--){for(b=1;b<=2;b++){sub=a-b;printf("a=%d,b=%d,sub=%d\n",a,b,sub);}}getch();}

Page 61: c Language

Output:

Page 62: c Language

Example 3:(with middle loop)#include<stdio.h>

void main(){int a,b,c;

clrscr();

for(a=1;a<=2;a++)

{

for(b=1;b<=2;b++)

{

for(c=1;c<=2;c++){

printf("a=%d,b=%d,c=%d,a+b+c=%d\n",a,b,c,a+b+c);

}}}

getch();}

Page 63: c Language

Output:

Page 64: c Language

Example 4:

#include<stdio.h>void main(){int a;clrscr();for(a=1;a<=32767;a++){printf("%d",a);}getch();}

Page 65: c Language

Example 5:#include<stdio.h>void main(){int x=1;clrscr();switch(x){printf("hello\n");case 1:printf("xyz\n");break;default:printf("abc\n");}getch();}

Page 66: c Language

Output:

Page 67: c Language

Break Statement:Used to take the control out of the body of current loop.

#include<stdio.h>

void main(){

int i;clrscr();

for(i=1;i<=5;i++){

if(i==3)

break;

else

printf("%d",i);}

printf("\nxyz");

getch();}

Page 68: c Language

Output:

Page 69: c Language

Continue Statement:Doesn’t take the control out of the body of current loop but skips the statements following continue statement and takes the control

back to the next iteration of loop. #include<stdio.h> /*Program for continue*/void main(){int i;clrscr();for(i=1;i<=10;i++){if(i==4)continue;elseprintf("%d\n",i);}printf("\nabd");getch();}

Page 70: c Language

Output:

Page 71: c Language

Use of both break and continue#include<stdio.h>void main(){int i;clrscr();for(i=-1;i<=10;i++){if(i<=5)continue;elsebreak;printf("civil");}printf("\nxyz");getch();}

Page 72: c Language

Output:

Page 73: c Language

The while LOOP

Syntax:

while(test condition)

{

body of while loop

}

The test condition may be any expression.The loop statements will be executed till the condition is true.

Page 74: c Language

Example 1:

#include<stdio.h>void main(){int i=1;clrscr();while(i<=5){printf("%d",i);i++;}printf("\nabc");getch();}

Page 75: c Language

Output:

Page 76: c Language

Example2:#include<stdio.h>void main(){int c=1,d=0;clrscr();while(d<=9){printf("\n%d %d",++d,++c);}printf(“xyz”);getch();}

Page 77: c Language

Output:

Page 78: c Language

Example 3:#include<stdio.h>void main(){int i=5;clrscr();while(i-->=0)printf("%d",i);i=5;printf("\n");while(i-->=0)printf("%d",i);printf("\n");while(i-->=0)printf("%d",i);getch();}

Page 79: c Language

Output:

Page 80: c Language

The do-while LOOP(Exit Controlled Loop)

Syntax:

do

{

Body of loop

}while(test condition);

Regardless of the test condition body of loop will executed at least once.

true

Page 81: c Language

Example 3#include<stdio.h>void main(){int i=5;clrscr();do{printf("%d",i);i--;}while(i>=0);getch();}

Page 82: c Language

Output:

Page 83: c Language

Example 4:#include<stdio.h>void main(){int i=5;clrscr();do{printf("%d",i);i--;}while(i<=0);getch();}

Page 84: c Language

Output:

Page 85: c Language

goto statement

Syntax:

goto label;-----label: This statement does not contain any

condition.This statement passes control anywhere in the program where the label is used.

label name must start with any character.

Page 86: c Language

goto statement example:#include<stdio.h>void main(){int a;clrscr();printf("Enter a number");scanf("%d",&a);if(a%2==0)goto even;elsegoto odd;even:printf("%d is a even number",a);exit(0);odd:printf("%d is a odd number",a);getch();}

Page 87: c Language

Output goto:

Page 88: c Language

goto statement#include<stdio.h>void main(){int i,j;clrscr();for(j=1;j<=10;j++){for(i=1;i<=10;i++){if(j<10)goto xyz;}printf("civil\n");printf("abc\n");}xyz:printf("the null chracter discards \0 the rest of line\n");printf("\nthis is \"cse\" in double quotes\n");printf("this is \\ escape character itself\n");printf("this line disappears.\r...reposition at the beginning\

n");getch();}

Page 89: c Language

Output:

Page 90: c Language

Problems:Control Structures1.Write a program to find factorial of a

number using do-while.

2.Write a program to display numbers from 1 to 16.Use incrementation operation in body of for loop more than once.

3.Write a program to print the entered number in reverse order using do-while loop.Also perform sum and multiplication with their digits.

Page 91: c Language

ArraysConsider the following example: #include<stdio.h>

void main(){

int a=2;

a=4;clrscr();

printf("%d",a);

getch();} OUTPUT:4

i.e ordinary variables are always capable of storing one value at a time(recent value).

Array variables are able to store more than one value at a time.

Page 92: c Language

Declaration of an arrayDeclaration of an array is done as follows: Array is used to hold the values of similar data

type.i.e integer type of array holds only integer type of values,float type of array is used to hold floating type of values.

int a[5]; It tells the compiler that ‘a’ is an integer type of array and can store 5 integers.

In the same way different data types can be represented as array:For Ex:

char ch[10];//nothing but creation of 10 variables of //char type in the memory.Instead of

declaring 10 variables for 10 values,the //programmer can define them in an array.

float x[4];long num[7];

Page 93: c Language

Array Initialization#include<stdio.h>void main(){int i[5]={1,2,5,6,7}; //Array Intialization clrscr();printf("1st element of array=%d\n",i[0]);printf("2nd element of array=%d\n",i[1]);printf("3rd element of array=%d\n",i[2]);printf("4th element of array=%d\n",i[3]);printf("5th element of array=%d\n",i[4]);getch();}

Page 94: c Language

Output:

Page 95: c Language

Example 1:#include<stdio.h>void main(){int i[5]={1,2};clrscr();printf("%d\n",i[0]);printf("%d\n",i[1]);printf("%d\n",i[2]);printf("%d\n",i[3]);printf("%d\n",i[4]);getch();}

Page 96: c Language

Output:

Page 97: c Language

Example 2:#include<stdio.h>void main(){float i[4]={12.4,2.5};float a[]={12.4,2.5,3.3}; ///clrscr();printf("%d\n",sizeof(i));printf("%d\n",sizeof(a));getch();} ///if the array is initialized where it is declared

mentioning the dimension of array is optional,as in above example.

Page 98: c Language

Output:

Page 99: c Language

Example 3:#include<stdio.h>

void main(){

float i[]={12.4,2.5,4.5,5.5};

clrscr();

printf("%d\n",sizeof(i)/sizeof(i[0]));

printf("%f\n",i[1]);

printf("%f\n",i[3]);

getch();

}

Page 100: c Language

Output:

Page 101: c Language

Example (Starting address=65516)

#include<stdio.h>

void main(){

int a[5]={5,20,36,47,60};

clrscr();

printf("%u\n%u\n%u\n%d\n%u\n%u\n%d",a,&a,&a[0],a[0],&a[1],&a[4],a[4]);

getch();

}

Page 102: c Language

Output:

Page 103: c Language

Inputting values from keyboard in array

#include<stdio.h>void main(){int arr[5],i;clrscr();for(i=0;i<5;i++){scanf("%d",&arr[i]);printf("the element is=%d",arr[i]);}getch();}

Page 104: c Language

Output

Page 105: c Language

Swap the elements of an array#include<stdio.h>void main(){int arr[2]={10,20},temp;clrscr();printf("%d,%d",arr[0],arr[1]);temp=arr[0];arr[0]=arr[1];arr[1]=temp;printf("\n%d,%d",arr[0],arr[1]);getch();}

Page 106: c Language

Output

Page 107: c Language

Example 4:#include<stdio.h>void main(){int a[5]={5,10,15,20,25},i,j,m,k;clrscr();i=++a[1];j=a[1]++;printf("i=%d\nj=%d\na[1]=%d\n",i,j,a[1]);i=1;m=a[i++];printf("i=%d\nm=%d\n",i,m);i=2;k=a[++i];printf("i=%d\nk=%d\n",i,k);getch();}

Page 108: c Language

Output

Page 109: c Language

Static array#include<stdio.h>void main(){int arr[6];static int b[4];int i;clrscr();for(i=0;i<6;i++){printf("%d\n",arr[i]);}printf("\n");for(i=0;i<4;i++){printf("%d\n",b[i]);}getch();}

Page 110: c Language

Output

Page 111: c Language

Example 5:(Assign Values)

#include<stdio.h>void main(){int a[5],i;for(i=0;i<5;i++){a[i]=i*2;printf("%d\n",a[i]);}getch();}

Page 112: c Language

Output

Page 113: c Language

2-D Arrays#include<stdio.h>void main(){int a[3][3]={2,4,3, 6,8,5, 3,5,1},i,j;clrscr();for(i=0;i<3;i++){for(j=0;j<3;j++)printf("%d",a[i][j]); to print in matrix form line

is left as in next caseprintf(“\n”);}getch();}

Page 114: c Language

Output

Page 115: c Language

2-D Arrays(Continued)#include<stdio.h>void main(){int a[3][3]={2,4,3, 6,8,5, 3,5,1};clrscr();printf("%u\n%u\n%u",a,a[2],a[2][2]);getch();}

Page 116: c Language

Output

Page 117: c Language

FunctionsA function is self contained block of code that

performs task of some kind.

Ex: Sum(int x,int y) is used to find sum of two integers.

Page 118: c Language

Example 1:#include<stdio.h>void main(){int x,y,z;x=3;y=4;z=sum(x,y); //Calling Functionprintf("%d",z);getch();}int sum(int a,int b) //Function definition(Called Fn){int c;c=a+b;return c;}

Page 119: c Language

Output

Page 120: c Language

Some facts about Functions:1.Functions can be either library functions or

user defined.

2.There can be any number of the functions in the program.

3.Program execution always begin with main().

4.Communication b/w the functions is called a message passing.

Page 121: c Language

Example 2:#include<stdio.h>float areacircle(float); //Function Prototypevoid main(){int area;float radius=2.0;clrscr();area=areacircle(radius);printf("%d",area);getch();}float areacircle(float r){float a;a=3.14*r*r;printf("%f\n",a);return a;}

Page 122: c Language

Output

Page 123: c Language

Message Passing

• Call By value

• Call By address

In Call by value we pass the value from calling to called function.

In Call By address we pass the address from calling to called function.

In Call by address we make use of Pointers.

Page 124: c Language

PointersPointer is used to hold the address of any

other variable.The pointer is denoted by (*)asterisk symbol.

It has two parts one is called as declaration part and other is reference part.

For Ex: int *x,i; //declaration part

This statement tells the compiler that it holds the address of any integer variable.

Similarly float *y;

x=&i; //reference part

Page 125: c Language

Pointers(* gives value at address)#include<stdio.h>void main(){int i=30;int *j,**k; //declaration partclrscr();j=&i; //dereferencing partk=&j;printf("%u\n",&i);printf("%u\n",j);printf("%u\n",*&i); // i=30 j=65524 k=65522 printf("%d\n",*j); //&i=65524 &j=65522 printf("%u\n",k);printf("%u\n",*k);printf("%d",**k);getch();}

Page 126: c Language

Output

Page 127: c Language

Pointers continued…#include<stdio.h>void main(){int i=30;int ***r,**q,*p;clrscr();p=&i;q=&p;r=&q;printf("%d\n",*p);printf("%d\n",**q);printf("%d",***r);getch();}

Page 128: c Language

Output

Page 129: c Language

Functions continued….#include<stdio.h>void main(){int i=3,k,l;clrscr();k=add(++i);l=add(i++);printf("%d\n%d\n%d",i,k,l);getch();}int add(int x){++x;return x;}

Page 130: c Language

Output

Page 131: c Language

Functions Ex2:#include<stdio.h>void main(){int k=35,z;clrscr();k=func1(k=func1(k=func1(k)));printf("k=%d",k);getch();}int func1(int x){++x;return x;}

Page 132: c Language

Output

Page 133: c Language

Types of functions1.(without arguments and without return values)

#include<stdio.h>void message();void main(){clrscr();message();getch();}void message(){printf("civil");}

Page 134: c Language

Output

Page 135: c Language

2.With arguments but without return values

#include<stdio.h>void sqr();void main(){int j;clrscr();for(j=1;j<5;j++)sqr(j);getch();}void sqr(int k){printf("%d\n",k*k);}

Page 136: c Language

Output

Page 137: c Language

3.With arguments and return values#include<stdio.h>int sqr();void main(){int j,i;clrscr();for(j=1;j<4;j++){i=sqr(j);printf("square=%d\n",i);}getch();}sqr(int k){return (k*k);}

Page 138: c Language

Output

Page 139: c Language

4.Without Arguments but with return values

#include<stdio.h>int sum();void main(){int j,i;clrscr();i=sum();printf("sum=%d",i);getch();}sum(){int x,y,z;scanf("%d%d%d",&x,&y,&z);return (x+y+z);}

Page 140: c Language

Output

Page 141: c Language

Use of Operator with function#include<stdio.h>#include<math.h>int y(),sqr(int),cube(int);void main(){int x,i;clrscr();scanf("%d",&x);i=x>y()? sqr(x):cube(x);printf("%d",i);getch();}y(){return (10);}sqr(int k){return (pow(k,2));}cube(int k){return (pow(k,3));}

Page 142: c Language

Output

Page 143: c Language

Example:2#include<stdio.h>int addsub(int,int);void main(){int i=10,j=20,k;clrscr();k=addsub(i,j);printf("%d",k);getch();}addsub(int c,int d){int x,y;x=c-d;y=c+d;return(x);return(y);}

Page 144: c Language

Output

Page 145: c Language

Example 3:#include<stdio.h>int addsub(int,int);void main(){int i=10,j=20,k;clrscr();k=addsub(i,j);printf("%d",k);getch();}addsub(int c,int d){int x,y;x=c-d;y=c+d;return(x,y);}

Page 146: c Language

Output

Page 147: c Language

Call by value

#include<stdio.h>void main(){

int a=10,b=20;clrscr();

swap(a,b);

printf("%d%d",a,b);

getch();}

int swap(int x,int y)

{int t;

t=x;

x=y;

y=t;

printf("%d%d\n",x,y);}

Page 148: c Language

Output

Page 149: c Language

Call By Reference#include<stdio.h>void main(){int a=10,b=20;clrscr();swap(&a,&b);printf("%d%d",a,b);getch();}int swap(int *x,int *y){int t;t=*x; //t=a;*x=*y; //a=b*y=t; //b=tprintf("%d%d\n",*x,*y);}

Page 150: c Language

Output

Page 151: c Language

Functions with Arithmetic Eqns#include<stdio.h>#include<math.h>int a(),b(),sqr(int);void main(){int s=0,k;clrscr();s=sqr(a()+b());printf("\n Square of sum=%d",s);getch();}a(){int a;printf("Enter the value of a");scanf("%d",&a);return a;}b(){int b;printf("Enter the value of b");scanf("%d",&b);return b;}sqr(int x){return (pow(x,2));}

Page 152: c Language

Output

Page 153: c Language

Call to Function through if #include<stdio.h>int a();void main(){clrscr();if(a()%2==0)printf("\n Even Number");elseprintf("\n Odd Number");getch();}a(){int a;printf("Enter the value of a");scanf("%d",&a);return a;}

Page 154: c Language

Output

Page 155: c Language

Example#include<stdio.h>int addmul(int,int);void main(){int i=3,j=4,k,l;clrscr();k=addmul(i,j);l=addmul(i,j);printf("%d,%d",k,l);getch();}int addmul(int a,int b){int x,y;x=a + b;y=a * b;return(x,y);}

Page 156: c Language

Output

Page 157: c Language

Address of the Function.#include<stdio.h>void show();void main(){clrscr();show();printf("%u",show);getch();}void show(){printf("\naddress of the function show() is :");}

Page 158: c Language

Output

Page 159: c Language

Example#include<stdio.h>void fun(int *,int *);void main(){int i=5,j=2;clrscr();fun(&i,&j);printf("%d,%d",i,j);getch();}void fun(int *a,int *b){*a=*a * *a;*b=*b * *b;}

Page 160: c Language

Output

Page 161: c Language

Recursive Functions#include<stdio.h>int fact(int);void main(){int k=5,l;clrscr();l=fact(k);printf("%d",l); //stack is used.(LIFO)getch();} //data structure.is the particularint fact(int n) //way of storing data in computer.//{int f;if (n==0)return 1;elsef=n*fact(n-1);return f;}

Page 162: c Language

Output

Page 163: c Language

Example(Diff B/w call by value and Reference)

#include<stdio.h>int xyz(int,int*);void main(){int i=-5,j=-2;clrscr();xyz(i,&j);printf("%d,%d",i,j);getch();}int xyz(int x,int *y){x=x * x;*y=*y * *y;}

Page 164: c Language

Output

Page 165: c Language

Arrays and Functions#include<stdio.h>int d(int);void main(){int i,k;int m[5]={55,65,75,85,95};clrscr();for(i=0;i<5;i++){k=d(m[i]);printf("%d,",k);}getch();}int d(int n){return n;}

Page 166: c Language

Output

Page 167: c Language

Arrays and Functions(reference) #include<stdio.h>int s(int*);void main(){int i,k;int m[5]={55,65,75,85,95};clrscr();for(i=0;i<5;i++){k=s(&m[i]);printf("%d,",k);}getch();}int s(int *n){return *n;}

Page 168: c Language

Output

Page 169: c Language

Pointers and Arrays#include<stdio.h>void main(){int i,*j;int m[5]={55,65,75,85,95};clrscr();j=&m[0];for(i=0;i<5;i++){printf("%u\t",&m[i]);printf("%d\n",*j);j++;} /*pointer when incremented points to

immediate next location of array element*/getch();}

Page 170: c Language

Output

Page 171: c Language

Strings

#include<stdio.h>void main(){char name1[7]={'s','a','n','j','a','y'};char name2[6]={"civil"};char name3[6]={"civ\0il"};clrscr();printf("%s\n",name1);printf("%s\n",name2);printf("%s",name3);getch();}

Page 172: c Language

Output

Page 173: c Language

Strings with different formats

#include<stdio.h>void main(){char text[15]={"Computers"};clrscr();printf("%s\n",text);printf("%.5s\n",text);printf("%.8s\n",text);printf("%11s\n",text);getch();}

Page 174: c Language

Output

Page 175: c Language

While loop to print array of Characters

#include<stdio.h>

void main(){

char text[]={"CIVIL ENGG"};

int i=0;

clrscr();

while(i<=10)

{printf("%c",text[i]);

i++;}

getch();

}

Page 176: c Language

Output

Page 177: c Language

While by taking help of ‘\0’character

#include<stdio.h>void main(){char text[]={"CIVIL ENGG"};int i=0;clrscr();while(text[i]!=‘\0’){printf("%c",text[i]);i++;}getch();}

Page 178: c Language

Output

Page 179: c Language

String I/P, O/P#include<stdio.h>void main(){char month[9];char str[10];clrscr();printf("enter the month\n");scanf("%s",&month);scanf("%s",&str);printf("%s\n",month);printf("%s",str);getch();}

Page 180: c Language

Output

Page 181: c Language

gets,puts and length of string

#include<stdio.h>#include<string.h>void main(){char text[20];int len;clrscr();printf("type text below.\n");gets(text);len=strlen(text);printf("length of string=%d\n",len);puts(text);getch();}

Page 182: c Language

Output

Page 183: c Language

strcpy(s2,s1) s1 is copied to s2.#include<stdio.h>#include<string.h>void main(){char s1[9];char s2[10];clrscr();printf("enter string s1\n");gets(s1);printf("enter string s2\n");gets(s2);strcpy(s2,s1);puts(s2);getch();}

Page 184: c Language

Output

Page 185: c Language

strncpy(s2,s1,n)copies specified length of characters

#include<stdio.h>#include<string.h>void main(){char s1[9];char s2[10];int n;clrscr();printf("enter string s1\n");gets(s1);printf("enter string s2\n");gets(s2);scanf("%d",&n);strncpy(s2,s1,n);puts(s2);getch();}

Page 186: c Language

Output

Page 187: c Language

strcmp(s1,s2)#include<stdio.h>#include<string.h>void main(){char s1[9];char s2[10];int diff;clrscr();printf("enter string s1\n");gets(s1);printf("enter string s2\n");gets(s2);diff=strcmp(s1,s2);if(diff==0)printf("s1 and s2 are identical");elseprintf("s1 and s2 are not identical");getch();}

Page 188: c Language

Output

Page 189: c Language

strncmp(s1,s2,n)compares upto first n characters

#include<stdio.h>#include<string.h>void main(){char s1[9];char s2[10];int diff,n;clrscr();printf("enter string s1\n");gets(s1);printf("enter string s2\n");gets(s2);scanf("%d",&n);diff=strncmp(s1,s2,n);if(diff==0)printf("s1 and s2 are identical");elseprintf("s1 and s2 are not identical");getch();}

Page 190: c Language

Output

Page 191: c Language

strlwr(upper)convert to lower case

#include<stdio.h>#include<string.h>void main(){char s1[9];clrscr();printf("enter string s1\n");gets(s1);printf("%s",strlwr(s1));getch();}

Page 192: c Language

Output

Page 193: c Language

strupr#include<stdio.h>#include<string.h>void main(){char s1[9];clrscr();printf("enter string s1\n");gets(s1);printf("%s",strupr(s1));getch();}

Page 194: c Language

Output

Page 195: c Language

Strcat(to concatenate s1 and s2) #include<stdio.h>#include<string.h>void main(){char s1[20];char s2[10];clrscr();printf("enter string s1\n");gets(s1);printf("enter string s2\n");gets(s2);strcat(s1,s2);puts(s1);getch();}

Page 196: c Language

Output

Page 197: c Language

Strncat(to append n chars of s2 in s1 )

#include<stdio.h>#include<string.h>void main(){char s1[20];char s2[10];int n;clrscr();printf("enter string s1\n");gets(s1);printf("enter string s2\n");gets(s2);scanf("%d",&n);strncat(s1,s2,n);puts(s1);getch();}

Page 198: c Language

Output

Page 199: c Language

Strrev(s1)reverse the string

#include<stdio.h>#include<string.h>void main(){char s1[20];clrscr();printf("enter string s1\n");gets(s1);strrev(s1);puts(s1);getch();}

Page 200: c Language

Output

Page 201: c Language

Strset(s1,symbol)#include<stdio.h>#include<string.h>void main(){char s1[20];char c;clrscr();printf("enter string s1\n");gets(s1);printf("enter symbol");scanf("\n%c",&c);strset(s1,c);puts(s1);getch();}

Page 202: c Language

Output

Page 203: c Language

Strnset(s1,symbol,n)#include<stdio.h>#include<string.h>void main(){char s1[20],c;int n;clrscr();printf("enter string s1\n");gets(s1);printf("enter symbol");scanf("\n%c",&c);scanf("%d",&n);strnset(s1,c,n);puts(s1);getch();}

Page 204: c Language

Output

Page 205: c Language

Strspn(return the position where source array does not match with destination

#include<stdio.h>#include<string.h>void main(){char s1[20];char s2[10];int n;clrscr();printf("enter string s1\n");gets(s1);printf("enter string s2\n");gets(s2);n=strspn(s1,s2);printf("%d",n);getch();}

Page 206: c Language

Output

Page 207: c Language

Copy string w/o using strcpy();#include<stdio.h>#include<string.h>void main(){char ori[20], dup[20];int i;clrscr();printf("enter string ori\n");gets(ori);for(i=0;ori[i]!='\0';i++)dup[i]=ori[i];dup[i]='\0';puts(dup);getch();}

Page 208: c Language

Output

Page 209: c Language

String is palindrome or not#include<stdio.h>#include<string.h>void main(){char str[10];int i=0,j,k;clrscr();printf("enter string str\n");gets(str);j=strlen(str)-1;for(i=0;i<=j;i++){if(str[i]==str[j])k=1;else {k=0;break;}j--;}if (k==1)printf("word is palindrome");else printf("word is not palindrome");getch();}

Page 210: c Language

Output

Page 211: c Language

String elements can also be accessed using Pointers

#include <stdio.h>void main (){char *ptr="civil";clrscr();while(*ptr!='\0'){printf("%c",*ptr);ptr++;}getch();}

Page 212: c Language

Output

Page 213: c Language

Structures(used to hold dissimilar data type under single name)

#include<stdio.h>void main(){struct book1{char book[30];int pages;float price;};struct book1 bk1={"cprograms",345,123};clrscr();printf("%s\n",bk1.book);printf("%d\n",bk1.pages);printf("%f",bk1.price);getch();}

Page 214: c Language

Output

Page 215: c Language

Size of structure type variable#include<stdio.h>void main(){struct book1{char book[30];int pages;float price;};struct book1 bk1={"cprograms",345,123};clrscr();printf("%d bytes\n",sizeof(bk1.book));printf("%d bytes\n",sizeof(bk1.pages));printf("%d bytes\n",sizeof(bk1.price));printf("%d bytes\n",sizeof(bk1));getch();}

Page 216: c Language

Output

Page 217: c Language

More about structures#include<stdio.h>void main(){struct player{char name[30];int age;}p1={"xyz",23};clrscr();printf("%s\n",p1.name);printf("%d\n",p1.age);getch();}

Page 218: c Language

Output

Page 219: c Language

Assignment of one structure variable into another

#include<stdio.h>void main(){struct player{char name[30];int age;}p1={"xyz",23};struct player p2;clrscr();p2=p1;printf("%s\n",p2.name);printf("%d\n",p2.age);getch();}

Page 220: c Language

Output

Page 221: c Language

Array of Structures#include<stdio.h>void main(){struct employee{int eno;char name[20];};struct employee a[3];int i;clrscr();for(i=0;i<3;i++){printf("Enter empno and name of employee");scanf("%d%s",&a[i].eno,&a[i].name);}for(i=0;i<3;i++)printf("Empno=%d,name=%s\n",a[i].eno,a[i].name);getch();}

Page 222: c Language

Output

Page 223: c Language

Pointer to structure#include<stdio.h>void main(){struct book{char name[20];char author[25];int pages;};struct book b1={"Cprograms","kanetkar",589};struct book *ptr;clrscr();ptr=&b1;printf("%s %s %d\n",b1.name,b1.author,b1.pages);printf("%s %s %d",ptr->name,ptr->author,ptr->pages);getch();}

Page 224: c Language

Output

Page 225: c Language

Structure and function(passing entire structure)#include<stdio.h>struct xyz{char name[20];int age;};void main(){struct xyz b1={"abc",23};clrscr();show(b1);getch();}show(struct xyz b2){printf("%s %d",b2.name,b2.age);return 0;}

Page 226: c Language

Output

Page 227: c Language

Structure and functions(passing reference)#include<stdio.h>struct book{char name[20];char author[25];int pages;};void main(){struct book b1={"Cprograms","kanetkar",589};clrscr();show(&b1);getch();}show(struct book *b2){printf("%s %s %d",b2->name,b2->author,b2->pages);return 0;}

Page 228: c Language

Output

Page 229: c Language

Passing structure elements to functions#include<stdio.h>struct xyz{char name[20];int age;};void main(){struct xyz b1={"abc",23};clrscr();show(&b1.name,b1.age);getch();}show(char *s,int z){printf("%s %d",s,z);return 0;}

Page 230: c Language

Output

Page 231: c Language

Structure within structure#include<stdio.h>void main(){struct part{char type[20];int qty;};struct vehicle{char xyz[20];struct part p1;};struct vehicle v1;v1.p1.qty=300;printf(“qty=%d",v1.p1.qty);getch();}

Page 232: c Language

Output

Page 233: c Language

Memory Organization of structure elements#include<stdio.h>#include<string.h>void main(){struct xyz{int num;float f;char branch[10];}a;clrscr();a.num=1; //assume starting address =65510a.f=3.14;strcpy(a.branch,"civil");printf("%u %u %u\n",&a.num,&a.f,&a.branch);printf("%d %f %s",a.num,a.f,a.branch);getch();}

Page 234: c Language

Output

Page 235: c Language

Memory w.r.t array of structures /*starting address of 1st struct is=65514*/#include<stdio.h>void main(){struct a{int num;float f;}v[2];clrscr();printf("%u %u\n",&v[0].num,&v[0].f);printf("%u %u",&v[1].num,&v[1].f);getch();}

Page 236: c Language

Output

Page 237: c Language

[Union(same portion of memory is accessed by member elements]

#include<stdio.h>void main(){struct a{int num;float f; }v;union b{float f;int num;}s;clrscr();printf("%d bytes\n",sizeof(v));printf("%d bytes",sizeof(s));getch();}

Page 238: c Language

Output

Page 239: c Language

Access elements of union

#include<stdio.h>void main(){union b{float f;int num;}s;clrscr();scanf("%f %d",&s.f,&s.num);printf("%f %d",s.f,s.num);getch();}

Page 240: c Language

Output

Page 241: c Language

Pointers and arrays#include<stdio.h>void main(){int num[4]={10,25,45,60},i;clrscr();printf("address element \n");for(i=0;i<4;i++){printf("%u\t",num+i);printf("num[%d]=%d\n",i,*(num+i));}getch();}

Page 242: c Language

Output

Page 243: c Language

Arrays and pointers#include<stdio.h>void main(){int arr[5]={10,20,30,40,50},p;clrscr();for(p=0;p<5;p++){printf("value of arr[%d]=",p);printf("%d |",arr[p]);printf("%d |",*(arr+p));printf("%d |",*(p+arr));printf("%d |",p[arr]);printf("address of arr[%d]=%u\n",p,(arr+p));}getch();}

Page 244: c Language

Output

Page 245: c Language

2-d array#include<stdio.h>void main(){int mat[3][3],i,j;clrscr();for(i=0;i<3;i++){for(j=0;j<3;j++)scanf("%d",&mat[i][j]);}for(i=0;i<3;i++){for(j=0;j<3;j++)printf("%d",mat[j][i]);printf("\n");}getch();}

Page 246: c Language

Output

Page 247: c Language

Storage classes1. It tells the compiler about scope of the variable.

2. The initial value of the variable if already not initialized.

3. Life of the variable i.e how long the variable would be active in the program.

4. Storage area of variables.

C can have four storage classes

1.Automatic variables.

2.External variables.

3.static variables.

4.Register variables.

Page 248: c Language

Automatic variables#include<stdio.h>void main(){int x=10;clrscr();printf("x=%d",x);{int x=20;printf("\nx=%d",x);}printf("\nx=%d",x);getch();}

Page 249: c Language

Output

Page 250: c Language

External Variables#include<stdio.h>int x=10;void main(){int x;clrscr();printf("x=%d",x);getch();}

Page 251: c Language

Output

Page 252: c Language

Continued..

#include<stdio.h>int x=10;void main(){extern int x;clrscr();printf("x=%d",x);getch();}

Page 253: c Language

Output

Page 254: c Language

Static Variables

#include<stdio.h>void main(){int x=10;static int y;clrscr();printf("x=%d y=%d",x,y);getch();}

Page 255: c Language

Output

Page 256: c Language

Register Variables

#include<stdio.h>

void main()

{

register int y=10;

clrscr();

printf("y=%d",y);

getch();

}

Page 257: c Language

output

Page 258: c Language

Size of pointer variables#include<stdio.h>void main(){int *p;char *c;float *f;clrscr();printf("%d\n",sizeof(p));printf("%d\n",sizeof(c));printf("%d\n",sizeof(f));printf("%u\n",&p);printf("%u\n",&c);printf("%u\n",&f);getch();}

Page 259: c Language

Output