Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

C Interview Questions And Answ ers

[C Frequently

Asked Questions ,C FAQ ]

What will print out?


main()
{
char *p1=name;
char *p2;
p2=(char*)malloc(20);
memset (p2, 0, 20);
while(*p2++ = *p1++);
printf(%s\n,p2);
}
The pointer p2 value is also increasing with p1 .
*p2++ = *p1++ means copy value of *p1 to *p2 , then increment both addresses
(p1,p2) by one , so that they can point to next address . So when the loop exits (ie
when address p1 reaches next character to name ie null) p2 address also points to
next location to name . When we try to print string with p2 as starting address , it
will try to print string from location after name hence it is null string .
e.g. :
initially p1 = 2000 (address) , p2 = 3000
*p1 has value n ..after 4 increments , loop exits at that time p1 value will be
2004 , p2 =3004 the actual result is stored in 3000 - n , 3001 - a , 3002 - m ,
3003 -e we r trying to print from 3004 . where no data is present that's why
its printing null .
Answer: empty string.

What will be printed as the result of the operation below:


main()
{
int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf(%d%d\n,x,y)
;
}
Answer : 5794

What will be printed as the result of the operation below:


main()
{

int x=5;
printf(%d,%d,%d\n,x,x<<2,>>2)
;
}
as x = 5 = 00000,0101; so x << 2 -< 00001,0100 = 20; x >7gt; 2 -> 00000,0001
= 1. Therefore, the answer is 5, 20 , 1
Answer: 5,20,1

What will be printed as the result of the operation below:

#define swap(a,b) a=a+b;b=a-b;a=a-b;


void main()
{
int x=5, y=10;
swap (x,y);
printf(%d %d\n,x,y)
; swap2(x,y);
printf(%d %d\n,x,y)
;}
int swap2(int a, int b)
{
int temp;
temp=a;
b=a;
a=temp;
return 0;
}

the correct answer is


10, 5
5, 10

Answer: 10, 5

What will be printed as the result of the operation below:


main()
{
char *ptr = Tech Preparation;
*ptr++; printf(%s\n,ptr)
; ptr++;

printf(%s\n,ptr);
}
1) ptr++ increments the ptr address to point to the next address. In the previous
example, ptr was pointing to the space in the string before C, now it will point to C.
2)*ptr++ gets the value at ptr++, the ptr is indirectly forwarded by one in this case.
3)(*ptr)++ actually increments the value in the ptr location. If *ptr contains a space,
then (*ptr)++ will now contain an exclamation mark.
Answer: Tech Preparation

What will be printed as the result of the operation below:


main()
{
char s1[]=Tech;
char s2[]= preparation;
printf(%s,s1)
;}
Answer: Tech

What will be printed as the result of the operation below:


main()
{
char *p1;
char *p2;

p1=(char *)malloc(25);
p2=(char *)malloc(25);
strcpy(p1,Tech);
strcpy(p2,preparation);
strcat(p1,p2);
printf(%s,p1)
;
}
Answer: Techpreparation

The following variable is available in file1.c, who can access it?: static int
average;
Answer: all the functions in the file1.c can access the variable.

You might also like