C Programming
C Programming
partner-pub-0153 ISO-8859-1
Custom Search
C QUESTIONS
1. PERFECT NUMBER.
void main()
{
int n,i=1,sum=0;
clrscr();
printf("\nEnter a number:-");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
Search
if(sum==n)
printf("\nThe no %d is a perfect
number",i);
else
printf("\nThe no %d is not a perfect
number",i);
getch();
}
2. ARMSTRONG NUMBER.
void main()
{
int num,r,sum=0,temp;
clrscr();
printf("\nEnter a number:-");
scanf("%d",&num);
temp=num;
while(num!=0)
{
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("\nThe number %d is an armstrong
number",temp);
else
printf("\nThe number %d is not an
armstrong number",temp);
getch();
3. STRONG NUMBER
void main()
{
int num,i,f,r,sum=0,temp;
clrscr();
printf("\nEnter a number");
scanf("%d",&num);
temp=num;
while(num)
{
i=1,f=1;
r=num%10;
while(i<=r)
{
f=f*i;
i++;
}
sum=sum+f;
num=num/10;
}
if(sum==temp)
printf("%d is a strong number",temp);
else
printf("%d is not a strong number",temp);
getch();
}
4. PRIME NUMBER.
void main()
{
int num,i,count=0;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
count++;
}
if(count==2)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
getch();
}
5. REVERSE A NUMBER
void main()
{
int num,sum=0,r;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
while(num)
{
r=num%10;
sum=sum*10+r;
num=num/10;
}
printf("\nReverse number=%d",sum);
getch();
}
6. SUM OF THE DIGITS OF A NUMBER
void main()
{
int num,sum=0,r;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
while(num)
{
r=num%10;
num=num/10;
sum=sum+r;
}
printf("sum=%d",sum);
getch();
}
7. PALINDROME NUMBER.
void main()
{
int num,r,sum=0,temp;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
temp=num;
while(num)
{
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("\n%d is a palindrome",temp);
else
printf("\n%d is not a palindrome",temp);
getch();
}
8. G.C.D OF TWO NUMBERS
void main()
{
int n1,n2;
clrscr();
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
while(n1!=n2)
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf("\nGCD=%d",n1);
getch();
}
9. L.C.M OF TWO NUMBERS.
void main()
{
int n1,n2,x,y;
clrscr();
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
x=n1,y=n2;
while(n1!=n2)
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf("L.C.M=%d",x*y/n1);
getch();
}
10. SWAP TWO VARIABLES WITHOUT USING THIRD VARIABLE
void main()
{
int a,b;
clrscr();
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
printf("\nBefore swapping a=%d b=%d",a,b);
a=a^b;
b=b^a;
a=a^b;
printf("\nAfter swapping a=%d b=%d",a,b);
getch();
}
11. FLOYDS TRIANGLE
1
2 3
4 5 6
void main()
{
int i,j,r,k=1;
clrscr();
printf("\nEnter the range:");
scanf("%d",&r);
printf("\nFLOYD'S TRIANGLE\n\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=i;j++,k++)
printf(" %d",k);
printf("\n");
}
getch();
}
12. PRIME FACTORS OF A NUMBER
void main()
{
int num,i=1,j,k;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
while(i<=num)
{
k=0;
if(num%i==0)
{
j=1;
while(j<=i)
{
if(i%j==0)
k++;
j++;
}
if(k==2)
printf("\n%d is
a prime factor",i);
}
i++;
}
getch();
}
13. MULTIPLICATION TABLE
void main()
{
int r,i,j,k;
clrscr();
printf("\nEnter the number range:-");
scanf("%d",&r);
for(i=1;i<=r;i++)
{
for(j=1;j<=10;j++)
printf(" %d*%d=%d",i,j,i*j);
printf("\n");
}
getch();
}
14. FACTORIAL OF A NUMBER
void main()
{
int i=1,f=1,num;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
while(i<=num)
{
f=f*i;
i++;
}
printf("\nFactorial of %d is:%d",num,f);
getch();
}
15. FIBONACCI SERIES
void main()
{
int i=0,j=1,k=2,r,f;
clrscr();
printf("Enter the number range:");
scanf("%d",&r);
printf("\nFIBONACCI SERIES: ");
printf("%d %d",i,j);
while(k<r)
{
f=i+j;
i=j;
}
getch();
j=f;
printf(" %d",j);
k++;
}
16. PRINTING ASCII VALUE
void main()
{
int i;
clrscr();
for(i=0;i<=255;i++)
{
printf("%d -> %c ",i,i);
delay(10);
}
getch();
}
17. CHECKING LEAP YEAR
void main()
{
int year;
clrscr();
printf("Enter any year->");
scanf("%d",&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
getch();
}
18. CONVERSION OF DECIMAL TO BINARY
void main()
{
int n,m,no=0,a=1,rem;
clrscr();
printf("Enter any decimal number->");
scanf("%d",&n);
m=n;
while(n!=0)
{
rem=n%2;
no=no+rem*a;
n=n/2;
a=a*10;
}
printf("The value %d in binary is->",m);
printf("%d",no);
getch();
}
19. CONVERSION OF BINARY TO DECIMAL
void main()
{
long int no,n=0,j=1,rem,no1;
clrscr();
printf("Enter any number any binary form->");
scanf("%ld",&no);
no1=no;
while(no!=0)
{
rem=no%10;
n=n+rem*j;
j=j*2;
no=no/10;
}
printf("\nThe value of binary no. %ld is ->
%ld",no1,n);
getch();
}
20. SWAPING OF TWO ARRAYS
void main()
{
int a[10],b[10],c[10],i;
clrscr();
printf("Enter First array->");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\nEnter Second array->");
for(i=0;i<10;i++)
scanf("%d",&b[i]);
printf("Arrays before swapping");
printf("\nFirst array->");
for(i=0;i<10;i++)
{
printf("%d",a[i]);
}
printf("\nSecond array->");
for(i=0;i<10;i++)
printf("%d",b[i]);
}
for(i=0;i<10;i++)
{
//write any swapping technique
c[i]=a[i];
a[i]=b[i];
b[i]=c[i];
}
printf("\nArrays after swapping");
printf("\nFirst array->");
for(i=0;i<10;i++)
{
printf("%d",a[i]);
}
printf("\nSecond array->");
for(i=0;i<10;i++)
{
printf("%d",b[i]);
}
getch();
}
21. FINDING NCR FACTOR
void main()
{
int n,r,ncr;
clrscr();
printf("Enter any two numbers->");
scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("The NCR factor of %d and %d is
%d",n,r,ncr);
getch();
}
int fact(int n)
{
int i=1;
while(n!=0)
{
i=i*n;
n--;
}
return i;
}
22. PASCALS TRIANGLE
void main()
{
int line,i,j,k;
clrscr();
printf("Enter the no. of lines");
scanf("%d",&line);
for(i=1;i<=line;i++)
{
for(j=1;j<=line-i;j++)
printf(" ");
for(k=1;k<i;k++)
printf("%d",k);
for(k=i;k>=1;k--)
printf("%d",k);
printf("\n");
}
getch();
}
23. CONVERSION FROM UPPERCASE TO LOWER CASE
void main()
{
char str[20];
int i;
clrscr();
printf("Enter any string->");
scanf("%s",str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nThe string in uppercase is->%s",str);
getch();
}
24. CONVERSION FROM LOWER CASE TO UPPER CASE
void main()
{
char str[20];
int i;
clrscr();
printf("Enter any string->");
scanf("%s",str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nThe string in lowercase is->%s",str);
getch();
}
25. DELETE THE VOWELS FROM A STRING
void main()
{
char str[20],s[20];
int i,j=0;
clrscr();
printf("Enter any string->");
scanf("%s",str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]=='a'||str[i]=='e'||
str[i]=='i'||str[i]=='o'||str[i]=='u')
str[i]=' ';
else
s[j++]=str[i];
}
s[j]='\0';
printf("\nThe string without vowel is->%s",s);
getch();
}
26. ADDITION OF MATRICES
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("Enter the First matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
}
28. COPY DATA FROM ONE FILE TO ANOTHER FILE
#include"stdio.h"
void main()
{
FILE *p,*q;
char file1[20],file2[20];
char ch;
clrscr();
printf("\nEnter the source file name to be
copied:");
gets(file1);
p=fopen(file1,"r");
if(p==NULL)
{
printf("cannot open %s",file1);
exit(0);
}
printf("\nEnter the destination file name:");
gets(file2);
q=fopen(file2,"w");
if(q==NULL)
{
printf("cannot open %s",file2);
exit(0);
}
while((ch=getc(p))!=EOF)
putc(ch,q);
printf("\nCOMPLETED");
fclose(p);
fclose(q);
getch();
}
29. ADDITION & SUBTRACTION OF TWO COMPLEX NUMBERS
void main()
{
int a,b,c,d,x,y;
clrscr();
printf("\nEnter the first complex number:");
scanf("%d%d",&a,&b);
printf("\nEnter the second complex number:");
scanf("%d%d",&c,&d);
if(b<0)
printf("%d-i\n",a-b);
else
if(d<0)
printf("d+i\n",a+b);
printf("d-i\n",c-d);
else
printf("%d+i\n",c+d);
printf("\nADDITION ");
x=a+c;
y=b+d;
if(y>0)
printf("%d-i%d",x,-y);
else
printf("%d+i%d",x,+y);
printf("\n\nSUBTRACTION ");
x=a-c;
y=b-d;
if(y<0)
printf("%d-i%d",x,-y);
else
printf("%d+i%d",x,+y);
getch();
}
30. SUM OF THE SERIES 1+2+3+---------+n
void main()
{
int r;
clrscr();
printf("\nEnter the number range: ");
scanf("%d",&r);
printf("\nSum of the series is: %d",(r*(r+1))/2);
getch();
}
31. SUM OF SQUARES OF THE SERIES 12+22+32+--------+n2
void main()
{
long int r;
clrscr();
printf("\nEnter the range: ");
scanf("%ld",&r);
printf("\nSum of the squares of the series is:
%ld",((r*(r+1))*(2*r+1))/6);
getch();
}
32. SUM OF CUBES OF THE SERIES 13+23+33+---------+n3
void main()
{
int r;
clrscr();
printf("\nEnter the number range: ");
scanf("%d",&r);
printf("\nSum of the cubes of the series is: %d",
(r*(r+1)/2)*(r*(r+1)/2));
getch();
}
33. LARGEST NUMBER IN AN ARRAY
void main()
{
int a[50],size,i,big;
clrscr();
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ,
size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++)
{
if(big<a[i])
big=a[i];
}
printf("\nBiggest: %d",big);
getch();
}
34. SECOND LARGEST NUMBER IN AN UNSORTED ARRAY
main()
{
int un[10], i, big1, big2;
printf("Enter array elements: ");
for ( i = 0; i < 10; ++i )
scanf("%d", &un[i]);
big1 = un[0];
for ( i = 1; i < 10; ++i )
{
if ( big1 < un[i] )
big1 = un[i];
if ( big1 != un[0] )
big2 = un[0];
else
big2 = un[1];
}
for ( i = 1; i < 10; ++i )
{
if ( big1 != un[i] && big2 < un[i] )
big2 = un[i];
}
printf("Second largest: %d\n", big2);
return 0;
}
35. SECOND SMALLEST NUMBER IN AN UNSORTED ARRAY
main()
{
int un[10], i, s1, s2;
clrscr();
printf("Enter array elements: ");
for ( i = 0; i < 10; ++i )
scanf("%d", &un[i]);
s1 = un[0];
for ( i = 1; i < 10; ++i )
{
if ( s1 > un[i] )
s1 = un[i];
if ( s1 != un[0] )
s2 = un[0];
else
s2 = un[1];
}
for ( i = 1; i < 10; ++i )
{
if ( s1 != un[i] && s2 > un[i] )
s2 = un[i];
}
printf("\nSecond smallest: %d", s2);
return 0;
}
36. MULTIPLICATION OF MATRICES
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
clrscr();
printf("\nEnter the row and column of first
matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the row and column of second
matrix");
scanf("%d %d",&o,&p);
if(n!=o)
{
printf("Matrix mutiplication is not
possible");
printf("\nColumn of first matrix must be
same as row of second matrix");
}
else
{
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i]
[j]);
printf("\nEnter the Second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i]
[j]);
printf("\nThe First matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\nThe Second matrix is\n");
for(i=0;i<o;i++)
{
printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++)//row of first matrix
{
for(j=0;j<p;j++)//column of
second matrix
{
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
}
printf("\nThe multiplication of two matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",c[i][j]);
}
}
getch();
}
37. SUM OF DIAGONAL ELEMENTS OF A MATRIX
void main()
{
int a[10][10],i,j,sum=0,m,n;
clrscr();
printf("\nEnter the row and column of matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
sum=sum+a[i][j];
}
}
printf("\n\nSum of the diagonal elements of a
matrix is -> ");
printf("%d",sum);
getch();
}
38. TRASPOSE OF A MATRIX
void main()
{
int a[10][10],b[10][10],i,j,k=0,m,n;
clrscr();
printf("\nEnter the row and column of matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[i][j]=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=a[j][i];
printf("\n
%d",b[i][j]);
}
printf("\n\nTraspose of a matrix is ->
");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("%d\t",b[i][j]);
}
}
getch();
}
39. CONVERSION OF FAREHNITE TO CENTIGRADE
void main()
{
float c,f;
clrscr();
printf("Enter temp. in farehnite");
scanf("%f",&f);
c=(5*(f-32))/9;//Formula for conversion
printf("The temp. in centigrade is->%f",c);
getch();
}
40. COUNTING DIFFERENT CHARACTERS IN A STRING
main()
{
int a[26],A[26],i,c=0;
char str[100];
clrscr();
puts("Enter a string->");
gets(str);
for(i=0;i<26;i++)
{
a[i]=0;
A[i]=0;
}
for(i=0;str[i]!='\0';i++)
{
c=str[i];
if(c<97)
{
c=c-65;
}
else
{
A[c]++;
c=c-97;
a[c]++;
}
}
for(i=0;i<26;i++)
{
if(a[i]!=0)
times",i+97,a[i]);
}
for(i=0;i<26;i++)
{
if(A[i]!=0)
printf("\n%c occurs %d
printf("\n%c occurs %d
times",i+97,A[i]);
}
getch();
}
41. SORTING OF STRING
void main()
{
int i,j,n;
char str[20][20],temp[20];
clrscr();
puts("Enter the no. of string to be sorted");
scanf("%d",&n);
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
printf("The sorted string\n");
for(i=0;i<=n;i++)
puts(str[i]);
getch();
}
42. BUBBLE SORT
void main()
{
int s,temp,i,j,a[20];
clrscr();
printf("\nEnter size of the array: ");
scanf("%d",&s);
printf("\nEnter %d elements in to the array:",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=0;i<s-1;i++)
{
for(j=0;j<s-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nThe array after sorting is: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
getch();
}
43. SELECTION SORT
void main()
{
int s,i,j,temp,a[20];
clrscr();
printf("\nEnter size of the array :");
scanf("%d",&s);
printf("\nEnter %d elements in to the array:");
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=0;i<s;i++)
for(j=i+1;j<s;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nThe array after sorting is: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
getch();
}
44. INSERTION SORT
void main()
{
int i,j,s,temp,a[20];
clrscr();
printf("\nEnter size of the array: ");
scanf("%d",&s);
printf("\nEnter %d elements in to the array:",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=1;i<s;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("\nAfter sorting the elements are: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
getch();
}
45. DISPLAY SOURCE CODE AS OUTPUT
#include"stdio.h"
void main()
{
FILE *p;
char ch;
clrscr();
p=fopen("raja.c","r");
while((ch=getc(p))!=-1)
putchar(ch);
fclose(p);
getch();
}
46. FACTORIAL OF A NUMBER USING RECURSION
void main()
{
int num,f;
clrscr();
printf("\nEnter a number: ");
scanf("%d",&num);
f=fact(num);
printf("\nFactorial of %d is: %d",num,f);
getch();
}
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}
47. GCD OF A NUMBER USING RECURSION
void main()
{
int n1,n2,gcd;
clrscr();
printf("\nEnter two numbers: ");
scanf("%d %d",&n1,&n2);
gcd=findgcd(n1,n2);
printf("\nGCD of %d and %d is: %d",n1,n2,gcd);
getch();
}
int findgcd(int x,int y)
{
while(x!=y)
{
if(x>y)
else
return findgcd(x-y,y);
return findgcd(x,y-x);
}
return x;
}
48. SUM OF DIGITS OF A NUMBER USING RECURSION
void main()
{
int num,x;
clrscr();
printf("\nEnter a number: ");
scanf("%d",&num);
x=findsum(num);
printf("Sum of the digits of %d is: %d",num,x);
getch();
}
int r,s;
int findsum(int n)
{
if(n)
{
r=n%10;
s=s+r;
findsum(n/10);
}
else
return s;
}
49. POWER OF A NUMBER
void main()
{
int pow,num,i=1;
long int sum=1;
clrscr();
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter power: ");
scanf("%d",&pow);
while(i<=pow)
{
sum=sum*num;
i++;
}
printf("\n%d to the power %d is: %ld",num,pow,sum);
getch();
}
50. POWER OF A NUMBER USING RECURSION
void main()
{
int pow,num;
long int res;
long int power(int,int);
clrscr();
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter power: ");
scanf("%d",&pow);
res=power(num,pow);
printf("\n%d to the power %d is: %ld",num,pow,res);
getch();
}
int i=1;
long int sum=1;
long int power(int num,int pow)
{
if(i<=pow)
{
sum=sum*num;
power(num,pow-1);
}
else
return sum;
}
Links to this post
0 comments
int i=320;
char *ptr=(char *)&i;
printf("%d",*ptr);
}
(a)320
(b)1
(c)64
(d)Compiler error
(e)None of above
Output: (c)
Explanation:
As we know size of int data type is two
byte while char pointer can pointer one
byte at time.
Memory representation of int i=320
test.c 1:
test.c 2: void main(){
test.c 3: int i;
test.c 4: i=5+2*5+2*5+2;
test.c 5: printf("%d",i);
test.c 6: }
test.c 7:
You can absorb #define only pastes the
5+2 in place of x in program. So,
i=5+2*5+2*5+2
=5+10+10+2
=27
What is intermediate file and how to see
intermediate file?
Preprocessor tutorial.
(3) What will be output if you will
compile and execute the following c code?
void main(){
char c=125;
c=c+10;
printf("%d",c);
}
(a)135
(b)+INF
(c)-121
(d)-8
(e)Compiler error
Output: (c)
Explanation:
As we know char data type shows cyclic
properties i.e. if you will increase or
decrease the char variables beyond its
maximum or minimum value respectively it
will repeat same value according to
following cyclic order:
So,
125+1= 126
125+2= 127
125+3=-128
125+4=-127
125+5=-126
125+6=-125
125+7=-124
125+8=-123
125+9=-122
125+10=-121
constant
5.2
is
101.00
11001100
11001100
11001100 11001100 11001101
stored
in
11001100
is
less
than
int i=4,x;
x=++i + ++i + ++i;
printf("%d",x);
}
(a)21
(b)18
(c)12
(d)Compiler error
(e)None of above
Output: (a)
Explanation:
In ++a, ++ is pre increment operator. In
any mathematical expression pre increment
operator first increment the variable up
to break point then starts assigning the
final value to all variable.
Step 1: Increment the variable I up to
break point.
So, i=7+7+7=21
What is break point?
Operator tutorial.
(6) What will be output if you will
compile and execute the following c code?
void main(){
int a=2;
if(a==2){
a=~a+2<<1;
printf("%d",a);
}
else
{ break;
}
}
(a)It will print nothing.
(b)-3
(c)-2
(d)1
(e)Compiler error
Output: (e)
Explanation:
Keyword break is not part of if-else
statement. Hence it will show compiler
error: Misplaced break
Where we can use break keyword?
Control statement tutorial
(7) What will be output if you will
compile and execute the following c code?
void main(){
int a=10;
printf("%d %d %d",a,a++,++a);
}
(a)12 11 11
(b)12 10 10
(c)11 11 12
(d)10 10 12
(e)Compiler error
Output: (a)
Explanation:
In
c
printf
function
follows
cdecl
parameter passing scheme. In this scheme
parameter is passed from right to left
direction.
pascal
parameter
Function tutorial.
(8) What will be output if you will
compile and execute the following c code?
void main(){
char *str="Hello world";
printf("%d",printf("%s",str));
}
}
(a)cquestionbank
(b)cquestionbank\0
(c)(null)
(d)It will print nothing
(e)Compiler error
Output: (c)
Explanation:
We cannot copy any
function
to
the
pointing to NULL.
String tutorial.
More questions of string.
(10) What will be output if you will
compile and execute the following c code?
#include "stdio.h"
#include "string.h"
void main(){
int i=0;
for(;i<=2;)
printf(" %d",++i);
}
(a)0 1 2
(b)0 1 2 3
(c)1 2 3
(d)Compiler error
(e)Infinite loop
Output: (c)
Explanation:
In for loop each part is optional.
Complete tutorial of looping in C.
(11) What will be output if you will
compile and execute the following c code?
void main(){
int x;
for(x=1;x<=5;x++);
printf("%d",x);
}
(a)4
(b)5
(c)6
(d)Compiler error
(e)None of above
Output: (c)
Explanation:
Body of for loop is optional. In this
question for loop will execute until
value of variable x became six and
condition became false.
Looping tutorial.
(12) What will be output if you will
compile and execute the following c code?
void main(){
printf("%d",sizeof(5.2));
}
(a)2
(b)4
(c)8
(d)10
(e)Compiler error
Output: (c)
Explanation:
Default type of floating point constant
is double. So 5.2 is double constant and
its size is 8 byte.
printf("%d",*ptr);
}
int * call(){
int a=25;
a++;
return &a;
}
(a)25
(b)26
(c)Any address
(d)Garbage value
(e)Compiler error
Output: (d)
Explanation:
In this question variable a is a local
variable and its scope and visibility is
within the function call. After returning
the address of a by function call
variable a became dead while pointer ptr
is still pointing to address of variable
a. This problem is known as dangling
pointer problem.
Complete pointer tutorial.
(16)
What
declaration?
is
error
in
following
struct outer{
int a;
struct inner{
char c;
};
};
(a)Nesting of structure is not allowed in
c.
(b)It is necessary to initialize the
member variable.
(c)Inner structure must have name.
(d)Outer structure must have name.
(e)There is not any error.
Output: (c)
Explanation:
It is necessary to assign name of inner
structure at the time of declaration
other wise we cannot access the member of
inner structure. So correct declaration
is:
struct outer{
int a;
struct inner{
char c;
}name;
};
Structure tutorial.
Union tutorial.
(17) What will be output if you will
compile and execute the following c code?
void main(){
int array[]={10,20,30,40};
printf("%d",-2[array]);
}
(a)-60
(b)-30
(c)60
(d)Garbage value
(e)Compiler error
Output: (b)
Explanation:
In c,
array[2]=*(array+2)=*(2+array)=2[array]=3
0
Array tutorial.
Array of pointer.
How to read complex pointers.
(18) What will be output if you will
compile and execute the following c code?
void main(){
int i=10;
static int x=i;
if(x==i)
printf("Equal");
else if(x>i)
printf("Greater than");
else
printf("Less than");
}
(a)Equal
(b)Greater than
(c)Less than
(d)Compiler error
(e)None of above
Output: (d)
Explanation:
static variables are load time entity
while auto variables are run time entity.
We can not initialize any load time
variable by the run time variable.
In this example i is run time variable
while x is load time variable.
What is storage class?
(18) What will be output if you will
compile and execute the following c code?
void main(){
int i=5,j=2;
if(++i>j++||i++>j++)
printf("%d",i+j);
}
(a)7
(b)11
(c)8
(d)9
(e)Compiler error
Output: (d)
Explanation:
|| is logical OR operator. In C logical
OR operator doesnt check second operand
if first operand is true.
++i>j++ || i++>j++
First operand: ++i>j++
Second operand: i++>j++
First operand
++i > j++
=> 6 > 2
Since first operand is true so it will
not check second operand.
Hence i= 6 and j=3
Properties of && operator.
Operator tutorial with examples.
(19) What will be output if you will
compile and execute the following c code?
#define max 5;
void main(){
int i=0;
i=max++;
printf("%d",i++);
}
(a)5
(b)6
(c)7
(d)0
(e)Compiler error
Output: (e)
Explanation:
#define is token pasting preprocessor. If
you will see intermediate file: test.i
test.c 1:
test.c 2: void main(){
test.c 3: int i=0;
test.c 4: i=5++;
test.c 5: printf("%d",i++);
test.c 6: }
test.c 7:
It is clear macro constant max has
replaced by 5. It is illegal to increment
the constant number. Hence compiler will
show Lvalue required.
//From
the
conversion
rule
of
automatic
type
(b)Greater than
(c)Less than
(d)Compiler error
(e)None of above
Output: (a)
Explanation:
As we know huge
physical address.
pointers
compare
its
(d)Compiler error
(e)None of above
Output: (d)
Explanation:
Array element cannot be address of auto
variable. It can be address of static or
extern variables.
What is auto variable?
What is extern variable?
What is static variable?
Array tutorial.
(25) What will be output if you will
compile and execute the following c code?
void main(){
int array[3]={5};
int i;
for(i=0;i<=2;i++)
printf("%d ",array[i]);
}
(a)5 garbage garbage
(b)5 0 0
(e)8
Output: 8
Explanation:
array[1][0][2] means 1*(2*3)+0*(3)+3=9th
element of array starting from zero i.e.
8.
Questions on two dimension array.
Complete tutorial of array.
(27) What will be output if you will
compile and execute the following c code?
void main(){
int a[2][4]={3,6,9,12,15,18,21,24};
printf("%d %d
%d",*(a[1]+2),*(*(a+1)+2),2[1[a]]);
}
(a)15 18 21
(b)21 21 21
(c)24 24 24
(d)Compiler error
(e)None of above
Output: (b)
Explanation:
In c,
a [1][2]
=*(a [1] +2)
=*(*(a+1) +2)
=2[a [1]]
=2[1[a]]
Now, a [1] [2] means 1*(4) +2=6th element
of an array staring from zero i.e. 21.
Concept of complex array.
Concept of complex pointer.
Concept of complex function.
(28) What will be output if you will
compile and execute the following c code?
void call(int,int,int);
void main(){
int a=10;
call(a,a++,++a);
}
void call(int x,int y,int z){
printf("%d %d %d",x,y,z);
}
(a)10 10 12
(b)12 11 11
(c)12 12 12
(d)10 11 12
(e)Compiler error
Output: (e)
Explanation:
Default parameter passing scheme of c is
cdecl i.e. argument of function will pass
from right to left direction.
void main(){
int x=5,y=10,z=15;
printf("%d %d %d");
}
(a)Garbage Garbage Garbage
(b)5 10 15
(c)15 10 5
(d)Compiler error
(e)Run time error
Output: (c)
Explanation:
Auto variables are stored
shown in following figure.
in
stack
as
printf("%d",x);
}
(a)17
(b)18
(c)21
(d)22
(e)Compiler error
Output: (e)
Explanation:
In c register variable stores in CPU it
doesnt
store
in
RAM.
So
register
variable have not any memory address. So
it is illegal to write &a.
Complete tutorial of storage class with
examples.
Properties of register storage class.
(31) What will be output if you will
compile and execute the following c code?
void main(){
int a=5;
int b=10;
{
int a=2;
a++;
b++;
}
printf("%d %d",a,b);
}
(a)5 10
(b)6 11
(c)5 11
(d)6 10
(e)Compiler error
Output: (c)
Explanation:
Default storage class of local variable
is auto. Scope and visibility of auto
variable is within the block in which it
has declared. In c, if there are two
variables of the same name then we can
access only local variable. Hence inside
the inner block variable a is local
variable which has declared and defined
inside that block. When control comes out
of the inner block local variable a
became dead.
BLUE=GREEN+1=-20+1=-19
YELLOW=BLUE+1=-19+1=-18
Complete tutorial of enum data type with
examples.
(34) What will be output if you will
compile and execute the following c code?
void main(){
asm{
mov bx,8;
mov cx,10
add bx,cx;
}
printf("%d",_BX);
}
(a)18
(b)8
(c)0
(d)Compiler error
(e)None of above
Output: (a)
Explanation:
Explanation:
Size of enum constant is size of sign
int. Since value of c=32767. Hence value
of d will be 32767+1=32768 which is
beyond the range of enum constant.
Tutorial of data type with examples.
(36) What will be output if you will
compile and execute the following c code?
void main(){
signed int a=-1;
unsigned int b=-1;
if(a==b)
printf("%d %d",a,b);
else
printf("Not equal");
}
(a)-1 -1
(b)-1 32767
(c)-1 -32768
(d)Not equal
(e)Compiler error
Output: (a)
Explanation:
What is automatic type conversion?
(37) What will be output if you will
compile and execute the following c code?
void main(){
float f=5.5f;
float x;
x=f%2;
printf("%f",x);
}
(a)1.500000
(b)1.000000
(c)5.500000
(d)Compiler error
(e)None of above
Output: (d)
Explanation:
Modular division
floating number.
is
not
allowed
with
char c='0';
printf("%d %d",sizeof(c),sizeof('0'));
}
(a)1 1
(b)2 2
(c)1 2
(d)2 1
(e)None of above
Output: (c)
Explanation:
Size of char data type is one byte while
size of character constant is two byte.
Why character constant is of two byte in
c?
(40) What will be output if you will
compile and execute the following c code?
void main(){
char *url="c:\tc\bin\rw.c";
printf("%s",url);
}
(a)c:\tc\bin\rw.c
(b)c:/tc/bin/rw.c
(c)c: c inw.c
(d)c:cinw.c
(e)w.c in
Output: (e)
Explanation:
1. \t is tab character which moves the
cursor 8 space right.
2. \b is back space character which moves
the cursor one space back.
3. \r is carriage return character which
moves the cursor beginning of the line.
goto abc;
printf("main");
getch();
}
void dispaly(){
abc:
printf("display");
}
(a)main
(b)display
(c)maindisplay
(d)displaymain
(e)Compiler error
Output: (e)
Explanation:
Label of goto cannot be in other function
because control cannot move from one
function to another function directly
otherwise it will show compiler error:
unreachable label
What is goto keyword.
Complete function tutorial with examples.
is
either
(e)None of above
Output: (d)
Explanation:
We cannot modify the const variable by
using increment operator.
Properties of const keyword.
Properties of volatile keyword.
Data type tutorial with examples.
(45) What will be output if you will
compile and execute the following c code?
void main(){
const int x=25;
int * const p=&x;
*p=2*x;
printf("%d",x);
}
(a)25
(b)50
(c)0
(d)Compiler error
(e)None of above
Output: (b)
Explanation:
const keyword in c doesnt make any
variable as constant but it only makes
the variable as read only. With the help
of pointer we can modify the const
variable. In this example pointer p is
pointing to address of variable x. In the
following line:
int * const p=&x;
p is constant pointer while content of p
i.e. *p is not constant.
*p=2*x put the value 50
location of variable x.
at
the
memory
}
(a)11
(b) 12
(c)Garbage value
(d)Compiler error
(e)None of above
Output: (c)
Explanation:
In the following line:
int const * p=&i;
*p i.e. content of p is constant pointer
p is not constant pointer. So we can
modify the pointer p. After incrementing
the pointer it will point next memory
location and its content will any garbage
value.
Note: We
address.
have
assumed
arbitrary
memory
To make
write:
pointer
as
constant
pointer
first
solve
result
1 > c
Since this condition is false so result
will be 0. Thus else part will execute.
What is associative?
What is precedence?
(48) What will be output if you will
compile and execute the following c code?
void main(){
float f;
f=3/2;
printf("%f",f);
}
(a)1.5
(b)1.500000
(c)1.000000
(d)Compiler error
(e)None of above
Output: (b)
Explanation:
In the following expression:
f=3/2 both 3 and 2 are integer constant
hence its result will also be an integer
constant i.e. 1.
Properties of floating type numbers.
(49) What will be output if you will
compile and execute the following c code?
void main(){
int a=sizeof(a);
a=modify(a);
printf("%d",a);
}
It
if(a==5.5)
PRINT
else
printf("Not equal");
}
(a)c c++
(b)Not equal
(c)c
c++
(d)Compiler error
(e)None of above
Output: (d)
Explanation:
First see intermediate file:
try.c 1:
try.c 2: void main(){
try.c 3: float a=5.5;
try.c 4: if(a==5.5)
try.c 5: printf("c");printf("c++");
try.c 6: else
(b) 2 -6 1
(c) 2 2 1
(d) Compiler error
(e) None of these
Answer: (c)
Explanation:
Binary value of 2: 00000010 (Select three
two bit)
Binary value of 6: 00000110
Binary value of -6: 11111001+1=11111010
(Select last three bit)
Binary value of 5: 00000101 (Select last
two bit)
Complete memory representation:
Structure tutorial
More questions
(52) What will be output if you will
compile and execute the following c code?
void main(){
static
char
*s[3]={"math","phy","che"};
typedef char *( *ppp)[3];
static ppp p1=&s,p2=&s,p3=&s;
char
*
(*(*array[3]))
[3]={&p1,&p2,&p3};
char * (*(*(*ptr)[3]))[3]=&array;
p2+=1;
p3+=2;
printf("%s",(***ptr[0])[2]);
}
(a) math
(b) phy
(c) che
(d) Compiler error
(e) None of these
Answer: (c)
Explanation:
Here
ptr: is pointer to array of pointer to
string.
P1, p2, p3: are pointers to array of
string.
array[3]: is array which contain pointer
to array of string.
Pictorial representation:
}
}
else
{
printf("File not found");
}
}
Step 2: Save the as list.c (You can give
any name)
Step 3: Compile and execute the file.
Step 4: Write click on My computer of
Window XP operating system and select
properties.
Step 5: Select Advanced -> Environment
Variables
Step 6: You will find following window:
Click on new button (Button inside the
red box)
(Path
printf("%d ",*ptr++);
}
(a) -51 -52 -52 -52 -52 -52 20 64
(b) 51 52 52 52 52 52 20 64
(c) Eight garbage values.
(d) Compiler error
(e) None of these
Answer: (a)
Explanation:
In c double data type is eight byte data
type while char pointer ptr can point one
byte of memory at a time.
Memory representation of double a=5.2
Explanation:
In c string constant xy is same as x
y
String tutorial.
(63) What will be output if you will
compile and execute the following c code?
void main(){
printf("%s",__DATE__);
}
(a) Current system date
(b) Current system date with time
(c) null
(d) Compiler error
(e) None of these
Answer: (a)
Explanation:
__DATE__
is
global
identifier
which
returns current system date.
What is global identifier?
(64) What will be output if you will
compile and execute the following c code?
void main(){
char *str="c-pointer";
printf("%*.*s",10,7,str);
}
(a) c-pointer
(b) c-pointer
(c) c-point
(d) cpointer null null
(e) c-point
Answer: (e)
Explanation:
Meaning of %*.*s in the printf function:
start function: 1
main function: 2
end function:3
(c)
main function: 2
end function:3
start function: 1
(d) Compiler error
(e) None of these
Answer: (b)
Explanation:
Every c program start with main function
and terminate with null statement. But
#pragma startup can call function just
before main function and #pragma exit
What is pragma directive?
Preprocessor tutorial.
(66) What will be output if you will
compile and execute the following c code?
void main(){
int a=-12;
a=a>>3;
printf("%d",a);
}
(a) -4
(b) -3
(c) -2
(d) -96
(e) Compiler error
Answer :( c)
Explanation:
Binary value of 12 is: 00000000 00001100
Binary value of -12 wills 2s complement
of 12 i.e.
So binary
11110100
value
of
-12
is:
11111111
fill
fill
right
three
shown
Operator tutorial.
(67) What will be output if you will
compile and execute the following c code?
#include "string.h"
void main(){
clrscr();
printf("%d
%d",sizeof("string"),strlen("string"));
getch();
}
(a) 6 6
(b) 7 7
(c) 6 7
(d) 7 6
(e) None of these
Answer: (d)
Explanation:
Sizeof operator returns the size of
string including null character while
strlen function returns length of a
string excluding null character.
String tutorial.
Library functions of string.
(68) What will be output if you will
compile and execute the following c code?
void main(){
static main;
int x;
x=call(main);
clrscr();
printf("%d ",x);
getch();
}
int call(int address){
address++;
return address;
}
(a) 0
(b) 1
(c) Garbage value
(d) Compiler error
(e) None of these
Answer: (b)
Explanation:
As we know main is not keyword of c but
is special type of function. Word main
can be name variable in the main and
other functions.
What is main function in c?
(69) What will be output if you will
compile and execute the following c code?
void main(){
int a,b;
a=1,3,15;
b=(2,4,6);
clrscr();
printf("%d ",a+b);
getch();
}
(a) 3
(b) 21
(c) 17
(d) 7
(e) Compiler error
Answer: (d)
Explanation:
In c comma behaves as separator as well
as operator.
a=1, 3, 15;
b= (2, 4, 6);
In the above two statements comma is
working as operator. Comma enjoys least
precedence and associative is left to
right.
Assigning the priority of each operator
in the first statement:
Operator tutorial.
(70) What will be output if you will
compile and execute the following c code?
int dynamic(int,...);
void main(){
int x,y;
x=dynamic(2,4,6,8,10,12,14);
y=dynamic(3,6,9,12);
clrscr();
printf("%d %d ",x,y);
getch();
}
int dynamic(int s,...){
void *ptr;
ptr=...;
(int *)ptr+=2;
s=*(int *)ptr;
return s;
}
(a) 8 12
(b) 14 12
(c) 2 3
(d) Compiler error
(e) None of these
Answer: (a)
Explanation:
In c three continuous dots is known as
ellipsis which is variable number of
arguments of function. In this example
ptr is generic pointer which is pointing
to first element of variable number of
argument. After incrementing it will
point third element.
What is variable number of argument?
(71) What will be output if you will
compile and execute the following c code?
int extern x;
void main()
printf("%d",x);
x=2;
getch();
}
int x=23;
(a) 0
(b) 2
(c) 23
(d) Compiler error
(e) None of these
Answer: (c)
Explanation:
extern
variables
can
search
the
declaration of variable any where in the
program.
Properties of extern storage class.
(72) What will be output if you will
compile and execute the following c code?
void main(){
int i=0;
if(i==0){
i=((5,(i=3)),i=1);
printf("%d",i);
}
else
printf("equal");
}
(a) 5
(b) 3
(c) 1
(d) equal
(e) None of above
Answer: (c)
Explanation:
Comma operator.
Operator tutorial.
(73) What will be output if you will
compile and execute the following c code?
void main(){
int a=25;
clrscr();
printf("%o %x",a,a);
getch();
}
(a) 25 25
(b) 025 0x25
(c) 12 42
(d) 31 19
(e) None of these
Answer: (d)
Explanation:
%o is used to print the number in octal
number format.
%x is used to print the number in
hexadecimal number format.
Note: In c octal number starts with 0 and
hexadecimal number starts with 0x.
What is octal number?
What is hexadecimal number?
(74) What will be output if you will
compile and execute the following c code?
#define message "union is\
power of c"
void main(){
clrscr();
printf("%s",message);
getch();
}
(a) union is power of c
(b) union ispower of c
(c) union is
Power of c
(d) Compiler error
if(printf("cquestionbank"))
printf("I know c");
else
printf("I know c++");
}
(a) I know c
(b) I know c++
(c) cquestionbankI know c
(d) cquestionbankI know c++
(e) Compiler error
Answer: (c)
Explanation:
Return type of printf function is integer
which returns number of character it
prints including blank spaces. So printf
function inside if condition will return
13. In if condition any non- zero number
means true so else part will not execute.
Prototype of printf function.
Links to this post
1 comments
Older Posts
Subscribe to: Posts (Atom)
Index of c questions and solution
FAQ questions of c programming
Standard of questions ?
Wellcome for asking doubt or suggestion in c,c++,sql
If you have any doubt in c,c++,sql you can ask questions,if you have good questions with or
without solution or if there any error in this blog then please,please send in comment section.
to view solution of your query click here
My other blogs
Advance c programming
Write a c programming code to create simple paint brush software.
Pointer in c programming
understanding pointer in c
preprocessor in c programming
INDEX
C QUESTIONS AND ANSWER
C QUESTIONS
C OBJECTIVE QUESTIONS WITH EXPLANATION
QUESTIONS OF OPERATOR
QUESTIONS OF POINTER
QUESTIONS OF PREPROCESSOR
QUESTIONS OF STRING IN C
QUESTIONS OF FUNCTION
INDEX
C programming tutorial index
Operator
Pointer tutorial
Function
Preprocessor
TUTORIALS
SQL TUTORIAL
DOWNLOAD EBOOKS
C LOVER COMMUNITY
HUGE COLLECTIONS OF C QUESTIONS
06 (8)
o 12/31 (8)
07 (148)
o
08/26 (4)
09/02 (3)
09/09 (30)
11/04 (1)
12/02 (2)
12/30 (108)
08 (64)
o
01/06 (26)
01/20 (5)
01/27 (3)
02/03 (4)
03/30 (1)
05/04 (1)
06/08 (1)
07/06 (1)
07/13 (2)
08/10 (1)
08/31 (10)
09/07 (1)
09/14 (1)
11/30 (7)
09 (177)
o
01/11 (47)
01/18 (4)
01/25 (7)
03/01 (1)
03/22 (1)
04/05 (8)
05/31 (6)
06/07 (1)
06/14 (15)
06/21 (1)
06/28 (1)
07/12 (1)
08/16 (2)
08/30 (12)
09/06 (69)
VARIABLES IN C
VARIABLES IN C
CONTROL STATEMENT IN C
STRING TUTORIAL IN C
PREPROCESSOR TUTORIAL IN C
C LANGUAGE COMPILERS
WHAT IS FILE?
C INTERVIEW QUESTIONS
C PROGRAMMING TUTORIAL
printf questions in c
c questions
09/13 (1)
C QUESTIONS
Subscribe To
Posts
Atom
Posts
All Comments
Atom
All Comments