Documente Academic
Documente Profesional
Documente Cultură
Program:-
Program is a collection of set of instructions to be executed to produce the
output using a special type of compiler or software.
Compiler:-
A compiler is a special program or software which is used to convert source
code to machine code. It is also used to detect errors at the time of compilation
of the program.
Algorithm:-
The programmer begins the programming process by analyzing the problem,
breaking it into manageable pieces, and developing a general solution for each
piece called an algorithm.
Flowchart:-
Flow chart is a visually presenting the flow of control through an information
processing systems, the operations performed within the system and the
sequence in which they are performed.
Algorithm:
Programming Language:-
The language which is used to represent the instructions of a program to execute
it is called a programming language.
Types:-
1. structural/procedural language:-it is based on structure or procedures.
ex-c
2. partial object oriented language:-which may or may not use the
concepts of oops to write a program.
ex-c++, php
3. fully object oriented language:-without the help of oops we cannot
write a program.
ex-java,.net
4. object based language:-based on objects.
ex-javascript, vbscript
Historical Development of Programming Languages:-
1. PASCAL - 1954
2. COBOL - 1957
3. ALGOL - 1960
4. CPL - 1963
5. BCPL - 1967
6. B - 1970
7. C - 1972
8. C++ - 1980
9. PYTHON – 1980-1990
10.JAVA - 1990
11..NET - 1991
12.PHP – 1993
Before c came to the market there exist many programming languages like
algol, cpl, bcpl, b. But Dennis Ritche found some problems in all these
languages. So he combines the best features of all these four languages and
develops a new language called C language. It was developed in the year of
1972 at AT & T(American Telegraph and Telecom) Bell Lab., USA.
Features of C:-
simple and easy to understand
flexible
platform independent(runs on any os)
error detection
efficient
Applications of C:-
used for software development
used for graphics and animation
used for fifth generation computer like robots
used for developing games
used for project design/development
used for real time development.
Elements of C(CH-2)
Characterset:-
It combines small alphabets, capital alphabets, digits and special symbols to
write a program.
a-z(ASCII code->97-122)
A-Z(ASCII code->65-90)
0-9(ASCII code->48-57)
Special symbols
ASCII- American Standard Code for Information Interchange
Constants:-
Constants are the fixed quantity whose value can never be change throughout
the program execution.
Types:-
1. Integer constant-
-may be +ve or –ve.
-range is -32768 to +32767
-size is 2 bytes
-examples are 9,-346
2. Float/real constant-
-may be +ve or -ve
-range is -3.4x1038 to _3.4x1038
-size is 4 bytes
-examples are 1/3,-4.89
3. Character/string constant-
-if it is a character constant then it must be enclosed within a pair
of single quote and size is one byte. ex-‘s’(1 byte)
-if it is a string constant then it must be enclosed within a pair of
double quote and size is not fixed. ex-“anil”(no range)
{ 7
printf(“%d\n”,i); 8
i++;
9
}
10
getch();
}
(38)Write a program to find the output of 12+22+32+....n2 ?
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,sum=0,n;
clrscr();
printf(“enter the range\n”);
scanf(“%d”,&n);
while(i<=n)
{
sum=sum+(i*i);
i++;
}
printf(“sum=%d”,sum);
getch();
}
(39)Write a program to input a number and check whether it is mirror or
not ?
#include<stdio.h>
#include<conio.h>
#include<math.h>
12 -> 144-> 441->21-> 12
void main()
{
int num,rev1,rev2,rem1,rem2,sqr,sqrt;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
sqr=power(num,2);
while(sqr!=0)
{
rem1=sqr%10;
rev1=rev1*10+rem1;
sqr=sqr/10;
}
sqrt=sqrt(rev1);
while(sqrt!=0)
{
rem2=sqrt%10;
rev2=rev2*10+rem2;
sqrt=sqrt/10;
}
if(rev2==num)
printf(“number is mirror”);
else
printf(“number is not mirror”);
getch();
}
(40)Write a program to input a number and check whether it is prime or
not ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i=1,counter=0;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
while(i<=num)
{
if(num%i==0)
counter++;
i++;
}
if(counter==2)
printf(“number is prime”);
else
printf(“number is not prime”);
getch();
}
(41)Write a program to find the reverse of a number ?
#include<stdio.h>
#include<conio.h> 123 -> 321
void main()
{
int num,rev=0,rem;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
while(num!=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
printf(“reverse value=%d”,rev);
getch();
}
(42)Write a program to input a number and check whether it is palindrome
or not ?
#include<stdio.h>
#include<conio.h>
void main() 121 -> 121
{
int num,rev=0,rem,temp;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
temp=num;
while(num!=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
if(temp==rev)
printf(“number is palindrome”);
else
printf(“number is not palindrome”);
getch();
}
(43)Write a program to convert a decimal number into its binary
equivalent ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rem,sum=0,prd=1;
clrscr();
printf(“enter a decimal number\n”);
scanf(“%d”,&num);
while(num!=0)
{
rem=num%2;
sum=sum+rem*prd;
prd=prd*10;
num=num/2;
}
printf(“binary equivalent=%d”,sum);
getch();
}
(44)Write a program to input a number and check whether it is strong
number or not ?
#include<stdio.h>
#include<conio.h> 145= 1!+4!+5!=1+120+24=145
void main()
{
int temp,num,fact,sum=0,rem;
clrscr();
printf(“enter a number\n”);
scanf(“%d”,&num);
while(num!=0)
{
fact=1;
rem=num%10;
while(rem!=0)
{
fact=fact*rem;
rem--;
}
sum=sum+fact;
num=num/10;
}
if(sum==temp)
printf(“number is strong”);
else
printf(“number is not strong”);
getch();
}
(45)Write a program to input a number and check whether it is perfect
number or not ?
#include<stdio.h>
#include<conio.h>
void main()
6=1+2+3=6
{
int num,sum=0,i=1;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
while(i<num)
{
if(num%i==0)
sum=sum+i;
i++;
}
if(sum==num)
printf(“number is perfect”);
else
printf(“number is not perfect”);
getch();
}
Write a program to input a number and check whether it is magic number
or not?
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rem,sum=0,temp;
clrscr();
printf(“enter a number\n”);
scanf(“%d”,&num);
temp=num;
while(num!=0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
s=sum*sum;
if(s==temp)
printf(“magic number”);
else
printf(“not”);
getch();
}
Write a program to find the smallest digit from a number?
#include<stdio.h>
#include<conio.h>
void main()
{
int num, temp = 0, small = 10;
printf("\nEnter A Number:\t");
scanf("%d", &num);
while(num > 0)
{
temp = num%10;
if(temp < small)
{
small = temp;
}
num = num / 10;
}
printf("\nSmallest Digit in the Integer: \t%d\n", small);
getch();
}
Write a program to input a number and check whether it is a perfect square
number or not?
#include<stdio.h>
#include<conio.h>
void main()
{
int num, temp, count = 1;
printf("\nEnter the Number:\t");
scanf("%d", &num);
temp = num;
while(temp > 0)
{
temp = temp - count;
count = count + 2;
}
if(temp == 0)
{
printf("\n%d is a Perfect Square\n", num);
}
else
{
printf("\n%d is Not a Perfect Square\n", num);
}
getch();
}
2. Do-while loop:-
It is otherwise called as bottom-tested loop or post-tested loop or exit
control loop. in this type of looping statement first the statements are get
executed and printed after that the condition is checked. if the condition is
false then no statement is executed or printed.
syn:- variable value initialization;
do
{
statement(s);
incr/decr;
}
while(condition);
print statement(s);
(46)Write a program to print the series like 11,22,33,....,99 ?
#include<stdio.h>
#include<conio.h>
void main()
{
int i=11;
clrscr();
do
{
printf(“%d\n”,i);
i=i+11;
}
while(i<=99);
getch();
}
(47)Write a program to input an integer number and find the sum of its
digits ?
#include<stdio.h>
#include<conio.h>
123=1+2+3=6
void main()
{
int sum=0,num,rem;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
do
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
while(num!=0);
printf(“sum of digits=%d”,sum);
getch();
}
(48)Write a program to input a number and find its factorial value ?
#include<stdio.h>
#include<conio.h>
void main()
5!=5x4x3x2x1=120
{
int fact=1,num;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
do
{
fact=fact*num;
num--;
}
while(num!=0);
printf(“factorial value=%d”,fact);
getch();
}
(49)Write a program to input a number and check whether it is armstrong
or not ?
#include<stdio.h>
3 3 3
#include<conio.h>153=1 +5 +3 =1+125+27=153
void main()
{
int sum=0,num,rem,temp;
clrscr();
printf(“enter a number”);
scanf(“%d”,&num);
temp=num;
do
{
rem=num%10;
sum=sum+(rem*rem*rem);
num=num/10;
}
while(num!=0);
if(temp==sum)
printf(“number is armstrong”);
else
printf(“number is not armstrong”);
getch();
}
3. For loop:-
It is the simplest type of looping statement because here all the three parts
of the loop written in one line; so it reduce the line of codes.
syn:- for(initialization; condition; incr/decr)
(50)Write a program to print the series like 50,45,40,....,5 ?
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=50;i>=5;i=i-5)
{
printf(“%d\n”,i);
}
getch();
}
(51)Write a program to find the hcf and lcm of two integer number ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,hcf,lcm,i;
clrscr();
printf(“enter two number”);
scanf(“%d%d”,&num1,&num2);
for(i=1;i<=num1,i<=num2;i++)
{
if(num1%i==0&&num2%i==0)
hcf=i;
}
lcm=(num1*num2)/hcf;
printf(“hcf value=%d\n lcm value=%d”,hcf,lcm);
getch();
}
(52)Write a program to find n fibonacci numbers starting from 0 ?
#include<stdio.h>
#include<conio.h>
void main()
0,1,1,2,3,5,8,13,21,34,...
{
int num,prev=0,curr=1,next=0,i;
clrscr();
printf(“enter the range”);
scanf(“%d”,&num);
printf(“%d\n”,prev);
printf(“%d\n”,curr);
for(i=1;i<=num-2;i++)
{
next=prev+curr;
printf(“%d\n”,next);
prev=curr;
curr=next;
}
getch();
}
Write a program to print Lucas series ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
int i,n;
printf("Enter the Limit:\t");
scanf("%d", &n);
a=2;
b=1;
for(i=0;i <n;i++)
{
printf("%d\t",a);
c=a+b;
a=b;
b=c;
}
getch();
}
(53)Write a program to find sum of all the even numbers and product of all
odd numbers in between 1 to 10?
#include<stdio.h>
#include<conio.h>
void main()
{
int i,even=0,odd=1;
clrscr();
for(i=1;i<=10;i++)
{
if(i%2==0)
even=even+i;
else
odd=odd*i;
}
printf(“even sum=%d\n odd product=%d”,even,odd);
getch();
}
(54)Write a program to print all the prime numbers between 1 and 100 ?
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,c=0;
clrscr();
for(i=1;i<=100;i++)
{
for(j=1;j<=i;j++)
if(i%j==0)
c++;
if(c==2)
printf(“%d\n”,i);
}
getch();
}
Nested for:-
When one or more than one for loops are nested inside another for loop then it
is called nested for loop.
(55)Write a program for pyramid series ?
#include<stdio.h> 1
#include<conio.h>
void main() 1 2
{ 1 2 3
int i,j,n;
1 2 3 4
clrscr();
printf(“enter the row size\n”); 1 2 3 4 5
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf(“%d”,j);
printf(“\n”);
}
getch();
}
(56)Write a program for pyramid series ?
#include<stdio.h> 1 2 3 4 5
#include<conio.h>
1 2 3 4
void main()
1 2 3
1 2
1
{
int i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
printf(“%d”,j);
printf(“\n”);
}
getch();
}
(57)Write a program for pyramid series ?
#include<stdio.h> A
#include<conio.h>
A B
void main()
{ A B C
int i,j; A B C D
clrscr();
A B C D E
for(i=65;i<=69;i++)
{
for(j=65;j<=i;j++)
printf(“%c”,i);
printf(“\n”);
}
getch();
}
(58)Write a program for pyramid series ?
#include<stdio.h>
*
#include<conio.h>
* * *
void main()
{ * * * * *
int i,j,k=1; * * * * * * *
clrscr(); * * * * * * * * *
for(i=1;i<=5;i++,k=k+2)
{
for(j=i;j<=4;j++)
printf(“ “);
for(j=1;j<=k;j++)
printf(“*”);
printf(“\n”);
}
getch();
}
(59)Write a program to print floyd triangle upto five rows ?
#include<stdio.h>
#include<conio.h> 1
void main()
2 3
{
int i,j,k=1; 4 5 6
clrscr(); 7 8 9 10
for(i=1;i<=5;i++)
11 12 13 14 15
{
for(j=1;j<=i;j++)
{
printf(“%d”,k);
k++;
}
printf(“\n”);
}
getch();
}
(60)Write a program to generate pascal triangle upto five rows ?
#include<stdio.h> 1
#include<conio.h> 1 1
void main()
1 2 1
{
1 3 3 1
int i,j,space,coef=1;
clrscr(); 1 4 6 4 1
for(i=1;i<=5;i++)
{
for(space=1;space<=5-i;space++)
printf(“ “);
for(j=0;j<=i;j++)
{
if(j==0||i==0)
coef=1;
else
coef=coef*(i-j+1)/j;
printf(“%d”,coef);
}
printf(“\n”);
}
getch();
}
Array(CH-4)
Array is a deriver datatype in c programming language. It is the collection of
homogeneous data or similar type of data. The concept of array says that we can
store more than one element/value of similar type inside a single variable by
defining a subscript or size. All the array elements are accessed by the help of
an unique index number which is by default starts from zero but we can start it
from any other number.
Types:-
1. Single Dimensional Array/One Dimensional Array(1d)
2. Double Dimensional Array/Two Dimensional Array(2d)
3. Multidimensional Array(3d)
1. 1d:-
Whenever the array elements are stored inside a single variable by
defining a single subscript or size then it is called as single dimensional
array.
syn:-<data-type> <variable-name>[size];
a. compile time array:-
(61)Write a program to display a compile time array ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={23,6,90,1,56};
clrscr();
printf(“first element=%d\n”,a[0]);
printf(“second element=%d\n”,a[1]);
printf(“third element=%d\n”,a[2]);
printf(“fourth element=%d\n”,a[3]);
printf(“fifth element=%d\n”,a[4]);
getch();
}
(62)Write a program to display a compile time array in reverse order?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={23,6,90,1,56};
clrscr();
printf(“first element=%d\n”,a[4]);
printf(“second element=%d\n”,a[3]);
printf(“third element=%d\n”,a[2]);
printf(“fourth element=%d\n”,a[1]);
printf(“fifth element=%d\n”,a[0]);
getch();
}
b. runtime array:-
(63)Write a program to display a 1d array at runtime?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,n;
clrscr();
printf(“enter range\n”);
scanf(“%d”,&n);
printf(“enter elements into the array\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(elements are\n”);
for(i=0;i<n;i++)
{
printf(“%d\n”,a[i]);
}
getch();
}
(64)Write a program to display all the array elements in ascending order ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],num,i,temp,j;
clrscr();
printf(“enter the range of the array\n”);
scanf(“%d”,&num);
printf(“enter the elements\n”);
for(i=0;i<num;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<num-1;i++)
for(j=i+1;j<num;j++)
{
if(a[i]>a[j])
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf(“elements in sorted order are\n”);
for(i=0;i<num;i++)
{
printf(“%d\n”,a[i]);
}
getch();
}
(65)Write a program to find the largest and smallest element present inside
the array ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],num,i,large,small;
clrscr();
printf(“enter the range of the array\n”);
scanf(“%d”,&num);
printf(“enter the elements\n”);
for(i=0;i<num;i++)
{
scanf(“%d”,&a[i]);
}
large=a[0];
for(i=0;i<num;i++)
{
if(a[i]>large)
large=a[i];
}
small=a[0];
for(i=0;i<num;i++)
{
if(a[i]<small)
small=a[i];
}
printf(“largest element is=%d\n”,large);
printf(“smallest element is%d”,small);
getch();
}
(66)Write a program to find the two-digited element present inside the
array ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],num,i,sum=0;
clrscr();
printf(“enter the range of the array\n”);
scanf(“%d”,&num);
printf(“enter the elements\n”);
for(i=0;i<num;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<num;i++)
{
if(a[i]>9&&a[i]<100)
sum=sum+a[i];
}
printf(“sum=%d”,sum);
getch();
}
(67)Write a program to search for an element using linear search ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,num,counter;
clrscr();
printf(“enter the elements\n”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
}
printf(“enter the element to search\n”);
scanf(“%d”,&num);
for(i=0;i<num;i++)
{
if(a[i]==num)
counter=1;
}
if(counter==1)
printf(“elements found”);
else
printf(“element not found”);
getch();
}
(68)Write a program to display all the array elements in reverse order ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,num;
clrscr();
printf(“enter range of array\n”);
scanf(“%d”,&num);
printf(“enter the elements\n”);
for(i=0;i<num;i++)
{
scanf(“%d”,&a[i]);
}
for(i=num-1;i>=0;i--)
{
printf(“%d\n”,a[i]);
}
getch();
}
(69)Write a program to insert an element into an array ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,p,k;
clrscr();
printf(“enter the ranf of the array\n”);
scanf(“%d”,&n);
printf(“enter elements into the array\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“enter the location to enter \”);
scanf(“%d”,&p);
printf(“enter the element to insert\n”);
scanf(“%d”,&k);
for(i=n-1;i>=p;i--)
{
a[i+1]=a[i];
a[p-1]=k;
}
printf(array elements are\n”);
for(i=0;i<n;i++)
{
printf(“%d\n”,a[i]);
}
getch();
}
(70)write a program to delete an element from an array ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,p;
clrscr();
printf(“enter the ranf of the array\n”);
scanf(“%d”,&n);
printf(“enter elements into the array\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“enter the location to delete \”);
scanf(“%d”,&p);
if(p>=n+1)
printf(“deletion not possible”);
else
for(i=p-1;i<n-1;i++)
{
a[i]=a[i+1];
}
printf(array elements are\n”);
for(i=0;i<n;i++)
{
printf(“%d\n”,a[i]);
}
getch();
}
2. 2d:-
Whenever the array elements are stored inside a single variable by
defining two subscript or size then it is called as double dimensional
array.
syn:-<data-type> <variable-name>[row-size][column-size];
(71)Write a program to display a 3x3 matrix ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf(“enter elements into the matrix\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
printf(“\n”);
}
printf(elements are\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d”,a[i][j]);
}
printf(“\n”);
}
getch();
}
(72)Write a program to find the transpose of 3x3 matrix ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf(“enter the matrix\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
printf(“\n”);
}
printf(“matrix transpose is\n”);
for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
{
printf(“%d”,a[j][i]);
}
printf(“\n”);
}
getch();
}
(73)Write a program to display the diagonal elements of a 3x3 matrix ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf(“enter the matrix\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
printf(“\n”);
}
printf(“diagonal elements are\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
printf(“%d”,a[i][j]);
}
printf(“\n”);
}
getch();
}
(74)Write a program to input mxn matrix and find the product of all the
matrix elements ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100][100],i,j,m,n,prd=0;
clrscr();
printf(“enter the row size\n”);
scanf(“%d”,&m);
printf(“enter the column size\n”);
scanf(“%d”,&n);
printf(“enter the matrix\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
printf(“\n”);
}
printf(“product of all elements are\n”);
for(j=0;j<m;j++)
{
for(i=0;i<n;i++)
{
prd=prd*a[i][j];
}
}
printf(“product is=%d”,prd);
getch();
}
3. md:-
Whenever the array elements are stored inside a single variable by
defining more than two subscript or size then it is called as multi
dimensional array.
syn:-<data-type> <variable-name>[size1]...[sizen];
(75)Write a program to display a 3x3x3 matrix ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3][3],i,j,k;
clrscr();
printf(“enter elements into the matrix\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
scanf(“%d”,&a[i][j][k]);
}
printf(“\n”);
}
printf(“\n”);
}
printf(elements are\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
printf(“%d”,a[i][j][k]);
}
printf(“\n”);
}
printf(“\n”);
}
getch();
}
String(CH-5)
String is a collection of array of characters. Whenever we want to store more
than one character inside a single variable enclosed within a pair of double
quote then the concept of string is used and it is accessed by the help of “%s”
format specifier. Just like array string is also accessed by the help of unique
index number which is by default starts from zero. After the last character of
string a null character that is ‘\0’ must be associated to terminate the string
reading. For string we use a header file called <string.h> to access some
predefined string library functions. For string output we can alse use a function
called puts() where as it is necessary to use gets() for taking string as input
inplace of scanf(), because scanf() is limited to a single word.
syn:- <datatype> <stringvaribalname>[size];
ex:- char str[30];
(76)Write a program to input a string and display it at compile time ?
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20]=”anil”;
clrscr();
printf(“%s”,name);
getch();
}
(77)Write a program to display a string at runtime ?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[30];
clrscr();
puts(“enter your name\n”);
gets(name);
printf(“%s”,name);
getch();
}
String Library Functions:-
1. strlen():- used to calculate the length of the string and returns an integer
value.
syn:- strlen(stringvariable);
ex:- char a[20]=”computer”;
strlen(a); o/p- 8
2. strrev():- used to reverse a string.
syn:- strrev(stringvariable);
ex:- char a[10]=”computer”;
strrev(a); o/p-retupmoc
3. strcat():- used to concatenate/join two different string as a single string.
syn:- strcat(str1,str2);
ex:- char a[20]=”good”;
char b[20]=”morning”;
strcat(a,b) o/p-goodmorning
4. strcpy():- to copy one string data into another.
syn:- strcpy(str1,str2);
ex:- char a[10]=”hello”;
char b[10];
strcpy(a,b);
5. strcmp():- to compare whether two strings are equal or not.
syn:- if(strcmp(str1,str2)==0)
ex:- char a[10]=”hello”;
char b[10]=”helo”;
if(strcmp(a,b)==0) o/p-true
6. strupr():- convert all the lowercase characters into its equivalent
uppercase characters.
syn:- strupr(str);
ex:- char a[10]=”hello”;
strupr(a); o/p-HELLO
7. strlwr():- used to convert all the uppercase characters into its equivalent
lowercase characters.
syn:- strlwr(str);
ex:- char a[10]=”HELLO”;
strlwr(a); o/p-hello
8. strcmpi():- same as strcmp() but avoid the case.
9. strncat():- appends a portion of string to another.
10. strncpy():- copies a given number of characters of one string to another.
(78)Write a program to input your full name and count the number of
characters present without using string library function ?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[30];
int c=0,i;
clrscr();
puts(“enter your full name\n”);
gets(name);
for(i=0;name[i]!=’\0’;i++)
{
c++;
}
printf(“number of characters=%d”,c);
getch();
}
(79)Write a program to input your full name and display it in reverse order
without using string library function ?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[30];
int i,c=0;
clrscr();
puts(“enter your full name\n”);
gets(name);
for(i=0;name[i]!=’\0’;i++)
{
c++;
}
for(i=c-1;i>=0;i--)
{
printf(“%c”,name[i]);
}
getch();
}
(80)Write a program to input your full name and display it in shortcut ?
#include<stdio.h>
#include<conio.h>
#include<string.h>
Anil Kumar Meher -> A.K.M
void main()
{
char name[100];
int i;
clrscr();
puts(“enter your full name\n”);
gets(name);
printf(“%c”,name[0]);
for(i=1;name[i]!=’\0’;i++)
{
if(name[i]==32)
printf(“.%c”,name[i+1]);
}
getch();
}
(81)Write a program to display your first name in pyramid like structure ?
#include<stdio.h> A
#include<conio.h>
A N
#include<string.h>
void main() A N I
{ A N I L
char name[20];
int i,j,l;
clrscr();
puts(“enter your first name\n”);
gets(name);
l=strlen(name);
for(i=1;i<=l;i++)
{
for(j=0;j<=i;j++)
printf(“%c”,a[j]);
printf(“\n”);
}
getch();
}
(82)Write a program to input your first name in lower case and display it
in upper case without using string library function ?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20];
int i;
clrscr();
puts(“enter your first name\n”);
gets(name);
for(i=0;name[i]!=’\0’;i++)
{
if(name[i]>=97&&name[i]<=122)
name[i]=name[i]-32;
printf(“%c”,name[i]);
}
getch();
}
(83)Write a program to input your email id and count the number of
alphabets, disgts and special symbols present inside your email id ?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char email[20];
int i,al=0,dg=0,sp=0;
clrscr();
puts(“enter your email id\n”);
gets(email);
for(i=0;name[i]!=’\0’;i++)
{
if((email[i]>=97&&email[i]<=122)||(email[i]>=65&&email[i]<=90))
al++;
else if(email[i]>=48&&email[i]<=57)
dg++;
else
sp++;
}
printf(“no. of alphabets=%d\n no. of digits=%d\n no. of special
symbols=%d”,al,dg,sp);
getch();
}
(84)Write a program to input your first name in first string, middle name
in second string and last name in third string and display the name as a
single string using library function ?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char fn[50];
char mn[20],
char ln[20];
clrscr();
puts(“enter your first name\n”);
gets(fn);
strcat(fn,” “);
puts(“enter your middle name\n”);
gets(mn);
strcat(fn,mn);
strcat(fn,” “);
puts(“enter your last name\n”);
gets(ln);
strcat(fn,ln);
printf(“%s”,fn);
getch();
}
(85)Write a program to input your first name and count the total number
of vowels and consonents present inside your name ?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[30];
int i,v=0,c=0;
clrscr();
puts(“enter your first name\n”);
gets(name);
for(i=0;name[i]!=’\0’;i++)
{
if(name[i]==’a’||name[i]==’a’||name[i]==’e’||name[i]==’e’||name[i]==’i’||name[
i]==’i’|| name[i]==’o’|| name[i]==’o’|| name[i]==’u’||name[i]==’u’)
v++;
else
c++;
}
printf(“no. of vowels present=%d\n”,v);
printf(“no. of consonents present=%d”,c);
getch();
}
(86)Write a program to input a string and check whether it is palindrome
or not without using string library functions ?
#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
int i,l=0,p=0;
clrscr();
printf(“enter a string\n”);
gets(str);
for(i=0;i<50;i++)
{
if(str[i]==’\0’)
break;
}
l++;
}
for(i=0;i<l;i++)
{
if(str[i]==str[l-i])
{
p++;
}
}
if(l==p)
printf(“string is palindrome”);
else
printf(“not”);
getch();
}
Pointer(CH-6)
Pointer is a derived datatype in c. Pointer in C language is a variable that
stores/points the address of another variable. A pointer in c is used to allocate
memory dynamically i.e. at run time. The pointer variable might be belonging
to any of the data type such as int, float, char, double, short etc. A pointer
allocates two bytes of memory space at a time to a variable and the memory is
always in numeric format. Pointers are accessed by the help of two operators.
Arithmetic pointer:-
We can perform arithmetic operations on pointer variable just as you can a
numeric value. The arithmetic operations on pointer variable effects the
memory address pointed by pointer.
Valid pointer arithmetic operations
adding a number to pointer.
subtracting a number form a pointer.
incrementing a pointer.
decrementing a pointer.
subtracting two pointers.
comparison on two pointers.
Invalid pointer arithmetic operations
addition of two pointers.
division of two pointers.
multiplication of two pointers.
Declaration of pointer:-
syn:- <datatype> *<pointervariable>;
ex:- int *p;
(100)Write a simple program for pointer ?
#include<stdio.h>
#include<,conio.h>
void main()
{
int a=52,*p,**q;
clrscr();
p=&a;
q=&p;
printf(“value of a=%d\n”,a);
printf(“value of a=%d\n”,&a);
printf(“value of a=%d\n”,p);
printf(“value of a=%d\n”,*p);
printf(“value of a=%d\n”,q);
printf(“value of a=%d\n”,**q);
getch();
}
(101)Write a program to display array elements using pointer ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf(“enter elements\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,a+i);
}
printf(“elements are\n”);
for(i=0;i<5;i++)
{
printf(“%d\n”,*(a+i));
}
getch();
}
(102)Write a program to display all array elements in reverse order using
pointer ?
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i,a[100];
int *ptr;
clrscr();
ptr=&a[0];
printf("\nenter the size of array : ");
scanf("%d", &n);
printf("\nenter elements into array: ");
for(i=0;i<n;i++)
{
scanf("%d",ptr);
ptr++;
}
ptr=&a[n-1];
printf("\nelements of array in reverse order are :");
for (i=n-1;i>=0;i--)
{
printf("%d%d",i,*ptr);
ptr--;
}
getch();
}
(103)Write a program to display a string using pointer ?
#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
char *ptr;
clrscr();
puts(“enter a string\n”);
gets(str);
ptr=str;
printf(“the string is\n”);
while(*ptr!=’\0’)
{
printf(“%c”,*ptr++);
}
getch();
}
(104)Write a program to display a string in reverse order using pointer?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *s;
int l,i;
clrscr();
puts(“enter a string\n”);
gets(s);
l=strlen(s);
printf(“string in reverse order is\n”);
for(i=l;i>=0;i--)
{
printf(“%c”,*(s+i));
getch();
}
Dynamic Memory Allocation(DMA):-
In array we saw that memory is allocated to the array variable is static. So the
static allocation of memory creates problem inside the program as its results in
memory shortage or memory wastage. So in order to resolve this problem c
programming releases a concept called dynamic memory allocation; it means
memory is allocated to the variable at the time of running a program. Using this
concept we can allocate, deallocate memory inside the program by the help of
some pre-defined functions calloc(), malloc(), realloc() and free(). All these
functions are come under the headed file <stdlib.h>.
1. calloc():- calloc() allocates the memory and also initializes the allocates
memory block to zero. If we try to access the content of these blocks
then we’ll get 0.
syn:- pointer-variable=(datatype *)calloc(size,sizeof(datatype));
(105)Write a program to display array elements in pointer using calloc() ?
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int n,i;
int *p;
clrscr();
printf(“enter the range\n”);
scanf(“%d”,&n);
p=(int *)calloc(n,sizeof(int));
printf(“enter the elements\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,p+i);
}
printf(“the elements are\n”);
for(i=0;i<n;i++)
{
printf(“%d”,*(p+i));
}
free(p);
getch();
}
2. malloc():- malloc() allocates memory block of given size (in bytes) and
returns a pointer to the beginning of the block. malloc() doesn’t initialize
the allocated memory. If we try to acess the content of memory block
then we’ll get garbage values.
syn:- pointer-variable=(datatype *)malloc(size*sizeof(datatype));
(106)Write a program to enter some elements and find their summation
value using malloc()?
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
void main()
{
int n,i,*p,s=0;
clrscr();
printf(“enter the range\n”);
scanf(“%d”,&n);
p=(int *)malloc(n*sizeof(int));
if(p==null)
{
printf(“memory not allocated”);
exit(0);
}
printf(“enter the elements\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,p+i);
s+=*(p+i);
}
printf(“sum=%d”,s);
}
free(p);
getch();
}
3. realloc():- used to reallocate new memory to the variable after allocating
through calloc() or malloc().
Syn:- pointer-variable=realloc(pointer-variable,new-size);
4. free():- used to free the memory spaces which are previously allocated to
the variable by the help of all the above three functions.
Syn:- free(pointer-variable);
(107)write a program for realloc() ?
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int n1,n2,i,*p;
clrscr();
printf(“enter the size of array\n”);
scanf(“%d”,&n1);
p=(int *)malloc(n*sizeof(int));
printf(“address of previously allocated memory\n”);
for(i=0;i<n1;i++)
{
printf(“%u\n”,p+i);
}
printf(“enter new size of array\n”);
scanf(“%d”,&n2);
p=realloc(p,n2);
printf(“address of newly allocated memory\n”);
for(i=0;i<n2;i++)
{
printf(“%u\n”,p+i);
}
free(p);
getch();
}
Function and Storage Classes(CH-7)
What are functions?
Functions are the self-contained block of codes which are used to perform
specific task inside the program. A function is used to convert large and
complex programs into simple one. A function is always followed by a pair of
( ) parenthesis.
categories:-
1. pre-define/built-in functions:- The functions which are already
available inside c library and carry a special meaning for the compiler are
called pre-defined or built-in functions. We cannot change the name of
these functions.
Ex:- clrscr(), getch(), printf(), scanf(), sqrt() etc.
2. user-define functions:- The functions which are declared and defined by
the user at the time of writing the program are called user-defined
functions.
Ex:- void abc(), void anil() etc.
Components of user-defined functions:-
1. return-type:- it states that whether a function returns a value or not
means a function if preceeded by void return type then it doesnot return
any value, it only prints the output but if a function uses other return type
like int, float, double etc. other than void then it must return a value to the
function.
2. function-name:- it is an user-defined name created by the user for
declaring a function followed by a pair of parenthesis.
3. arguments:- these are just like variables which take value as input for the
function by specifying their datatype.
4. body:- it contains all the operations related to the function which the user
want to do using the arguments.
5. return keyword:- a return keyword is used when the function returns a
value to the program and the function must associated with a return type
rather than void.
Syntax for creating user-defined function:-
<return-type> function-name(type argument1,...type argumentn)
{
//body
return(value/variable);
}
Ex:-
int x;
int abc(int a,int b)
{
int c;
c=a+b;
return(c);
}
Scope of variable:-
1. Local variable:- The variable which is declared inside a particular block
of a program and works for that block only then it is called as local
variable. Its scope is limited to that block and when it comes out of that
block it will dead.
2. Global variable:- The variable which is declared outside all blocks of the
program and can be accessed anywhere inside the program is called
global variable its life will be expired when the program terminated or
comes to the end.
Actual arguments and Formal arguments:-
1. Actual:- Arguments which are mentioned in the function call is known as
the actual argument. Actual arguments can be constant, variables,
expressions etc.
Ex:- swap(5,4); or swap(a,b);
2. Formal:- Arguments which are mentioned in the definition of the function
is called formal arguments. Formal arguments are very similar to local
variables inside the function. Just like local variables, formal arguments
are destroyed when the function ends.
Ex:- int fact(int n)
{
//body
}
An user-defined function can be called in four different ways.
a. function with argument and with return type
b. function with argument and without return type
c. function without argument and with return type
d. function witout argument and without return type
1. function with argument and with return type:-
(87)Write a program to find the reverse of a number using function with
argument and with return type ?
#include<stdio.h>
#include<conio.h>
int reverse(int n);
void main()
{
int n,r;
clrscr();
printf(“enter a number”);
scanf(“%d”,&n);
r=reverse(n);
printf(“reverse value=%d”,r);
getch();
}
int reverse(int n)
{
int r=0,d;
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
return(r);
}
2. function with argument and without return type:-
(88)Write a program to find the factorial of a number using function with
argument and without return type ?
#include<stdio.h>
#include<conio.h>
void facto(int n);
void main()
{
int n;
clrscr();
printf(“enter a number”);
scanf(“%d”,&n);
facto(n);
getch();
}
void facto(int n)
{
int f=1;
while(n!=0)
{
f=f*n;
n--;
}
printf(“factorial value=%d”,f);
}
3. function without argument and with return type:-
(89)Write a program to find the sum of a digits of a number using function
without argument and with return type ?
#include<stdio.h>
#include<conio.h>
int sum();
void main()
{
int s;
clrscr();
s=sum();
printf(“sum of digits=%d”,s);
getch();
}
int sum()
{
int s=0,d,n;
printf(“enter a number”);
scanf(“%d”,&n);
while(n!=0)
{
d=n%10;
s=s+d;
n=n/10;
}
return(s);
}
4. function without argument and without return type:-
(90)Write a program to convert a decimal number into its binary
equivalent using function without argument and without return type ?
#include<stdio.h>
#include<conio.h>
void convert();
void main()
{
clrscr();
convert();
getch();
}
void convert()
{
int s=0,p=1,d,n;
printf(“enter a decimal number”);
scanf(“%d”,&n);
while(n!=0)
{
d=n%2;
s=s+d*p;
p=p*10;
n=n/2;
}
printf(“binary equivalent=%d”,s);
}
Recursion:-
When a function calls itself again and again then it is called recursion. It works
just like loop. Means if we are not using any loop then we can use recursive
function to handle the program.
(91)Write a program to find the factorial of a number using recursion ?
#include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
int f,n;
clrscr();
printf(“enter a number\n”);
scanf(“%d”,&n);
f=fact(n);
printf(“factorial value=%d”,f);
getch();
}
int fact(int n)
{
if(n==0)
return(1);
else
return(n*fact(n-1));
}
(92)Write a program for 1+2+3+....+n using recursion ?
#include<stdio.h>
#include<conio.h>
int sum(int n);
void main()
{
int s,n;
clrscr();
printf(“enter a number\n”);
scanf(“%d”,&n);
s=sum(n);
printf(“summation value=%d”,s);
getch();
}
int sum(int n)
{
if(n==0)
return(1);
else
return(n+sum(n-1));
}
(93)Write a program to find fibonacci series using recursion ?
#include<stdio.h>
#include<conio.h>
int fibo(int n);
void main()
{
int n,r;
clrscr();
printf(“enter the range\n”);
scanf(“%d”,&n);
r=fibo(n);
printf(“%d\n”,r);
}
int fibo(int n)
{
if(n==0)
return 0;
else
return(fibo(n-1)+fibo(n-2));
}
Write a program to find the sum of digits of a number using recursion ?
#include<stdio.h>
#include<conio.h>
int sum(int x);
void main()
{
int res, num;
printf("\nEnter a Number:\t");
scanf("%d", &num);
res = sum(num);
printf("\nSum of Digits : \t%d\n", res);
getch();
}
int sum(int x)
{
if(x == 0)
{
return x;
}
else
{
return (x%10 + sum(x/10));
}
}
Calling a function:-
A function in C programming can be called in two different ways.
1. Call by value method:- When calling block passes its argument values
to called block or vice-versa directly then it is called call by value
method.
(94)Write a program to swap two integer number using call by value
method?
#include<stdio.h>
#include<conio.h>
void swap(int a,int b);
void main()
{
int a,b;
clrscr();
printf(“enter two number\n”);
scanf(“%d%d”,&a,&b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;
printf(“after swapping value of a=%d\n after swapping value of b=%d”,a,b);
}
2. Call by reference method:- When calling method passes its argument’s
address to called method or vice-versa then it is called call by
reference/address method. For this we require two different operators.
i. Address of operator (&):- The ‘&’ is an unary operator in c which
returns the memory address of the passed operand. This is also known as
address of operator. a pointer contains the memory address of some
object.
ii. Value at address of operator (*):- The ‘*’ is an unary operator which
returns the value of object pointer by a pointer variable. It is known as
value of operator. It is also used for declaring pointer variable.
(95)Write a program to swap two integer number using call by reference
method?
#include<stdio.h>
#include<conio.h>
void swap(int *a,int *b);
void main()
{
int a,b;
clrscr();
printf(“enter two number\n”);
scanf(“%d%d”,&a,&b);
swap(&a,&b);
getch();
}
void swap(int *a,int *b)
{
int c;
c=*a;
*a=*b;
*b=c;
printf(“after swapping value of a=%d after swapping value of b=%d”,*a,*b);
}
Storage Classes:-
Storage classes decode in which area of memory the variable values are stored.
It also tells about the scope, life time and initial value of the variable to be used
inside the program. A storage class is created by the help of keywords
succeeded by variable name.
Types:-
1. Automatic storage class:- Created by the help of the keyword auto. It is
the default storage class of c programming.
Features:-
1. Memory:- inside main memory
2. Default initial value:- garbage value
3. Scope:- local means inside the same block
4. Lifetime:- its life will be expires when it comes out of the block where
it is declared.
Ex:- int a=9; (or) auto int a=9;
(96)Write a program for auto storage class ?
#include<stdio.h>
#include<conio.h>
void main()
{
auto int a=9; //int a=9;
clrscr();
printf(“%d”,a);
getch();
}
2. Register storage class:- Created by the help of the keyword register.
Features:-
1. Memory:- inside register memory.
2. Default initial value:- garbage value.
3. Scope:- local means limited to a particular block.
4. Lifetime:- its life will be expires when it comes out of the block where
it is declared.
(97)Write a program for register storage class ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5];
register int i;
clrscr();
a[0]=2;
a[1]=28;
a[2]=0;
a[3]=67;
a[4]=90;
for(i=0;i<5;i++)
{
printf(“%d\n”,a[i]);
}
getch();
}
3. Static storage class:- Created by the help of the keyword static.
Features:-
1. Memory:- inside main memory
2. Default initial value:- 0
3. Scope:- global
4. lifetime:- its life will be expires when the program comes to the end.
(98)Write a program for static storage class ?
#include<stdio.h>
#include<conio.h>
void increment(void);
void main()
{
clrscr();
increment();
increment();
increment();
increment();
getch();
}
void increment(void)
{
static int i=0;
printf(“%d\n”,i);
i++;
}
4. External storage class:- Created by the help of the keyword extern.
Features:-
1. Memory:- inside main memory
2. Default initial value:- 0
3. Scope:- global
4. Lifetime:- its life will be expires when the program comes to the end.
(99)Write a program for extern storage class?
#include<stdio.h>
#include<conio.h>
int x=10;
void main()
{
extern int y;
clrscr();
printf(“x=%d\n”,x);
printf(“%d”,y);
getch();
}
int y=50;
Structure(CH-8)
It is a user-defined datatype created by the help of the keyword struct. Structure
is used to store, retrieve, and manipulate records inside the program. A structure
is the collection of heterogeneous or dissimilar type of data. All the structure
data are called data-members and are accessed by help of structure variable
using dot operator (.).
syn:- :- struct <structurename>
{
<datatype> datamember1;
.
.
<datatype> datamembern;
}<structurevariable>;
(108)Write a program to create a simple student structure and display all
the data ?
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[30];
float per;
};
void main()
{
struct student s;
clrscr();
printf(“enter student roll\n”);
scanf(“%d”,&s.roll);
fflush(stdin);
printf(“enter student name\n”);
gets(s.name);
printf(“enter student percentage\n”);
scanf(“%f”,&s.per);
printf(“student record is as follows\n”);
printf(“roll\t name\t per\n”);
printf(“------------------------------------\n”);
printf(“roll=%d\t name=%s\t per=%f”,s.roll,s.name,s.per);
getch();
}
Array of structure:-
Whenever we want to store/access more than one structure record from a single
structure program then we must use the concept of array of structure by defining
a subscript/size for the structure variable.
(109)Write a program to display n number of student records using array
of structure?
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[30];
int age;
};
void main()
{
struct student s[100];
int n,i;
clrscr();
printf(“enter range\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“enter student roll\n”);
scanf(“%d”,&s[i].roll);
fflush(stdin);
printf(“enter student name\n”);
gets(s[i].name);
printf(“enter student age\n”);
scanf(“%f”,&s[i].age);
}
printf(“student recordsare as follows\n”);
for(i=0;i<n;i++)
{
printf(“roll\t name\t per\n”);
printf(“------------------------------------\n”);
printf(“roll=%d\t name=%s\t per=%f\n”,s[i].roll,s[i].name,s[i].per);
}
getch();
}
Nested structure:-
When one or more than one structure is written inside another structure then it is
called as nested structure.
(110)Write a program to create dept structure and nested it inside emp
structure?
#include<stdio.h>
#include<conio.h>
struct dept
{
int did;
char dnm[10];
char dloc[20];
};
struct emp
{
int eid;
char enm[30];
float esal;
struct dept d;
};
void main()
{
struct emp e;
clrscr();
printf(“enter emp id\n”);
scanf(“%d”,&e.eid);
fflush(stdin);
printf(“enter emp name\n”);
gets(e.enm);
printf(“enter emp salary\n”);
scanf(“%f”,&e.esal);
printf(“enter dept id\n”);
scanf(“%d”,&e.d.did);
fflush(stdin);
printf(“enter dept name\n”);
gets(e.d.dnm);
fflush(stdin);
printf(“enter dept location\n”);
scanf(“%f”,&e.d.dloc);
printf(“employee database is as follows\n”);
printf(“------------------------------------------\n”);
printf(“emp id=%d\n”,e.eid);
printf(“emp id=%s\n”,e.enm);
printf(“emp id=%f\n”,e.esal);
printf(“emp id=%d\n”,e.d.did);
printf(“emp id=%s\n”,e.d.dnm);
printf(“emp id=%s\n”,e.d.dloc);
getch();
}
Pointer structure:-
When we want to process any information of structure through pointer then it is
called pointer structure. Here instead of accessing the structure data through the
structure variable, we access them by the help of pointer; so it is called as
pointer to structure. For this we use pointer to member access operator(->)
instead of dot operator(.) and pointer should store the address of structure
variable.
(111)Write a program to describe the concept of structure pointer?
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[30];
int per;
}s;
void main()
{
struct student *p;
clrscr();
printf(“enter student roll\n”);
scanf(“%d”,&s.roll);
fflush(stdin);
printf(“enter student name\n”);
gets(s.name);
printf(“enter student percentage\n”);
scanf(“%f”,&s.per);
printf(“student record is as follows\n”);
printf(“roll\t name\t per\n”);
printf(“------------------------------------\n”);
printf(“roll=%d\t name=%s\t per=%f”,p->roll,p->name,p->per);
getch();
}
Union(CH-9)
It is a user-defined data-type just like structure created by the help of the
keyword union. It is used to access data in the form of records just like structure
but the difference is that in case of union a single/same memory location is
allocated to all the data members, so that it is compulsory to display the data
members after each single entry.
syn:- union <union-name>
{
<data-type> datamember-1;
.
.
<data-type> datamember-n;
}<union-variable>;
(112)Write a program to create a simple customer data using union?
#include<stdio.h>
#include<conio.h>
union student
{
int roll;
char name[30];
int age;
};
void main()
{
union student s;
clrscr();
printf(“enter student roll\n”);
scanf(“%d”,&s.roll);
printf(“student roll=%d\n”,s.roll);
fflush(stdin);
printf(“enter student name\n”);
gets(s.name);
printf(“student name=%s\n”,s.name);
printf(“enter student age\n”);
scanf(“%f”,&s.per);
printf(“student age=%d”,s.age);
getch();
}
Difference between structure and union:-
Structure union
1. created by the help of struct keyword. 1. Created with union keyword.
2. allocate different-different memory 2. Allocates same memory space
space to structure members. to datamembers.
3. initial memory size 4 bytes. 3. Initial memory size is 2 bytes.
4. all the members are displayed after 4. One member is displayed after
input. one input and so on.
Enum(CH-10)
Stands for enumerated data-type, created by the help of the keyword enum. It is
an user-defined data-type which is used to access all the enumerators/elements
from the enum list and all the enumerators are by default assigned with an
integer value which is by default starts from zero but we can change it to any
other number.
syn:- enum <enumname>{enumerator1,........enumeratorn};
(113)Write a program for enum to display the day of your choice ?
#include<stdio.h>
#include<conio.h>
enum day{sun,mon,tue,wed,thu,fri,sat};
void main()
{
enum day d;
int choice;
clrscr();
printf(“enter your choice\n”);
scanf(“%d”,&choice);
d=n;
if(d==sun)
printf(“the day is sunday”);
else if(d==mon)
printf(“the day is monday”);
else if(d==tue)
printf(“the day is tuesday”);
else if(d==wed)
printf(“the day is wednesday”);
else if(d==thu)
printf(“the day is thursday”);
else if(d==fri)
printf(“the day is friday”);
else if(d==sat)
printf(“the day is saturday”);
else
printf(“wrong choice”);
getch();
}
(114)Write a program for enum with structure ?
#include<stdio.h>
#include<conio.h>
enum course
{
bca,
bba,
mca,
mba,
mtech
};
struct student
{
int roll;
char name[35];
enum course c;
};
void main()
{
struct student s;
int ch;
clrscr();
printf(“enter student roll no\n”);
scanf(“%d”,&s.roll);
fflush(stdin);
printf(“enter student name\n”);
gets(name);
printf(“enter student course choice\n”);
scanf(“%d”,&ch);
s.c=ch;
if(s.c==bca)
printf(“student registered for bca course”);
else if(s.c==bba)
printf(“student registered for bba course”);
else if(s.c==mca)
printf(“student registered for mca course”);
else if(s.c==mba)
printf(“student registered for mba course”);
else if(s.c==mtech)
printf(“student registered for mtech course”);
getch();
}
File handling(CH-11)
File:-
A file represents a sequence of bytes which is used to store information. C
programming language provides access on high level functions as well as low
level (OS level) calls to handle file on our storage devices.
Types:-
1. Text file:- Text files are the normal .txt files that you can easily create
using Notepad or any simple text editors. When you open those files,
you'll see all the contents within the file as plain text. You can easily edit
or delete the contents. They take minimum effort to maintain, are easily
readable, and provide least security and takes bigger storage space.
2. Binary file:- Binary files are mostly the .bin files in your computer.
Instead of storing data in plain text, they store it in the binary form (0's
and 1's). They can hold higher amount of data, are not readable easily and
provides a better security than text files.
File operation:-
1. declaring a file:-
syn:- FILE *<pointervariable>;
ex:- FILE *p;
2. opening a file:-
syn:- <filepointer>=fopen(“path with filename.ext”,”mode”);
ex:- p=fopen(“c:\abc.txt”,”r”);
3. closing a file:-
syn:- fclose(filepointer);
ex:- fclose(p);
Modes to open a file:-
1. ‘r’ mode:- Opens an existing text file for reading purpose.
2. ‘w’ mode:- Opens a text file for writing. If it does not exist, then a new
file is created. Here your program will start writing content from the
beginning of the file.
3. ‘a’ mode:- Opens a text file for writing in appending mode. If it does not
exist, then a new file is created. Here your program will start appending
content in the existing file content.
4. ‘r+’ mode:- Opens a text file for both reading and writing.
5. ‘w+’ mode:- Opens a text file for both reading and writing. It first
truncates the file to zero length if it exists, otherwise creates a file if it
does not exist.
6. ‘a+’ mode:- Opens a text file for both reading and writing. It creates the
file if it does not exist. The reading will start from the beginning but
writing can only be appended.
File I/O Functions:-
1. fgetc():- used to read a single character from a file.
Syn:- character-variable=fgetc(file-pointer);
2. fputc():- used to write a single character to a file.
Syn:- fputc(character-variable,file-pointer);
3. fgets():- used to read a string from a file.
Syn:- string-variable=fgets(file-pointer);
4. fputs():- used to write a string from a file.
Syn:- fputs(string-variable,file-pointer);
5. fprintf():- used to writes to output stream.
6. fscanf():-used to reads from output stream.
7. fread():- used to read binary data from a file.
Syn:- fread(&variable,sizeof(datatype),range,filepointername);
8. fwrite():- used to write binary data to a file.
Syn:- fwrite(variable,range,sizeof(datatype),filepointername);
9. rewind():-rewind sets the file position indicator for stream to the
beginning of the file.
Syn:- rewind(filepointername);
10.fseek():- sets the file position indicator for stream.
Syn:- fseek(filepointername,offsetvalue,seekconstant);
11.ftell():- returns the current file position for stream.
(115)Write a program to read a character from a file ?
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
file *p;
char ch;
clrscr();
p=fopen(“c:\abc.txt”,”r”);
if(p==null)
{
printf(“file not exist”);
exit(0);
}
while((ch=getc(p))!=eof)
{
printf(“%c”,ch);
}
fclose(p);
getch();
}
(116)Write a program to write a character to a file ?
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
file *p;
char ch;
clrscr();
p=fopen(“c:\abc.txt”,”w”);
if(p==null)
{
printf(“file not exist”);
exit(0);
}
printf(“enter a character”);
scanf(“%c”,&ch);
fputc(ch,p);
fclose(p);
getch();
}
(117)Write a program to copy the content of a file to another file ?
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
file *p,*q;
char ch;
clrscr();
p=fopen(“c:\abc.txt”,”r”);
if(p==null)
{
printf(“file not exist”);
exit(0);
}
q=fopen(“c:\xyz.txt”,”w”);
if(q==null)
{
printf(“file not exist”);
exit(0);
}
while((ch=getc(p))!=eof)
{
fputc(ch,q);
}
fclose(p);
fclose(q);
getch();
}
(118)Write a program to read a string from a file ?
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
FILE *p;
char str[50];
clrscr();
p=fopen(“c:\abc.txt”,”r”);
if(p==NULL)
{
printf(“file not exist”);
exit(0);
}
while(fgets(str,50,p)!=NULL)
{
printf(“%s”,str);
}
fclose(p);
getch();
}
(119)Write a program to write a string to a file ?
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
FILE *p;
char str[50];
clrscr();
p=fopen(“c:\abc.txt”,”w”);
if(p==NULL)
{
printf(“file not exist”);
exit(0);
}
printf(“enter a string”);
gets(str);
fprintf(p,”%s”,str);
fclose(p);
getch();
}
(120)Write a program to write a record to a file ?
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
struct customer
{
char fname[20],lname[20];
int ac;
float bal;
};
void main ()
{
FILE *p;
struct customer c;
clrscr();
p=fopen("c:\abc.txt","w");
if(p==NULL)
{
fprintf(“file not exist");
exit(0);
}
while (1)
{
printf("\nFirst Name");
scanf ("%s",c.fname);
if(strcmp(c.fname,"stop")== 0)
exit(0);
printf("\nLast Name");
scanf ("%s",c.lname);
printf("\nAccount Number");
scanf("%d", &c.ac);
printf("\nBalance");
scanf ("%f",&c.bal);
fwrite (&c,sizeof(struct customer),1,p);
}
getch();
}
(121)Write a program to read a record from a file ?
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct customer
{
char fname[20],lname[20];
int ac;
float bal;
};
void main()
{
FILE *p;
struct customer c;
clrscr();
p=fopen("c:\abc.txt","r");
if(p==NULL)
{
fprintf("file not exist");
exit(0);
}
while(fread(&c,sizeof(struct customer),1,p))
{
printf("FName=%s\n LName=%s\n
AcNo=%d\nBalance=f\n",c.fname,c.lname,c.ac,c.bal);
}
getch();
}
Miscellaneous(CH-12)
C Preprocessor or macro:-
The C Preprocessor is not a part of the compiler, but is a separate step in the
compilation process. In simple terms, a C Preprocessor is just a text
substitution tool and it instructs the compiler to do required pre-processing
before the actual compilation. We'll refer to the C Preprocessor as CPP.All
preprocessor commands begin with a hash symbol (#). It must be the first
nonblank character, and for readability, a preprocessor directive should begin
in the first column.
1. #define:- Substitutes a preprocessor macro.
2. #include:- Inserts a particular header from another file.
3. #undef:- Undefines a preprocessor macro.
4. #ifdef:- Returns true if this macro is defined.
5. #ifndef:- Returns true if this macro is not defined.
6. #if:- Tests if a compile time condition is true.
7. #else:- The alternative for #if.
8. #elif:- #else and #if in one statement.
9. #endif:- Ends preprocessor conditional.
10. #error:- Prints error message on stderr.
11. #pragma:- Issues special commands to the compiler, using a standardized
method.
Ex:-
#define m 5
#define n (m+1)
#define max(m,n) ((m>n)?m:n)
#define a 10
Typedef:-
C programming language provides a keyword called typedef, which you can use
to give a type a new name. You can use typedef to give a name to your user
defined data types as well. For example, we can use typedef with structure to
define a new data type and then use that data type to define structure variables
directly.
Syn:- typedef datatype aliasname;
Ex:-
122) write a program for typedef ?
#include<stdio.h>
#include<conio.h>
void main()
{
typedef int a;
a b=20,c=30;
printf(“value of b=%d\n”,b);
printf(“value of c=%d\n”,c);
getch();
}
Interview Questions
1.What is a pointer on pointer?
It’s a pointer variable which can hold the address of another pointer variable. It
de-refers twice to point to the data held by the designated pointer variable.
Eg: int x = 5, *p=&x, **q=&p;
Therefore ‘x’ can be accessed by **q.
2.Distinguish between malloc() & calloc() memory allocation ?
Both allocates memory from heap area/dynamic memory. By default calloc
fills the allocated memory with 0’s.
3.What is keyword auto for?
By default every local variable of the function is automatic (auto). In the below
function both the variables ‘i’ and ‘j’ are automatic variables.
void f() {
int i;
auto int j;
}
NOTE − A global variable can’t be an automatic variable.
4.What are the valid places for the keyword break to appear ?
Break can appear only with in the looping control and switch statement. The
purpose of the break is to bring the control out from the said blocks.
5.Explain the syntax for for loop ?
for(expression-1;expression-2;expression-3) {
//set of statements
}
When control reaches for expression-1 is executed first. Then following
expression-2, and if expression-2 evaluates to non-zero ‘set of statements’ and
expression-3 is executed, follows expression-2.
6.What is difference between including the header file with-in angular
braces < > and double quotes “ “ ?
If a header file is included with in < > then the compiler searches for the
particular header file only with in the built in include path. If a header file is
included with in “ “, then the compiler searches for the particular header file
first in the current working directory, if not found then in the built in include
path.
7.How a negative integer is stored ?
Get the two’s compliment of the same positive integer. Eg: 1011 (-5)
Step-1 − One’s compliment of 5 : 1010
Step-2 − Add 1 to above, giving 1011, which is -5
8.What is a static variable?
A static local variable retains its value between the function call and the default
value is 0. The following function will print 1 2 3 if called thrice.
void f() {
static int i;
++i;
printf(“%d “,i);
}
If a global variable is static then its visibility is limited to the same source code.
9.What is a NULL pointer ?
A pointer pointing to nothing is called so. Eg: char *p=NULL;
10.What is the purpose of extern storage specifier ?
Used to resolve the scope of global symbol.
Eg:
main() {
extern int i;
Printf(“%d”,i);
}
int i = 20;
11.Explain the purpose of the function sprintf() ?
Print the formatted output onto the character array.
12.What is the meaning of base address of the array ?
The starting address of the array is called as the base address of the array.
13.When should we use the register storage specifier ?
If a variable is used most frequently then it should be declared using register
storage specifier, then possibly the compiler gives CPU register for its storage
to speed up the look up of the variable.
14.S++ or S = S+1, which can be recommended to increment the value by 1
and why ?
S++, as it is single machine instruction (INC) internally.
15.What is a dangling pointer ?
A pointer initially holding valid address, but later the held address is released
or freed. Then such a pointer is called as dangling pointer.
16.What is lvalue and rvalue ?
The expression appearing on right side of the assignment operator is called as
rvalue. Rvalue is assigned to lvalue, which appears on left side of the
assignment operator. The lvalue should designate to a variable not a constant.
17.What is the difference between actual and formal parameters ?
The parameters sent to the function at calling end are called as actual
parameters while at the receiving of the function definition called as formal
parameters.
18.Can a program be compiled without main() function?
Yes, it can be but cannot be executed, as the execution requires main() function
definition.
19.What is the advantage of declaring void pointers ?
When we do not know what type of the memory address the pointer variable is
going to hold, then we declare a void pointer for such.
Where an automatic variable is stored?
Every local variable by default being an auto variable is stored in stack
memory.
20.What is a nested structure ?
A structure containing an element of another structure as its member is referred
so.
21.What is the difference between variable declaration and variable
definition ?
Declaration associates type to the variable whereas definition gives the value to
the variable.
22.What is a self-referential structure ?
A structure containing the same structure pointer variable as its element is
called as self-referential structure.
23.Does a built-in header file contains built-in function definition ?
No, the header file only declares function. The definition is in library which is
linked by the linker.
24.Explain modular programming ?
Dividing the program in to sub programs (modules/function) to achieve the
given task is modular approach. More generic functions definition gives the
ability to re-use the functions, such as built-in library functions.
25.What is a token ?
A C program consists of various tokens and a token is either a keyword, an
identifier, a constant, a string literal, or a symbol.
26.What is a pre-processor ?
Preprocessor is a directive to the compiler to perform certain things before the
actual compilation process begins.
27.Explain the use of %i format specifier w.r.t scanf() ?
Can be used to input integer in all the supported format.
28.How can you print a \ (backslash) using any of the printf() family of
functions ?
Escape it using \ (backslash).
29.Does a break is required by default case in switch statement ?
Yes, if it is not appearing as the last case and if we do not want the control to
flow to the following case after default if any.
30.When to user -> (arrow) operator ?
If the structure/union variable is a pointer variable, to access structure/union
elements the arrow operator is used.
31.What are bit fields ?
We can create integer structure members of differing size apart from non-
standard size using bit fields. Such structure size is automatically adjusted with
the multiple of integer size of the machine.
32.What are command line arguments ?
The arguments which we pass to the main() function while executing the
program are called as command line arguments. The parameters are always
strings held in the second argument (below in args) of the function which is
array of character pointers. First argument represents the count of arguments
(below in count) and updated automatically by operating system.
main( int count, char *args[]) {
}
33.What are the different ways of passing parameters to the functions?
Which to use when ?
Call by value − We send only values to the function as parameters. We
choose this if we do not want the actual parameters to be modified with
formal parameters but just used.
Call by reference − We send address of the actual parameters instead of
values. We choose this if we do want the actual parameters to be
modified with formal parameters.
34.What is the purpose of built-in stricmp() function ?
It compares two strings by ignoring the case.
35.Describe the file opening mode “w+” ?
Opens a file both for reading and writing. If a file is not existing it creates one,
else if the file is existing it will be over written.
36.Where the address of operator (&) cannot be used ?
It cannot be used on constants. It cannot be used on variable which are declared
using register storage class.
37.Is FILE a built-in data type ?
No, it is a structure defined in stdio.h.
38.What is reminder for 5.0 % 2 ?
Error, It is invalid that either of the operands for the modulus operator (%) is a
real number.
39.How many operators are there under the category of ternary operators
?
There is only one operator and is conditional operator (? : ).
40.Which key word is used to perform unconditional branching ?
goto
41.What is a pointer to a function? Give the general syntax for the same ?
A pointer holding the reference of the function is called pointer to a function.
In general it is declared as follows.
T (*fun_ptr) (T1,T2…); Where T is any date type.
Once fun_ptr refers a function the same can be invoked using the pointer as
follows.
fun_ptr();
[Or]
(*fun_ptr)();
42.Explain the use of comma operator (,) ?
Comma operator can be used to separate two or more expressions.
Eg: printf(“hi”) , printf(“Hello”);
43.What is a NULL statement ?
A null statement is no executable statements such as ; (semicolon).
Eg: int count = 0;
while( ++count<=10 ) ;
Above does nothing 10 times.
44.What is a static function ?
A function’s definition prefixed with static keyword is called as a static
function. You would make a function static if it should be called only within
the same source code.
45.Which compiler switch to be used for compiling the programs using
math library with gcc compiler ?
Opiton –lm to be used as > gcc –lm <file.c>
46.Which operator is used to continue the definition of macro in the next
linec?
Backward slash (\) is used.
E.g. #define MESSAGE "Hi, \
Welcome to C"
47.Which operator is used to receive the variable number of arguments for
a function ?
Ellipses (…) is used for the same. A general function definition looks as
follows
void f(int k,…) {
}
48.What is the problem with the following coding snippet ?
char *s1 = "hello",*s2 = "welcome";
strcat(s1,s2);
s1 points to a string constant and cannot be altered.
49.Which built-in library function can be used to re-size the allocated
dynamic memory ?
realloc()
50.Define an array ?
Array is collection of similar data items under a common name.
51.What are enumerations ?
Enumerations are list of integer constants with name. Enumerators are defined
with the keyword enum.
52.Which built-in function can be used to move the file pointer internally ?
fseek()
53.What is a variable ?
A variable is the name storage.
54.Who designed C programming language ?
Dennis M Ritchie.
55.C is successor of which programming language ?
B
56.What is the full form of ANSI ?
American National Standards Institute.
57.Which operator can be used to determine the size of a data type or
variable ?
sizeof
58.Can we assign a float variable to a long integer variable ?
Yes, with loss of fractional part.
59.Is 068 a valid octal number ?
No, it contains invalid octal digits.
60.What it the return value of a relational operator if it returns any ?
Return a value 1 if the relation between the expressions is true, else 0.
61.How does bitwise operator XOR works ?
If both the corresponding bits are same it gives 0 else 1.
62.What is an infinite loop ?
A loop executing repeatedly as the loop-expression always evaluates to true
such as
while(0 == 0) {
}
63.variables belonging to different scope have same name? If so show an
example ?
Variables belonging to different scope can have same name as in the following
code snippet.
int var;
void f() {
int var;
}
main() {
int var;
}
64.What is the default value of local and global variables ?
Local variables get garbage value and global variables get a value 0 by default.
65.Can a pointer access the array ?
Pointer by holding array’s base address can access the array.
66.What are valid operations on pointers ?
The only two permitted operations on pointers are
Comparision ii) Addition/Substraction (excluding void pointers)
67.What is a string length ?
It is the count of character excluding the ‘\0’ character.
68.What is the built-in function to append one string to another ?
strcat() form the header string.h
69.Which operator can be used to access union elements if union variable is
a pointer variable ?
Arrow (->) operator.
70.Explain about ‘stdin’ ?
stdin in a pointer variable which is by default opened for standard input device.
71.Name a function which can be used to close the file stream ?
fclose().
72.What is the purpose of #undef pre-processor ?
It be used to undefine an existing macro definition.
73.Define a structure ?
A structure can be defined of collection of heterogeneous data items.
74.Name the predefined macro which be used to determine whether your
compiler is ANSI standard or not ?
__STDC__
75.What is typecasting ?
Typecasting is a way to convert a variable/constant from one type to another
type.
76.What is recursion ?
Function calling itself is called as recursion.
77.Which function can be used to release the dynamic allocated memory ?
free().
78.What is the first string in the argument vector w.r.t command line
arguments?How can we determine whether a file is successfully opened or
not using fopen() function ?
On failure fopen() returns NULL, otherwise opened successfully.
79.What is the output file generated by the linker ?
Linker generates the executable file.
80.What is the maximum length of an identifier ?
Ideally it is 32 characters and also implementation dependent.
81.What is the default function call method ?
By default the functions are called by value.Functions must and should be
declared. Comment on this. Function declaration is optional if the same is
invoked after its definition.
82.When the macros gets expanded ?
At the time of preprocessing.
83.Can a function return multiple values to the caller using return reserved
word ?
No, only one value can be returned to the caller.
84.What is a constant pointer ?
A pointer which is not allowed to be altered to hold another address after it is
holding one.
85.To make pointer generic for which date type it need to be declared ?
Void
86.Can the structure variable be initialized as soon as it is declared ?
Yes, w.r.t the order of structure elements only.
87.Is there a way to compare two structure variables ?
There is no such. We need to compare element by element of the structure
variables.
88.Which built-in library function can be used to match a pattern from the
string ?
Strstr()
89.What is difference between far and near pointers ?
In first place they are non-standard keywords. A near pointer can access only
2^15 memory space and far pointer can access 2^32 memory space. Both the
keywords are implementation specific and are non-standard.
90.Can we nest comments in a C code ?
No, we cannot.
91.Which control loop is recommended if you have to execute set of
statements for fixed number of times ?
for – Loop.
92.What is a constant ?
A value which cannot be modified is called so. Such variables are qualified
with the keyword const.
93.Can we use just the tag name of structures to declare the variables for
the same ?
No, we need to use both the keyword ‘struct’ and the tag name.
94.Can the main() function left empty ?
Yes, possibly the program doing nothing.
95.Can one function call another ?
Yes, any user defined function can call any function.
96.Apart from Dennis Ritchie who the other person who contributed in
design of C language ?
Brain Kernighan
97.What is the output of the following code snippet ?
#include<stdio.h>
main()
{
short unsigned int i = 0;
printf("%u\n", i--);
}
A-0
B - Compile error
C - 65535
D - 32767
Answer : A
Explanation
0, with post decrement operator value of the variable will be considered as the
expression’s value and later gets decremented.
98.What is the size of ‘int’ ?
A-2
B-4
C-8
D - Compiler dependent
Answer : D
Explanation
The size of ‘int’ depends upon the complier i.e. whether it is a 16 bit or 32 bit.
99.What is the output of the following program ?
#include<stdio.h>
main()
{
int i = 1;
Charminar:
printf("%d ",i++);
if(i==3) break;
if(i<=5) goto Charminar;
}
A-12
B-123
C-1245
D - Compile error
Answer : D
Explanation
Compile error, wrong place for ‘break’ to appear.
100.To store a word/sentence declare a variable of the type ‘string’ ?
A - true
B - false
Answer : B
Explanation
There is no such data type called ‘string’ in C language.
101.What is the output of the following program ?
#include<stdio.h>
main()
{
char *s = "Hello";
while(*s!=NULL)
printf("%c", *s++);
}
A - Hello
B - Helloellolloloo
C - ello
D - Compile error
Answer : A
Explanation
NULL is equivalent to ‘\0’ in value. Statement *s++ prints the character first
and increments the address later.
102.Choose the correct function which can return a reminder by dividing -
10.0/3.0 ?
A - rem = mod(-10.0, 3.0);
B - rem = fmod(-10.0, 3.0);
C - rem = modf(-10.0, 3.0);
D - Division of floating point values can’t return reminder
Answer : B
Explanation
In the C Programming Language, the fmod() function compute and returns the
floating-point remainder when x is divided by y.
#include <math.h>
#include <stdio.h>
int main( void )
{
double x = -10.0, y = 3.0, z;
z = fmod( x, y );
printf( "The remainder of %.2f / %.2f is %f\n", w, x, z );
}
103.For a structure, if a variable behave as a pointer then from the given
below operators which operator can be used to access data of the structure
via the variable pointer ?
A-.
B-%
C - ->
D-#
Answer : C
Explanation
For a structure, Dot(.) operator can be used to access the data using normal
structure variable and arrow (->)can be used to access the data using pointer
variable.
104.What actually get pass when you pass an array as a function argument
?
A - First value of elements in array
B - Base address of the array
C - All value of element in array
D - Address of the last element of array
Answer : B
Explanation
By passing the name of an array as a function argument; the name contains the
base address of an array and the base address (array value) get updated in the
main function.
105.The correct order of evaluation for the expression “z = x + y * z / 4 %
2 – 1” ?
A-*/%=+-
B-/*%-+=
C--+=*%/
D-*/%+-=
Answer : D
Explanation
* / % holds highest priority than + - . All with left to right associativity.
106.In the given below code, what will be the value of a variable x ?
#include<stdio.h>
int main()
{
int y = 100;
const int x = y;
printf("%d\n", x);
return 0;
}
A - 100
B-0
C - Print x
D - Return Error
Answer : A
Explanation
Although, integer y = 100; and constant integer x is equal to y. here in the
given above program we have to print the x value, so that it will be 100.
107.What is the output of the following code snippet ?
#include<stdio.h>
main()
{
int const a = 5;
a++;
printf(“%d”,a);
}
A-5
B-6
C - Runtime error
D - Compile error
108.What is the output of the following code snippet?
#include<stdio.h>
main()
{
const int a = 5;
a++;
printf("%d", a);
}
A-5
B-6
C - Runtime error
D - Compile error
109.What is the output of the below code snippet ?
#include<stdio.h>
main()
{
char s[]="hello", t[]="hello";
if(s==t){
printf("eqaul strings");
}
}
A - Equal strings
B - Unequal strings
C - No output
D - Compilation error
110.What is the output of the below code snippet ?
#include<stdio.h>
main()
{
int a = 5, b = 3, c = 4;
if(x=5)
{
if(x=5) break;
printf("Hello");
}
printf("Hi");
}
A - Compile error
B - Hi
C - HelloHi
D - Compiler warning
119.What is the output of the following code snippet ?
#include<stdio.h>
main()
{
int x = 5;
if(x=5)
{
if(x=5) printf("Hello");
}
printf("Hi");
}
A - HelloHi
B - Hi
C - Hello
D - Compiler error
120.What is the output of the below code snippet ?
#include<stdio.h>
main()
{
for(;;)printf("Hello");
}
A - Infinite loop
B - Prints “Hello” once.
C - No output
D - Compile error
121.What is the output of the below code snippet ?
#include<stdio.h>
main()
{
for()printf("Hello");
}
A - Infinite loop
B - Prints “Hello” once.
C - No output
D - Compile error
122. What is the output of the below code snippet ?
#include<stdio.h>
main()
{
for(1;2;3)
printf("Hello");
}
A - Infinite loop
B - Prints “Hello” once.
C - No output
D - Compile error
Q 17 - int x=~1; What is the value of 'x'?
A-1
B - -1
C-2
D - -2
123.What is the output of the following program ?
#include<stdio.h>
void f()
{
static int i;
++i;
printf("%d", i);
}
main()
{
f();
f();
f();
}
A-111
B-000
C-321
D-123
124.What is the output of the following code snippet ?
#include<stdio.h>
main()
{
int *p = 15;
printf("%d",*p);
}
A - 15
B - Garbage value
C - Runtime error
D - Compiler error
125. What is the output of the following program ?
#include<stdio.h>
main()
{
register int x = 5;
int *p;
p=&x;
x++;
printf("%d",*p);
}
A - Compile error
B-5
C-6
D - Garbage value
126.What is the output of the following program ?
#include<stdio.h>
main()
{
int x = 65, *p = &x;
void *q=p;
char *r=q;
printf("%c",*r);
}
A - Garbage character.
B-A
C - 65
D - Compile error
127.What is the output of the following program ?
#include<stdio.h>
void f()
{
printf(“Hello\n”);
}
main()
{
;
}
A - No output
B - Error, as the function is not called.
C - Error, as the function is defined without its declaration
D -Error, as the main() function is left empty
128.What is the output of the following program ?
#include<stdio.h>
main()
{
printf("\");
}
A-\
B - \"
C-"
D - Compile error
129.What is the output of the following program ?
#include<stdio.h>
{
int x = 1;
switch(x)
{
default: printf("Hello");
case 1: printf("hi"); break;
}
}
A - Hello
B - Hi
C - HelloHi
D - Compile error
130.What is the output of the following program ?
#include<stdio.h>
main()
{
struct { int x;} var = {5}, *p = &var;
printf("%d %d %d",var.x,p->x,(*p).x);
}
A-555
B - 5 5 garbage value
C-550
D - Compile error
131.What is the output of the following program ?
#include<stdio.h>
void swap(int m, int n)
{
int x = m;
m = n;
n = x;
}
main()
{
int x=5, y=3;
swap(x,y);
printf("%d %d", x, y);
}
A-35
B-53
C-55
D - Compile error
132.What will be printed for the below statement ?
#include<stdio.h>
main()
{
printf("%d",strcmp("strcmp()","strcmp()"));
}
A-0
B-1
C - -1
D - Invalid use of strcmp() function
133. What is the following program doing ?
#include<stdio.h>
main()
{
FILE *stream=fopen("a.txt",'r');
}
A - Trying to open “a.txt” in read mode
B - Trying to open “a.txt” in write mode.
C - “stream” is an invalid identifier
D - Compile error
134.What is the output of the following program ?
#include<stdio.h>
main()
{
int r, x = 2;
float y = 5;
r = y%x;
printf("%d", r);
}
A-1
B-0
C-2
D - Compile error
135.Which operator is used to continue the definition of macro in the next
line ?
A-#
B - ##
C-$
D-\
136.What is the size of the following union definition ?
#include<stdio.h>
union abc {
char a,b,c,d,e,f,g,h;
int i;
}abc;
main()
{
printf( "%d", sizeof( abc ));
}
A-1
B-2
C-4
D-8
137.What is the size of ‘int’ ?
A-2
B-4
C-8
D - Compiler dependent
138.The type name/reserved word ‘short’ is ___
A - short long
B - short char
C - short float
D - short int
139.What is the value of ‘y’ for the following code snippet ?
#include<stdio.h>
main()
{
int x = 1;
float y = x>>2;
printf( "%f", y );
}
A-4
B - 0.5
C-0
D-1
140. What is the output of the following program ?
#include<stdio.h>
main()
{
float t = 2;
switch(t)
{
case 2: printf("Hi");
default: printf("Hello");
}
}
A - Hi
B - HiHello
C - Hello
D - Error
141.What is the output of the following program ?
#include<stdio.h>
main()
{
int i = 1;
while(++i <= 5)
printf("%d ",i++);
}
A-135
B-24
C-246
D-2
142.What is the output of the following program ?
#include<stdio.h>
main()
{
int i = 1;
while( i++<=5 )
printf("%d ",i++);
}
A-135
B-24
C-246
D-2
143.What is the output of the following program ?
#include<stdio.h>
main()
{
int i = 1;
while(i++<=5);
printf("%d ",i++);
}
A-4
B-6
C-26
D-24
144.What is the output of the following program ?
#include<stdio.h>
main()
{
int x = 1;
do
printf("%d ", x);
while(x++<=1);
}
A-1
B-12
C - No output
D - Compile error
145.What is the output of the following program ?
#include<stdio.h>
main()
{
int a[] = {1,2}, *p = a;
printf("%d", p[1]);
}
A-1
B-2
C - Compile error
D - Runtime error
146.What is the output of the following program ?
#include<stdio.h>
main()
{
int a[3] = {2,1};
printf("%d", a[a[1]]);
}
A-0
B-1
C-2
D-3
147.What is the output of the following program ?
#include<stdio.h>
main()
{
int a[3] = {2,,1};
printf("%d", a[a[0]]);
}
A-0
B-1
C-2
D - Compile error
148.What is the output of the following program ?
#include<stdio.h>
main()
{
int a[] = {2,1};
printf("%d", *a);
}
A-0
B-1
C-2
D - Compile error.
149.What is the output of the following program ?
#include<stdio.h>
main()
{
int i = 1;
Charminar:
printf("%d ",i++);
if(i==3) break;
if(i<=5) goto Charminar;
}
A-12
B-123
C-1245
D - Compile error
150.What is the output of the following program ?
#include<stdio.h>
main()
{
int i = 13, j = 60;
i ^= j;
j ^= i;
i ^= j;
printf("%d %d", i, j);
}
A - 73 73
B - 60 13
C - 13 60
D - 60 60
151.What is the output of the following program ?
#include<stdio.h>
main()
{
union abc {
int x;
char ch;
}var;
var.ch = 'A';
printf("%d", var.x);
}
A-A
B - Garbage value
C - 65
D - 97
152.Identify the incorrect file opening mode from the following ?
A-r
B-w
C-x
D-a
153.Function fopen() with the mode "r+" tries to open the file for __
A - reading and writing
B - reading and adding new content
C - only for reading
D - it works only for directories
154.Identify the invalid constant used in fseek() function as ‘whence’
reference ?
A - SEEK_SET
B - SEEK_CUR
C - SEEK_BEG
D - SEEK_END
155.First operating system designed using C programming language ?
A - DOS
B - Windows
C - UNIX
D - Mac