Search Your Question
Thursday, July 24, 2008
C programming questions!!
Well, to all the C programmers out there.. here are some of the questions which I
bet you would surely enjoy solving...
1)
Find the bug in this code.
#include
int main()
{
int i=3,j=5;
printf("%d",(i+j)++);
return(0);
}
Ans:
2)
What is the output of the following piece of code?
#include
int main()
{
char c='8';
int d=8;
printf("%d %d %d",d,d+=c>='0'&&c<='9',c++);
return(0);
}
Ans:
3)
x=x*y+1; --------------1
Is this different from x=x*(y+1); --------------2
How to right the assignment operation "2" with out using
parenthesis in one statement.
Ans:
4)
What does this code do?
#include
void func(char s[],int c)
{
int i,j;
for(i=j=0;s[i]!='\0';i++)
if(s[i]!=c)
s[j++]= s[i] + s[i]==c ;
s[j]='\0';
}
int main()
{
char s[]="aelloworld";
func(s,'k');
printf("%s",s);
return(0);
}
Ans:
5)
Can you find any difference between the two scanf calls made in the
code given below ?
#include
int main()
{
int x,y;
scanf("%d",&x);
scanf(" %d",&y);
return 0;
}
Ans:
6)
Predict the output of the following piece of code.
Assume that bit position 0 is at the right end and that i and j are
sensible positive values.
#include
unsigned getbits(unsigned a,int i,int j)
{
return(a>>(i+1-j)) & ~(~0<
int main()
{
unsigned num=128;
printf("%d\n",getbits(num,7,5));
return(0);
}
Ans:
7)
What does this code do on input 'abcd' from console?
#include
int main()
{
char c;
while(c=getchar()!='\n')
printf("%d",c);
return(0);
}
Ans:
8)
What does this code do?
#include
int main()
{
int i;
int a[20]={1,2,3,4,5,6,7,8,9,10,10,9,8,7,6,1,2,3,4,5};
int n=20;
for(i=0;i<20;i++)
{
printf("%6d%c",a[i],(i%10==9 || i== 19)?'\n':' ');
}
return(0);
}
Ans:
9)
Will this code work properly? If yes why? If not why not?
#include
void swap(int *a,int *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
int main()
{
int a=3617283450;
int b=3617283449;
swap(&a,&b);
printf("%d %d\n",a,b);
return(0);
}
Ans:
10)
What is the output of the following piece of code?
#include
int main()
{
int a=3,b=1;
a = b<>2;
b = b<>2;
printf("%d %d",a,b);
return 0;
}
Ans:
11)
Can you predict the output of the following code snippet ?
#include
int main()
{
int i=9876;
printf("%d\n",printf("%d",printf("%d",i)));
return 0;
}
Ans:
12)
Give the output of the following code.
#include
typedef struct s
{
int a[10];
}s;
typedef struct t
{
int *a;
}t;
int main()
{
s s1,s2;t t1;
s1.a[0]=0;
t1.a=s1.a;
s1.a[0]++;
s2=s1;
s1.a[0]++;
printf("%d %d %d",s1.a[0],s2.a[0],t1.a[0]);
return 0;
}
Ans:
13)
You just have to write the declaration of an unsigned int p;
Obviously you can't use any spaces or new lines in the declaration !!
Ans:
14)
What is the output of the following code?
#include
int main()
{
int x=2,y=3,z1,z2;
z1=x + + + y;
z2=x+++y;
printf("%d %d %d %d",x,y,z1,z2);
return 0;
}
Ans:
15)
Predict the output of the following code with the following inputs ->
1. 2c3
2. 2 5
#include
#include
int main()
{
int *i=0,*j=4;
i=(int*)malloc(sizeof(int));
scanf("%dc%d",&i,&j);
printf("%d %d",i,j);
return(0);
}
Ans:
16)
What is the output of the following code for the input "dcba"?
#include
#include
int main()
{
char c;
while (c=getchar()!='a') printf("%d",c);
return 0;
}
Ans:
17)
What is the output of the following code?
#include
int array[]={1,2,3,4,5,6,7,8};
#define SIZE (sizeof(array)/sizeof(int))
int main()
{
printf("%d",SIZE);
if(-1<=SIZE) printf("1");
else printf("2");
return 0;
}
Ans:
18)
What is the output of the following code?
#include
#include
int main()
{
char a[]="aaa";
char *b="bbb";
strcpy(a,"cc");
printf("%s",a);
strcpy(b,"dd");
printf("%s",b);
return 0;
}
Ans:
19)
Can you predict the output of the following code snippet ?
#include
#define sq(x) x*x
int main()
{
int a=3;
printf("%d\n",sq(a+3));
}
Ans:
20)
What is the output of this code?
#include
int main()
{
int *i=0;
printf(" %p\n",i);
return(0);
}
Ans:
21)
Does the following C code compile ???? If it does .. can you
explain what happens when you run it with some arbitrary text
as input or what does it do ????
[ The input is redirected from a file called "input.txt" ]
int main()
{
for(;scanf("%*[^a-z]")+1;putchar(getchar()));
}
Ans:
22)
What is the output of the following code?
#include
int main()
{
int a=1,b=3,c,d;
c=(a,b);
d=a,b,c;
printf("%d %d",c,d);
return 0;
}
Ans:
23)
What is the expected output of the code ?????
#include
#define concatinate(a,b) a##b
#define same1(a) #a
#define same2(a) same1(a)
int main()
{
printf("%s\n",same2(concatinate(1,2)));
printf("%s\n",same1(concatinate(1,2)));
return 0;
}
Ans:
24)
The intention of the following program was to print 42 astericks('*')
But it fails to do so.
You have to add/replace/delete exactly one character in the program
to make it work ???
Find as many solutions as possible.
#include
int main()
{
int n = 42;
for(int i = 0; i < n; i-- )
printf("*");
return 0;
}
Ans:
25)
What does this code do?
#include
void insert(char s[],int c)
{
int i,j;
for(i=j=0;s[i]!='\0';i++)
if(s[i]!=c)
s[j++]= s[i];
s[j]='\0';
}
int main()
{
char s[]="helloworld";
insert(s,'l');
printf("%s",s);
return(0);
}
Ans:
26)
What is the output of the following code?
#include
#include
int s(char *A[20],char *B[20])
{
char *a,*b;
a=A;b=B;
while(*a++!=*b++); *a=*b='\0';
return strlen(A);
}
int main()
{
char A[20]="somestring",
B[20]="debugthecbug";
printf("%d %s %s\n",s(&A,&B),A,B);
return 0;
}
Ans:
27)
Can you predict the output ?
#include
#define print(var) printf("%s : %d\n",#var,(var))
int main()
{
int y = 100;
int *p;
p = new int;
*p = 10;
y = y/*p; /*dividing y by *p */;
print(y);
return 0;
}
Ans:
28)
Take the input as 4, and write the output of the following code.
#include
main()
{
int i=0;
printf("%d %d\n",scanf(" %d",&i),i);
printf("%d %d\n",i=4,i);
}
Ans:
29)
Predict the output of the following program.
#include
int main()
{
int a=3, b = 5;
printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
printf(&a["WHAT%c%c%c\n%c%c\n%c !\n"], 1["this"],
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
return 0;
}
Ans:
30)
What is the output of the following code?
#include
#include
int main()
{
int *ptr=(int*)malloc(sizeof(int));
*ptr=4;
printf("%d",(*ptr)+++*ptr++);
return(0);
}
Ans:
31)
What is the output of the following piece of code?
#include
int main(int k)
{
if(k<10)
printf("%d ",main(k+1));
return k;
}
Ans:
32)
Predict the output or error if any in the following code.
#include
main()
{
int i = 4;
printf("%d %d %d",++i,i++,i*i);
printf("\n");
printf("%d %d %d",i*i,++i,i++);
system("PAUSE");
}
Ans:
33)
Give the output of the following piece of code.
#include
int main()
{
int a,b,c=2,d=10;
printf("%d\n ",scanf("%d%d%d",&a,&b,&c,&d) ) ;
printf("%d\n%d\n%d\n%d\n",a,b,c);
return(0);
}
Ans:
34)
#include
main()
{
int a[15][10][5]={1};
func(a);
}
Which of these is/are correct function prototypes for func ?
void func( int a[][10][5] );
void func( int a[15][][5] );
void func( int a[15][10][] );
Ans:
35)
Give the output of the following piece of code.
#include
main()
{
float x=3.14;
printf("%e, %E, %f",x,x,x);
}
Ans:
36)
What is the output of the following program?
#include
int fun1()
{
static int c=20;
return --c;
}
int fun2()
{
static int c=1;
return fun1()+c--;
}
int main()
{
int i=0;
while(i
return 0;
}
Ans:
37)
What is the output of the following program?
#include
#include
void p(char *a)
{
static int y=1;
if(y=1-y) printf("%c",*a);
return;
}
int main()
{
char *a;a=(char*)malloc(20*sizeof(char));
strcpy(a,"DbugtheCbug");
while(*a!='\0') p(a++);
printf("\n");
return 0;
}
Ans:
38)
Give the output of the following piece of code.
#include
main()
{
int a=4,b=10;
printf("%d %d %d %d\n",a,a^=b=b^=a=a^=b,b,printf("%d %d %d\n",b,a,a^=b=b^=a=a^=b));
}
Ans:
39)
Predict the output of the following program.
#include
int main()
{
int x = 5,p = 10;
printf("%*d",x,p);
}
Ans:
40)
What is the output of the following code?
#include
int main()
{
int i=2;
i=i++;
printf("%d ",i++ + i++);
}
Ans:
41)
Predict the output for the below code.
#include
int main()
{
int a[]={2,3,4,5,6};
int i=0;
printf("%d",a[i++]+i[a+1]);
return(0);
}
Ans:
42)
What is the output of the following piece of code?
#include
#include
void weird(int *a)
{
a=(int*)malloc(sizeof(int));
}
int main()
{
int *a;
weird(a);
*a=6;
printf("%d\n",*a);
return(0);
}
Ans:
43)
What is the output of the following piece of code?
#include
#include
int main()
{
int *ptr=(int*)malloc(sizeof(int));
*ptr=4;
printf("%d",*ptr++);
return(0);
}
Ans:
44)
Correct the following code in case of any errors.
#include
#include
#define SIZE 15
int main()
{
int *a, i;
a = (void*)malloc(SIZE*sizeof(int));
for (i=0; i
for (i=0; i
free(a);
return 0;
}
Ans:
45)
What is the output of the following program?
#include
int main()
{
char c;
c=255;
printf("%d",c);
return(0);
}
Wednesday, July 23, 2008
C Programming Puzzles 6.
126)
#include
main()
{
char c='6';
int d=6;
printf("%d %d %d",d,d+=c>'0'&&c<='7',c++);
}
127)
#include
main()
{
int a=4,b=2;
a=b<>2;
b=b<>2;
printf("%d %d",a,b);
}
128)
#include
main()
{
int i=98765;
printf("%d\n",printf("%d",printf("%d",i)));
}
129)
#include
main()
{
char c;
while(c=getchar()!='a')printf("%d",c);
}
//INPUT=dcba
130)
#include
main()
{
int *i=0;
printf(" %p\n",i);
}
131)
#include
#include
main()
{
int *ptr=(int*)malloc(sizeof(int));
*ptr=4;
printf("%d",(*ptr)+++*ptr++);
}
132)
#include
unsigned getbits(unsigned a,int i,int j)
{
return(a>>(i+1-j)) & ~(~0<
main()
{
unsigned num=128;
printf("%d\n",getbits(num,7,5));
}
133)
#include
#define sq(x) x*x
main()
{
int a=5;
printf("%d\n",sq(a+5));
}
134)
#include
#define concatinate(a,b) a##b
#define same1(a) #a
#define same2(a) same1(a)
main()
{
printf("%s\n",same2(concatinate(1,2)));
printf("%s\n",same1(concatinate(1,2)));
}
135)
#include
main()
{
int a=1,b=3,c,d;
c=(a,b);
d=a,b,c;
printf("%d %d",c,d);
}
136)
#include
#include
main()
{
int*ptr=(int*)malloc(sizeof(int));
*ptr=4;
printf("%d",(*ptr)+++*ptr++);
}
137)
#include
main()
{
int x;
x=20;
printf("x:%d\n",x);
printf("sizeof(x++) is: %d\n",sizeof(x++));
printf("x:%d\n",x);
}
138)
#include
#define f(x,y) x##y
#define g(x) #x
#define h(x) g(y)
int main()
{
printf("%s\n",h(f(2,3)));
printf("%s\n",g(f(2,3)));
return 0;
}
139)
#include
int main()
{
int i;
i = 10,20,30;
printf("i:%d\n",i);
}
140)
#include
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int Shiftfn(int a)
{
int t;
t = a<<2 + a;
return t;
}
int main()
{
int i = 1, j = 2,k = 3;
PrintInt(Shiftfn(i));
PrintInt(Shiftfn(j));
PrintInt(Shiftfn(k));
}
141)
#include
enum {false,true};
int main()
{
int i=1;
do
{
printf("%d\n",i);
i++;
if(i < 15)
continue;
}while(false);
}
142)
#include
int main()
{
float a = 12.5;
printf("%d\n",a);
printf("%d\n", *(int *)&a);
return 0;
}
143)
#include
int main()
{
int a=1;
switch(a)
{ int b=20;
case 1: printf("b is %d\n",b);
break;
default:printf("b is %d\n",b);
break;
}
return 0;
}
144)
#include
#include
int s(char*A[20],char*B[20])
{
char *a,*b;
a=A,b=B;
while(*a++!=*b++);
*a=*b='\0';
return strlen(A);
}
int main()
{
char A[20]="somestring",B[20]="debugthecbug";
printf("%d %s %s\n",s(&A,&B),A,B);
return 0;
}
145)
#include
void insert(char a[],int n)
{
int i,j;
for(i=j=0;a[i]!='\0';i++)
if(a[i]!=n)
a[j++]=a[i];
a[j]='\0';
}
main()
{
char a[]="helloworld";
insert(a,'l');
printf("%s",a);
}
146)
#include
#include
void weird(int*a)
{
a=(int*)malloc(sizeof(int));
}
main()
{
int*a;
weird(a);
*a=6;
printf("%d\n",*a);
}
147)
#include
main()
{
int a=4,b=10;
printf("%d %d %d %d\n",a,a^=b=b^=a=a^=b,b,printf("%d %d %d\n",
b,a,a^=b=b^=a=a^=b));
}
148)
#include
int fun1()
{
static int c=20;
return --c;
}
int fun2()
{
static int c=1;
return fun1()+c--;
}
int main()
{
int i=0;
while(i
return 0;
}
149)
#include
#include
main()
{
char a[]="aaa";
char *b="bbb";
strcpy(a,"cc");
printf("%s",a);
strcpy(b,"dd");
printf("%s",b);
}
150)
#include
int array[]={1,2,3,4,5,6,7,8};
#define SIZE (sizeof(array)/sizeof(int))
int main()
{
printf("%d",SIZE);
if(-1<=SIZE) printf("1");
else printf("2");
return 0;
}