Professional Documents
Culture Documents
C Meterial
C Meterial
INDEX
1. Introduction to C Language
2. Basics and Data Types
3. Operators
4. Control Statements
a. Selection/Conditional Statements
b. Loops
c. Jump Statements
5. Arrays
6. Strings
7. Pointers
a. Pointes with Arrays
b. Pointers with Strings
8. Functions
a. Functions with Arrays
b. Functions with Strings
c. Functions with Pointers
9. Storage Classes
10. Structures and Bit Fields
a. Structures with Arrays
b. Structures with Pointers
11. Unions
12. Enums
13. Preprocessing
14. Dynamic Memory Allocation
15. Files
1. Introduction to C Language
1
C Language was developed by Dennis Ritchie in the year 1972 at AT&T bell labs. It is
procedural oriented language.
Languages
OS cannot convert the language directly to machine understandable form , It takes the
support of translators.
Translator: It is software which converts high level language into low level language and
vice versa.
Types of Translators:-
1. Assembler
2. Compiler
3. Interpreter
Assembler: It converts the assembly language into machine understandable form line by
line.
2
Compiler: A compiler is a software that translates high-level language source code to
machine language code(object code).
Compiler Vs Interpreter
Compiler converts the entire code at a time where as interpreter converts the code line
by line.
Procedure – oriented programming:
Conventional programming using high level languages such as COBOL, FORTRAN and C
are commonly known as procedure-oriented programming language. In the procedure-
oriented approach, the problem is viewed as a sequence of things to be done, such as
reading, calculating and printing. A number of functions are written to accomplish these
tasks.
Applications of C language
We can develop
Games
Drivers
language translators
spread sheets,
data bases and
part of windows is also designed using c language.
Comments: It improves the program readability, It hides the information from the
compiler, It is good practice to use comments in the program.
stdio.h :It stands for standard Input and Output header file.
#include<stdio.h>
Output functions:
printf(); is used to print formatted data on the screen
puts(); is used to print a string on the screen.
putc(); is used to print a single character.
Input functions:
scanf(); is used to read formatted data from std input device
gets(); is used to read a string from std input device
getc(); is used to read a single character.
when we are writing these functions in program we have to include this header file
#include<stdio.h>
when we are writing these functions in program we have to include conio.h header file
#include<conio.h>
Execution of C program
Execution starts with main function.
void main():
4
main is the special function, program execution always starts from main function.
void main()
{
=====
Entry point ====== executable part
======
} Exit Point
void fun1()
{
======
======
}
We can write main function in different ways.
void main() void main(void) int main()
{ { { retun 0;
} } }
5
//Sample C program to print a welcome message
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr(); // clears the screen
printf("Welcome to C programming\n");
getch(); //reads a character from std input device
}
Data Types
Two types of data
1. Constant
2. Variable
Data Types: The data type of a variable is a keyword used to determine, that what
kind (format) of data it can store.
ex:
int num1,num2;
float f;
char ch;
Format Specifiers:
They are used to specify the type of data to display or to read.
int %d or %i
long int %ld
float %f
double %lf
long double %Lf
char %c
String %s
Hexadecimal %x
Unsigned Integer %u
7
Escape sequence characters (non-printable characters)
\n - new line
\t - tab space
\r - carriage return
\a - alarm or bell
\b - back space
\" - to print double quotes
\\ - to print back slash
#include<stdio.h>
#include<conio.h>
void main()
{
int num1;
printf("Enter a number\n");
scanf("%d",&num1);
printf("You entered %d",num1);
getch();
}
/*program to read a floating point number from user and display it on the screen
*/
#include<stdio.h>
#include<conio.h>
void main()
{
float f;
clrscr();
printf("Enter a number\n");
scanf("%f",&f);
printf("You entered %.1f",f);
getch();
}
8
/*program to read a string from user and display it on the screen */
#include<stdio.h>
#include<conio.h>
void main()
{
char name[15];
clrscr();
printf("Enter Name\n");
scanf("%s",&name);
printf("Hello %s",name);
getch();
}
3. Operators
Operators in C:
1.Arithmetic operators
2.Relational operators
3.Logical operators
4.Assignment operators
5.Increment or decrement operators
6.Conditional operators
7.Bitwise Operators
1.arithmetic operators:(+,-,*,/,%)
Arithmetic operators are used to perform arithmetic operations like addition,
subtraction, multiplication, division and remainder.
ex:
a>b
a<c
a==b
a!=c
a>=b
a<=c
&& : It is used to combine two or more relational expressions, it returns true when all
conditions are true, other wise it returns false.
Truth Table:
T T T
T F F
F T F
F F F
|| : This operator is used to combine two or more relational expressions, it returns true
when any one of the condition is true, else it returns false.
Truth table:
T T T
T F T
10
F T T
F F F
! (not): This is the negative operator, we can place the not operator before any one of
the condition. It returns the opposite result of the condition. i.e. It converts true to false
and false to true.
Truth table:
T F
F T
First the value will be incremented, and then the new value will be printed.
b. post increment(a++)
ex:- printf(“%d”,a++);
11
First the value will be printed and then the value will be incremented.
7. Bitwise Operators
C provides six operators for bit manipulation; these may only be applied to integral
operands, that is, char, short, int, and long, whether signed or unsigned.
& bitwise AND
| bitwise inclusive OR
^ bitwise exclusive OR
<< left shift
>> right shift
~ one's complement (unary)
/* program to read 2 numbers from user and calculate the sum and display the sum*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,sum;
clrscr();
printf("Enter 2 numbers\n");
12
scanf("%d%d",&n1,&n2);
sum=n1+n2;
printf("%d + %d : %d\n",n1,n2,sum);
getch();
}
4. Control Statements
Control structures: Control structures are used to control the flow of the program.
1.Conditional statements.
2.Loops
3.Multibranching statement
4.Unconditional statement
Conditional Statements:
i) if
ii) if
else
iii) if
13
else if
else
iv) Nested if
Syntax:
i)
if(condition)
{
set of statements; block1
}
ii)
if(condition)
{
set of statements; //block 1
}
else
{
set of statements; //block 2
}
if Condition is true block 1 statements will be executed, if condition is false block 2
statements will be executed.
iii)
if(condition1)
{
set of statements; //block 1
}
else if(condition2)
{
set of statements; //block 2
}
else
{
set of statements; //block 3
14
}
iv)Nested if
if(condition1)
{
if(condition2)
{
set of statements; //block 1
}
else
{
set of statements; //block 2
}
}
else
{
if(condition3)
{
set of statements; //block 3
}
else
{
set of statements; //block 4
}
}
if Condition1 is true and Condition 2 is true then block 1 statements will be executed, if
condition1 is true and condition2 is false block 2 statements will be executed, if
Condition1 is false and Condition 3 is true then block 3 statements will be executed, if
condition1 is false and condition3 is false block 4 statements will be executed.
15
/* program to find whether the given number is Even or Odd */
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("Enter a number\n");
scanf("%d",&n);
if(n%2==0)
{
printf("The number is even\n");
}
else
{
printf("The number is Odd\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("Enter a number\n");
scanf("%d",&n);
if(n>0)
{
printf("The number is Positive\n");
}
else if(n<0)
{
printf("The number is Negative\n");
}
else
{
printf("Zero\n");
}
getch();
}
/* program to read student num,name,marks and calculate total and average and
print result and division */
#include<stdio.h>
#include<conio.h>
void main()
{
int sno,m1,m2,m3,total,avg;
char sname[15];
clrscr();
printf("Enter Student number\n");
scanf("%d",&sno);
printf("Enter Student name\n");
scanf("%s",&sname);
printf("Enter 3 subjects marks\n");
scanf("%d%d%d",&m1,&m2,&m3);
total=m1+m2+m3;
avg=total/3;
clrscr();
printf("Student Profile\n");
printf("Student No: %d\n",sno);
printf("Student Name: %s\n",sname);
if(m1>=40 && m2>=40 && m3>=40)
{
printf("Result: Pass\n");
19
printf("Total marks : %d\n",total);
if(avg>=75)
printf("Distinction\n");
else if(avg>=60)
printf("First Division\n");
else if(avg>=50)
printf("Second Division\n");
else
printf("Third Division\n");
}
else
{
printf("Result: Fail\n");
printf("Total marks : %d",total);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int basic_salary,gross_salary;
int net_salary,pf,hra,da,eno;
char ename[15];
20
clrscr();
printf("Enter employee no:\n");
scanf("%d",&eno);
printf("Enter employee name:\n");
scanf("%s",&ename);
printf("Enter Basic salary:\n");
scanf("%d",&basic_salary);
pf=0.12*basic_salary;
hra=0.25*basic_salary;
da=015*basic_salary;
gross_salary=basic_salary+pf+hra+da;
net_salary=gross_salary-pf;
clrscr();
printf("Employee profile\n");
printf("Employee No: %d\n",eno);
printf("Employee Name: %s\n",ename);
printf("Basic salary: %d\n",basic_salary);
printf("Gross salary: %d\n",gross_salary);
printf("Net salary: %d\n",net_salary);
getch();
}
Switch:
Switch is used to execute one of the options from number of options. It is also called as multi branching
conditional statement.
Syntax:
switch(expression)
21
.
default:statements; break;
The expression value may be numeric or character type. The switch statement evaluates the expression. It will
select one of the cases based on the expression. It will execute default statements when no case is selected
break statement:
It is used to exit from a looping statement. We can use this in for, while , do while, and switch statement.
ex:- break;
/*program to read a character from user and check whether it is vowel or not using switch*/
#include<stdio.h>
#include<conio.h>
void main()
char ch;
clrscr();
printf("Enter a character\n");
scanf("%c",&ch);
switch(ch)
22
case 'a':
case 'e':
case 'i':
case 'o':
getch();
Output:
#include<stdio.h>
#include<conio.h>
void main()
int n;
clrscr();
printf("Enter a number\n");
scanf("%d",&n);
switch(n)
case 1: printf("ONE");break;
case 2: printf("TWO");break;
23
case 3: printf("THREE");break;
case 4: printf("FOUR");break;
case 5: printf("FIVE");break;
getch();
Output:
unconditional statement:
goto statement: It is used to transfer the control from one place to another place in the program. It is also
called as unconditional jumping statement
syntax:
example:-
goto print;
#include<stdio.h>
#include<conio.h>
void main()
int n;
start:
24
clrscr();
scanf("%d",&n);
switch(n)
case 1: printf("Jan");break;
case 2: printf("Feb");break;
case 3: printf("Mar");break;
case 4: printf("Apr");break;
case 5: printf("May":);break;
case 6: printf("Jun");break;
case 7: printf("Jul");break;
case 8: printf("Aug");break;
case 9: printf("Sep");break;
getch();
output
25
Ex:4Which of the following errors would be reported by the compiler on compiling the program given
below?
#include<stdio.h>
int main()
int a = 5;
switch(a)
case 1:printf("First");
case 2:printf("Second");
case 3 + 2:printf("Third");
case 5:printf("Final");break;
return 0;
Answer: Option C
#include<stdio.h>
int main()
26
int P = 10;
switch(P)
return 0;
Answer: Option B
#include <stdio.h>
void main()
int i = 5, k;
if (i == 0)
goto label;
27
label: printf("%d", i);
printf("Hey");
a) 5
b) Hey
c) 5 Hey
d) Nothing
output:C
#include <stdio.h>
int main()
goto l1;
l1:goto l2;
a) 1 4
c) 1 2 4
d) 1 3 4
28
output:a
#include <stdio.h>
int main()
l1:l2:
printf("%d\n", 3);
b) 1 2 3
c) 1 2
d) 1 3
ans :b
#include <stdio.h>
int main()
{
29
printf("%d ", 1);
goto l1;
void fun1()
a) 1 2 3
b) 1 3
c) 1 3 2
ans :d
explanation: goto is a jumping statement from one place to another place with in the funtion.in above
program lable existed in fun1() goto statement existed in main.
#include <stdio.h>
int main()
int i = 0, j = 0;
while (i < 2)
30
l1: i++;
while (j < 3)
printf("loop\n");
goto l1;
a) loop loop
d) Infinite loop
ans:
LOOPS
Looping statements: when we want to execute a statement or set of statements repeatedly then we use
loops.
while statement:
It is used to execute a statement or set of statements repeatedly as long as the given condition is true.
syntax:
while(condition)
31
First the condition will be tested, if it is true the body of the while loop will be executed first time, again the
control will be transferred back to while loop. If it is false the control will come out of the while loop.
#include<stdio.h>
#include<conio.h>
void main()
int i=1,n;
clrscr();
printf("Enter a number\n");
scanf("%d",&n);
while(i<=n)
printf("%d\t",i);
i++;
getch();
} ouput:
/* program to print even numbers from 1 to n(user input) using while loop*/
#include<stdio.h>
#include<conio.h>
void main()
int i,n;
clrscr();
32
printf("Enter a number\n");
scanf("%d",&n);
i=1;
while(i<=n)
if(i%2==0)
printf("%d\t",i);
i++;
getch();
Output:
/* program to find the sum of numbers from 1 to n(user input) using while loop*/
#include<stdio.h>
#include<conio.h>
void main()
int i,n,sum=0;
clrscr();
printf("Enter a number\n");
scanf("%d",&n);
33
i=1;
while(i<=n)
sum=sum+i;
i++;
getch();
Output:
#include<stdio.h>
#include<conio.h>
void main()
int n,r,rev=0,temp;
clrscr();
printf("Enter a number\n");
scanf("%d",&n);
temp=n;
while(n>0)
r=n%10;
n=n/10;
34
rev=rev*10+r;
getch();
Output:
/*accept a number from the user findout given number is polindrum or not?
#include<stdio.h>
#include<conio.h>
void main()
int n,r,rev=0,temp;
clrscr();
printf("Enter a number\n");
scanf("%d",&n);
temp=n;
while(n>0)
r=n%10;
n=n/10;
rev=rev*10+r;
Else
getch();
Output:
#include<stdio.h>
#include<conio.h>
void main()
int n,r,total=0,n1;
clrscr();
printf("Enter a number\n");
scanf("%d",&n);
n1=n;
while(n>0)
r=n%10;
n=n/10;
total=total*(r*r*r);
36
}
if(total=n1)
else
getch();
Output:
#include<stdio.h>
#include<conio.h>
void main()
int a=0,b=1,fib,n;
clrscr();
printf("Enter a number\n");
scanf("%d",&n);
printf("%d\t%d\t",a,b);
fib=a+b;
while(fib<=n)
printf("%d\t",fib);
a=b;
b=fib;
fib=a+b;
37
}
getch();
Output:
Assignment:
1)accept a number from user findout sum of even elements upto that number (1 to n)?
2)accept a number from the user fincout avg of even elements and odd elements (1 to n)?
3)accept a number from the user findout given number is prime or not?
4)accept a number from the user findout given number is perfect number or not?
if
----------------------------------------------------------------
2)Accept marks from the user print fail when marks <35?
if-else
--------------------------------------------------------
3)Accept a number from the user print given number is +ve r not?
else 20%
7)Accept a character from the user print male if given char 'm' or
38
'M' else female ?
8) Accept a char from the user find out given char ovel or not?
9) Accept a number from the user and check whether the given number
is divided by 3 or 7?
10) Accept 3 subjects marks from the user and find the result? (Note : pass marks are greater than 34 in all
subjects )
if-else-if
--------------------------------------------------------
12) Accept age from the user print status of the candidate
13) Accept a number from the user and find the number of digits
so on.)
14) Accept 3 numbers from the user and find the largest.
nested if
-----------
39
Do-while
do while loop:
syntax:
do
set of statements;
}while(condition);
This is similar to while loop, but the difference is this loop will execute the statements at least once.
#include<stdio.h>
#include<conio.h>
void main()
int i=1,sno,marks;
char sname[15],ch;
clrscr();
do
scanf("%d",&sno);
scanf("%d",&marks);
printf("Student %d Details\n",i++);
ch=getch();
}while(ch=='y');
getch();
Output:
for loop :
It is used to execute a set of statements repeatedly as long as the given condition is true.
syntax:
When the for loop is executing for the very first time the initial value will be stored in the given variable.
Then the condition will be tested. If it is true, the statements will be executed. After executing the last
41
statement the control will return back to the for loop. Then the value will be incremented / decremented.
Again the condition is tested, if it is false the control will come out of the for loop.
ex:
for( ;i<=10;i++)
for(; i<=10 ; )
for(i=1,j=10;i<=10;i++,j--)
#include<stdio.h>
#include<conio.h>
void main()
int base,exp,i,res=1;
clrscr();
scanf("%d%d",&base,&exp);
for(i=1;i<=exp;i++)
res*=base;
getch();
Output:
42
/* program to check whether the number is prime or not */
#include<stdio.h>
#include<conio.h>
void main()
int n,I,nf=0;
clrscr();
printf("Enter a number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
if(n%i==0)
nf++;
If(nf==2)
else
getch();
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
43
int n1,n2,r,t,i;
clrscr();
printf("Enter range(n1,n2)\n");
scanf("%d%d",&n1,&n2);
for(i=n1;i<n2;i++)
t=0;
n1=i;
while(n1>0)
r=n1%10;
n1=n1/10;
t=t+r*r*r;
if(t==i)
printf("%d\n",i);
getch();
} output:
#include<stdio.h>
#include<conio.h>
void main()
int i,j,k;
for(i=1;i<=5;i++)
44
{
for(k=5;k>=i;k--)
printf(" ");
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
getch();
**
***
****
*****
*/
#include<stdio.h>
#include<conio.h>
void main()
int i,j,k;
for(i=1;i<=5;i++)
{
45
for(k=5;k>i;k--)
printf(" ");
for(j=1;j<=i;j++)
printf("*");
printf("\n");
getch();
Output:
Arrays
========
Array is a group of values that belongs to similar data type that can share a common name.
Array values can be called by using the index number, index number starts from 0 and ends with the total size-
1. It allocates memory continuously.By default it will have all garbage values.
2)Multi dimensional arrays: The array which is using more than one subscripts is known as 2 – dimensional
array
int n[10]={1,2,3,4,5,6,7,8,9,10};
char ch[15]={'a','b','c',....};
char st[15]="hyderabad";
int n[3][3]={{1,2,3},
{4,5,6},
{7,8,9}};
char st[5][4]={"str1","str2","str3","str4","str5"};
Explanation:
1)An array is a derived datatype in c, which is constructed from fundamental datatype of c language.
3)In implimentation when we required n number of variables of same data type then go for array.
4)when we are working with arrays always memory will created in continuous memory location.so randomly
we can access the data.
5)In arrays all elements will share same name with unique identification value called index.
6)always array index will starts with 0 and ends with size-1.
7)when we are working with array compiletime memory management will occur i.e static memory allocation.
syntax:
datatype arrayname[size];
always size of the array must be unsigned integer value which is greater then 0 only.
properties of Arrays
=====================
1)Always size of array must be an unsigned integer values which is gerater then 0
int a[5];
size --5
2)int a[5];
size --5
a[0]
a[1]
a[2]
48
a[3]
a[4]
3)
In array declaration,size of the array must be required, if size is not mentioned then compiler will give
an error.
6)int a[5]={10,20,30,40,50};
a[0]=10
a[1]=20
a[2]=30
a[3]=40
a[4]=50
7)int a[5]={10,20};
a[0]=10
a[1]=20
a[2],a[3],a[4]=0
in initialisation of the array if specific number of values are not initialised if then rest of all values will
be initialised with '0';
9)in initialisation of the array mentioning the size is optional, in this case how many elements are initialise if
that many are variables are created.
49
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
In c and cpp there is no upper boundary checking process will occur ,that is why above syntax is valid.
whenever we are crossing the upper limit depends on security level of os anything can be happens.
11)int a[40000];
on dos based compiler limit is 65535 bytes only will create.but in above styntax we required 80000 bytes.
size 12
sizeof(a)---48 bytes
in declaration of array size must be constant type only. i.e variable or constant variables or not
allowed.
18)#define size 5
50
int a[size]; //valid
In declaration of array we can mention symbolic constant data because at the time of preprocessing
identifier will replace with replacement text.
/*program to read 10 numbers and store it in array and display on the screen */
#include<stdio.h>
#include<conio.h>
void main()
int n[10],i;
clrscr();
printf("Enter 10 numbers\n");
for(i=0;i<10;i++)
scanf("%d",&n[i]);
for(i=0;i<10;i++)
printf("%d\t",n[i]);
getch();
Output:
/*program to read 10 numbers and store it in array and calculate the sum of numbers in the array*/
51
#include<stdio.h>
#include<conio.h>
void main()
int n[10],i,sum=0;
clrscr();
printf("Enter 10 numbers\n");
for(i=0;i<10;i++)
scanf("%d",&n[i]);
for(i=0;i<10;i++)
sum+=n[i]; //sum=sum+n[i];
getch();
Output:
#include<stdio.h>
#include<conio.h>
void main()
char st[10];
clrscr();
scanf("%s",&st);
52
printf("Hello : %s\n",st);
getch();
Output:
#include<stdio.h>
#include<conio.h>
void main()
char st[10][10];
int i;
clrscr();
for(i=0;i<10;i++)
scanf("%s",&st[i]);
clrscr();
for(i=0;i<10;i++)
printf("\n%s\n",st[i]);
getch();
Output:
53
/* program to find the largest and smallest element in the array*/
#include<stdio.h>
#include<conio.h>
void main()
int n[10];
int large,small,i;
clrscr();
for(i=0;i<10;i++)
printf("Enter number%d\n",i+1);
scanf("%d",&n[i]);
large=small=n[0];
for(i=1;i<10;i++)
if(large<n[i])
large=n[i];
else if(small>n[i])
small=n[i];
}
54
Output:
#include<stdio.h>
#include<conio.h>
void main()
int a[3][3],b[3][3],c[3][3];
int i,j;
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
}
55
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
printf("\n");
getch();
Output:
/* matrix multiplication */
#include<stdio.h>
#include<conio.h>
void main()
56
{
int a[2][2],b[2][2],c[2][2]={0,0,0,0};
int i,j,k;
clrscr();
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
for(k=0;k<2;k++)
for(i=0;i<2;i++)
for(j=0;j<2;j++)
57
{
c[k][i]=c[k][i]+a[k][j]*b[j][i];
printf("Resultant matrix\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("%5d ",c[i][j]);
printf("\n");
getch();
/* transpose of a matrix */
#include<stdio.h>
#include<conio.h>
void main()
int a[3][3];
int i,j;
clrscr();
58
printf("Enter elements of the matrix\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nTranspose matrix:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf("%d",a[j][i]);
printf("\n");
getch();
Output:
100 500
010 050
001 005
#include<conio.h>
void main()
int i,j,a[3][3],de,oe,temp;
de=oe=0;
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
temp=a[0][0];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(i==j)
if(a[i][j]==temp)
de++;
else
if(a[i][j]==0)
oe++;
}
60
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf("%d ",a[i][j]);
printf("\n");
printf("Diagonal matrix\n");
else
getch();
#include<stdio.h>
#include<conio.h>
void main()
int i,j,a[3][3],de,oe,temp;
de=oe=0;
clrscr();
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(i==j)
if(1==a[i][j])
de++;
else
if(a[i][j]==0)
oe++;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf("%d ",a[i][j]);
printf("\n");
else
getch();
Output:
Strings
constats
syntax:
char str[size];
properties
============
1)char a[5];
size=5
sizeof(a)= 5 bytes
5)char a[5]={a,b,c,d,e};//error
6)char a[5]={'a','b','c','d','e'};//valid
7)char a[5]={'a','b','c','d','e','f'};//error
9)char str[2]={'a','b','c'}
64
In initialization of the string we cant initialized more than size
of string elements.
11)char a[5]={65,66,67,68,69};//valid
12)char a[10]="welcome";//valid
15)char str[]="hello";
size -6b
sizeof(str) --6b
16)char a[5]={'a','b','c','d','e'};
17)char a[5]="abcd";
65
when we are working with string data at end of the string
null char will occupies physical memory.
String functions:
syntax:- strlen(string);
2) strcpy():- This function copies a string from one string variable to another
string variable.
strcpy(s2,s1);
syntax:- strcat(string1,stirng2 );
This compares one string with another string, if two strings are equal this
function returns 0, else it returns the numeric difference between the non-
matching characters.
66
5) stricmp(): This function is similar to strcmp, but this function ignores the case
sensitivity.
Syntax:- strrev(string);
7) strupr(): This function converts the given string into upper case(capitals)
syntax:- strupr(string)
8) strlwr(): This function converts the given string into lower case.
syntax:- strlwr(string);
#include<stdio.h>
#include<conio.h>
void main()
int i;
clrscr();
for(i=1;i<255;i++)
{
67
printf("%c => %d\n",i,i);
if(i%40==0)
getch();
getch();
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char st[20];
int len;
clrscr();
printf("Enter a string\n");
scanf("%s",&st);
len=strlen(st);
getch();
68
}
// string copy
#include<stdio.h>
#include<conio.h>
void main()
char st[10],st1[10];
int i;
printf("Enter a string\n");
gets(st);
for(i=0;st[i]!='\0';i++)
st1[i]=st[i];
st1[i]='\0';
getch();
69
/* program to reverse the contents of a string using string
function */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char st[15];
clrscr();
printf("Enter a string\n");
scanf("%s",&st);
strrev(st);
getch();
function */
#include<stdio.h>
70
#include<conio.h>
#include<string.h>
void main()
int len,i;
char st[15],temp;
clrscr();
printf("Enter a string\n");
scanf("%s",&st);
for(i=0;i<len/2;i++)
temp=st[i];
st[i]=st[len-1-i];
st[len-1-i]=temp;
getch();
71
/* program to convert a string to uppercase and lowercase without
#include<stdio.h>
#include<conio.h>
void main()
int len,i;
char s1[15],s2[15],temp;
clrscr();
printf("Enter a string\n");
scanf("%s",&s1);
printf("Enter a string\n");
scanf("%s",&s2);
for(i=0;s1[i]!='\0';i++)
s1[i]=s1[i]-32;
printf("Uppercase : %s\n",s1);
for(i=0;s2[i]!='\0';i++)
{
72
if(s2[i]>=65 && s2[i]<=90)
s2[i]=s2[i]+32;
printf("Lowercase : %s\n",s2);
getch();
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char st1[15],st2[15];
clrscr();
printf("Enter 2 strings\n");
scanf("%s%s",&st1,&st2);
if(strcmp(st1,st2)==0)
else
73
getch();
#include<stdio.h>
#include<conio.h>
void main()
char st1[15],st2[15];
int l1,l2,i;
clrscr();
printf("Enter 2 strings\n");
scanf("%s%s",&st1,&st2);
for(l1=0;st1[l1]!='\0';l1++);
for(l2=0;st2[l2]!='\0';l2++);
if(l1==l2)
for(i=0;i<l1;i++)
if(st1[i]!=st2[i])
break;
74
}
if(i==l1)
else
getch();
#include<stdio.h>
#include<conio.h>
void main()
char st1[25],st2[15];
clrscr();
printf("Enter 2 strings\n");
scanf("%s%s",&st1,&st2);
strcat(st1,st2)
printf("st1: %s\n",st1);
75
getch();
#include<stdio.h>
#include<conio.h>
void main()
char st1[25],st2[15];
int l1,l2,i;
clrscr();
printf("Enter 2 strings\n");
scanf("%s%s",&st1,&st2);
for(l1=0;st1[l1]!='\0';l1++);
for(l2=0;st2[l2]!='\0';l2++);
for(i=0;i<l2;i++)
st1[l1+i]=st2[i];
printf("st1: %s\n",st1);
76
getch();
/* program to print numbers from 1 to 255 and their equivalent ascii characters
*/
#include<stdio.h>
#include<conio.h>
void main()
int i;
clrscr();
for(i=1;i<255;i++)
if(i%40==0)
getch();
getch();
#include<stdio.h>
77
#include<conio.h>
void main()
int i,j,found=0,len;
char st1[30],st2[10];
clrscr();
gets(st1);
gets(st2);
for(len=0;st2[len]!='\0';len++);
for(i=0;st1[i]!='\0';i++)
if(st1[i]==st2[0])
for(j=0;j<len;j++)
if(st1[i+j]!=st2[j])
break;
if(j==len)
78
{
found=1;
if(found==0)
getch();
4)
void main()
char str[5]="Welcome";
printf("%s",str);
getch();
output:error
79
5)
void main()
char str[]="Welcome";
printf("\n %s",str);
printf("\n %s",str+3);
printf("\n %s",str+5);
getch();
output:
6)
void main()
char str[]="abcdxyz";
80
printf("\n %s",str);
printf("\n %s",str+3);
str[3]=0;
printf("\n %s",str);
str[3]='0';
str[4]=100;
printf("\n %s",str);
str[4]='\0';
printf("\n %s",str);
getch();
output:
7)
81
void main()
char str[10]="Welcome";
int i;
clrscr();
for(i=0;str[i]!='\0';i++)
puts(str+i);
getch();
output:
welcome
elcome
lcome
come
ome
82
me
8)
puts(ptr);
if(*ptr);
abc(ptr+1);
void main()
char str[]="Hello";
clrscr();
abc(str);
getch();
83
output:
9)
void main()
char str="welcome";
clrscr();
l=strlen(str);
l=strlen(str+3);
l=strlen(str+5);
84
printf("\n %d",l); //2
getch();
10)
void main()
char str="welcome";
clrscr();
strrev(str);
strrev(str+3);
strrev(str+5);
strrev(str+7);
getch();
85
11)
void main()
char s1[15]="Welcome";
char s2[10]="Hello";
clrscr();
puts(s1);
puts(s2);
strcat(s1," ");
strcat(s1,s2);
getch();
1)strcat(s1,s2+2)
86
2)strcat(s1+3,s2)
3)strcat(s1+3,s2+2)
note:
when we are working with strcat always appending will take place at the end of
the destination string.
void main()
char a[20]="welcome";
clrscr();
strupr(str);
strupr(str+3);
strupr(str+5);
getch();
87
}
examples
printf(2+"welcome"); lcome
printf("welcome"+3); come
printf(2+"welcome"+3);
Pointers
Advantages:
=========
88
Dynamic memory allocation is possible using pointers only.
function can return only one value at a time, using pointers we can
syntax:
datatype *ptrname;
int a=5;
int *ptr;
ptr=&a;
89
recommended to go for %x ,%p,%lp will print the address in hexa
decimal format
i.e
void main()
Int x;
char *p;
p=&x; //warning
getch();
void main()
int a;
int *ptr;
a=10;
ptr=a;
syntax:
void main()
91
{
int i;
int *ptr;
int **pptr;
int ***ppptr;
ptr=&i;
pptr=&ptr;
ppptr=&pptr;
i=10;
int i1,i2;
int *p1,*p2;
p1=&i1;
p2=&i2;
p1+p2;
p1*p2;
p1/p2;
p1*2;
92
p1/5;
p1%2;
p1++ nextaddress
p2-p1 no of elements
Pointer Properties
================
Pointer arithmetic
Rule 1:
======
address+number=address(next address)
++address=address(next address)
--address=address(pre address)
Rule 2:
93
=====
address-address=number(no of elements)
p2-p2=50;
200-100=100/sizeof(int); =100/2=50
note :
int *p1,*p2;
p2-p1 value is 1.
Rule 3
=====
address+address=illegal
address*address=illegal
address/address=illegal
address %address=illegal
Rule 4
======
(>,<,>=,<=,==,!=)
94
address>address=t/f
address>=address=t/f
Rule 5:
====
address | address=illegal
address ^ address=illegal
~address=illegal
Rule 6:
======
=====================
void main()
int a;
95
when we are working with pointers always recommended to initialised with any
variable address or make it null.
Null pointer:
==========
which pointer variable initialised with null ,it is called null pointer.
syntax:
=====
datatype *ptr=null;
int *ptr=null;
float *ptr=null;
=====================
syntax
=====
=========
void main()
int i;
float f;
char ch;
97
char *cptr=(char *)0;
iptr=&i;
i=11;
printf("\n %d %d %d",i,*iptr);
fptr=&fp;
f=12.12;
printf("\n %d %d %d",f,*ptr);
cptr=&ch;
ch='A';
printf("\n %c %c",ch,*ptr);
getch();
void main()
int i;
char ch;
98
float f;
ptr=&i;
i=11;
ptr=&ch;
ch='A';
ptr=&f;
f=3.4;
printf("\n %f %f",f,*(float*)ptr);
getch();
99
#include <stdio.h>
int main()
int i = 10, j = 2;
b) 10 2 4
c) 10 2 2
d) 10 2 6
Answer:b
#include <stdio.h>
int main()
int i = 10, j = 3;
b) 10 3
100
d) Undefined behaviour
Answer:c
#include <stdio.h>
int main()
int i = 10, j = 3, k = 3;
b) 10 3 3
c) 10 3
d) 10 3 somegarbage value
Answer:c
#include <stdio.h>
int main()
char *s = "myworld";
int i = 9;
printf("%*s", i, s);
101
}
a) myworld
d) Undefined
Answer:b
#include <stdio.h>
char *s = "myworld";
int i = 3;
printf("%10.%s", i, s);
a) myw
Answer:d
c) No differences
a) %
b) /
c) ”
Answer:d
d) Both a and c
Answer:b
a) %
b) %
c) ‘%’
d) %%
Answer:d
103
10.What is the output of this C code?
#include <stdio.h>
void main()
double k = 0;
printf("%lf", k);
a) 2.000000
b) 4.000000
c) 3.000000
ans :c
#include <stdio.h>
void main()
do
printf("hello");
104
} while (str);
a) Nothing
c) Varies
Answer:d
#include <stdio.h>
void main()
int i = 0;
i++;
printf("hi\n");
while (i < 8)
i++;
105
printf("hello\n");
Answer:
a) for
b) while
c) do-while
Answer:d
14. Number of times while loop condition is tested is, i is initialized to 0 in both
case.
while (i < n)
i++;
-------------
do
i++;
}
106
while (i <= n);
a) n, n
b) n, n+1
c) n+1, n
d) n+1, n+1
Answer:d
#include <stdio.h>
int main()
int i = 0;
while (i = 0)
printf("True\n");
printf("False\n");
c) False
d) Compiler dependent
107
Answer:c
#include <stdio.h>
int main()
int i = 0, j = 0;
i++;
j++;
a) 5, 5
b) 5, 10
c) 10, 10
d) Syntax error
Answer:c
17. Which loop is most suitable to first perform the operation and then test the
condition?
a) for loop
108
b) while loop
c) do-while loop
Answer:c
#include <stdio.h>
void main()
int k;
printf("Hello");
a) Hello
b) Infinite hello
d) Nothing
ans :d
#include <stdio.h>
int main()
109
int i = 0;
for (; ; ;)
printf("After loop\n");
b) Infinite loop
c) After loop
d) Undefined behaviour
ans:c
#include <stdio.h>
int main()
int i = 0;
for (i++; i == 1; i = 2)
printf("After loop\n");
110
b) After loop
d) Undefined behaviour
ans:a
#include <stdio.h>
int main()
int i = 0;
for (foo(); i == 1; i = 2)
printf("After loop\n");
int foo()
return 1;
a) After loop
d) Infinite loop
111
ans: a
#include <stdio.h>
int main()
int *p = NULL;
for (foo(); p; p = 0)
printf("After loop\n");
c) Infinite loop
ans:b
#include <stdio.h>
int main()
112
}
b) In for loop
ans : c
#include <stdio.h>
int main()
int a = 10, b = 5, c = 5;
int d;
d = b + c == a;
printf("%d", d);
output:1
25)
#include <stdio.h>
int main()
int a = 10, b = 5, c = 3;
113
b != !a;
c = !!a;
printf("%d\t%d", b, c);
output: 5 1
26)#include <stdio.h>
int main()
int a = 10;
if (a == a--)
printf("TRUE 1\t");
a = 10;
if (a == --a)
printf("TRUE 2\t");
output:
TRUE 1 TRUE 2
27)#include
int main()
int i = 0, j = 0;
114
if (i && (j = i + 10))
printf("\n %d",j);
Output:0
28)
#include
int main()
int i = 1;
printf("Yes\n");
else
printf("No\n");
115
}
#include<stdio.h>
int main()
printf("%d\n", -1<<1);
return 0;
output:
-2
30)#include<stdio.h>
int main()
m=m-64;
printf("%d",m); //-32
printf("%u",m); //65502
return 0;
output :
-32 65502
116
31)
#include<stdio.h>
int main()
unsigned int m = 5;
printf("%d",~i);
return 0;
32. Which of the following statements are correct about the below C-program?
#include<stdio.h>
int main()
if(x != y);
return 0;
117
}
A. 1
B. 2, 3
C. 3, 4
D. 4
Answer: Option B
#include<stdio.h>
int main()
int k, num=30;
printf("%d\n", num);
return 0;
A. 200
118
B. 30
C. 100
D. 500
Answer: Option B
34. The keyword used to transfer control from a function back to the calling
function is
A. switch
B. goto
C. go back
D. return
Answer: Option D
int i;
long f=1;
f = f * i;
return f;
D. None of above
Answer: Option C
#include<stdio.h>
int main()
float a=3.14;
char *j;
j = (char*)&a;
printf("%d\n", *j);
return 0;
A. It prints ASCII value of the binary number present in the first byte of a float
variable a.
B. It prints character equivalent of the binary number present in the first byte
of a float variable a.
C. It will print 3
Answer: Option A
#include<stdio.h>
120
int main()
void *k;
j=k=&a;
j++;
k++;
return 0;
A. Yes
B. No
Answer: Option B
2: The expression num[1] designates the very first element in the array.
A. 1
121
B. 1,4
C. 2,3
D. 2,4
Answer: Option B
39.The library function used to find the last occurrence of a character in a string
is
A. strnstr()
B. laststr()
C. strrchr()
D. strstr()
Answer: Option C
#include <string.h>
#include <stdio.h>
int main(void)
if (ptr)
122
printf("The position of '%c' is: %d\n", c, ptr-text);
else
return 0;
41.If char=1, int=4, and float=4 bytes size, What will be the output of the
program ?
#include<stdio.h>
int main()
char ch = 'A';
return 0;
A. 1, 2, 4
B. 1, 4, 4
C. 2, 2, 4
D. 2, 4, 8
Answer: Option B
#include<stdio.h>
123
int main()
str[0]='K';
str = "Kanpur";
printf("%s", str+1);
return 0;
A. Kagpur, Kanpur
B. Nagpur, Kanpur
C. Kagpur, anpur
D. Error
Answer: Option D
124