Example 1
Nov 29, 2014
Example 1
Structure of C Program
Output
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:
Use of Control strings
Output
Use of Input Function (Scanf)
Output
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();}
//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();}
Output
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();}
Output of Example1:
Example 2;(operators)
#include<stdio.h>
void main()
{
int a=10,b=10;
clrscr();
printf("ans=%d",a>b?a*a:b/b);
getch();
}
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();}
Output (Example 3):
Example4(operators)
#include<stdio.h>void main(){int x=3,y=4,z=4;clrscr();printf("ans=%d",(z>=y>=x?100:200));getch();}
Output(Example4):
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();}
Output Example5:
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();}
Output
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();
}
Output
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();}
Output
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();}
Output
Control Statements
• Decision Control Statements -The If Statement
-The If - Else Statement
-The switch Statement
• Looping Control Statements
• Breaking Control Statements
• Jump Statements
The If Statement
Syntax:
If(condition)
{
statement 1;
statement 2;
}
The if-else statement
Syntax:if(condition) // no semicolon{statement 1;statement 2; }else{statement 3;statement 4;}
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();}
Output
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();}
Output:
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();}
Output
Nested if statementsSyntax:if(condition 1){ if(condition 2) statement 1; else statement 2;}else statement 3;
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();}
Output:
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.
Multiway selection:switch statement Syntax:switch(variable or expression){case constant A:Statement 1;break;case constant B:Statement 2;break;……default:Statement;}
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();}
Output
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();}
Output
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).
Example 1:#include<stdio.h>void main(){int i;clrscr();for(i=0;i<3;i++){printf("%d\n",i);}getch();}
Output 1:
Example 2:#include<stdio.h>void main(){int i;clrscr();for(i=0;i<=4;i++){printf("%d\n",i);}getch();}
Output 2:
Example 3:#include<stdio.h>void main(){int i;clrscr();for(i=0;i<=3;i++){printf("civil\n");printf("%d\n",i);}getch();}
Output 3:
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();}
Output:
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();}
Output:
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();}
Output:
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();}
Output:
Example 4:
#include<stdio.h>void main(){int a;clrscr();for(a=1;a<=32767;a++){printf("%d",a);}getch();}
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();}
Output:
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();}
Output:
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();}
Output:
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();}
Output:
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.
Example 1:
#include<stdio.h>void main(){int i=1;clrscr();while(i<=5){printf("%d",i);i++;}printf("\nabc");getch();}
Output:
Example2:#include<stdio.h>void main(){int c=1,d=0;clrscr();while(d<=9){printf("\n%d %d",++d,++c);}printf(“xyz”);getch();}
Output:
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();}
Output:
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
Example 3#include<stdio.h>void main(){int i=5;clrscr();do{printf("%d",i);i--;}while(i>=0);getch();}
Output:
Example 4:#include<stdio.h>void main(){int i=5;clrscr();do{printf("%d",i);i--;}while(i<=0);getch();}
Output:
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.
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();}
Output goto:
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();}
Output:
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.
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.
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];
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();}
Output:
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();}
Output:
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.
Output:
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();
}
Output:
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();
}
Output:
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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.
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;}
Output
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.
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;}
Output
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.
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
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();}
Output
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();}
Output
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;}
Output
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;}
Output
Types of functions1.(without arguments and without return values)
#include<stdio.h>void message();void main(){clrscr();message();getch();}void message(){printf("civil");}
Output
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);}
Output
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);}
Output
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);}
Output
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));}
Output
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);}
Output
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);}
Output
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);}
Output
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);}
Output
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));}
Output
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;}
Output
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);}
Output
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 :");}
Output
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;}
Output
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;}
Output
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;}
Output
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;}
Output
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;}
Output
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();}
Output
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();}
Output
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();}
Output
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();
}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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;}
Output
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;}
Output
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;}
Output
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();}
Output
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();}
Output
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();}
Output
[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();}
Output
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();}
Output
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();}
Output
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();}
Output
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();}
Output
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.
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();}
Output
External Variables#include<stdio.h>int x=10;void main(){int x;clrscr();printf("x=%d",x);getch();}
Output
Continued..
#include<stdio.h>int x=10;void main(){extern int x;clrscr();printf("x=%d",x);getch();}
Output
Static Variables
#include<stdio.h>void main(){int x=10;static int y;clrscr();printf("x=%d y=%d",x,y);getch();}
Output
Register Variables
#include<stdio.h>
void main()
{
register int y=10;
clrscr();
printf("y=%d",y);
getch();
}
output
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();}
Output