Interview Questions

C Back

main()
{
int i=5;
printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
}

Ans: 

45545

The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result..

main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}

Ans: 

hai

\n - newline,\b - backspace,\r - linefeed.

main()
{
struct xx {
int x;
struct yy {
char s;
struct xx *p;
};
struct yy *q;
};
}

Ans: 

Compiler Error

The structure yy is nested within structure xx. Hence, the elements are of yy are to be accessed through the instance of structure xx, which needs an instance of yy to be known. If the instance is created after defining the structure the compiler will not know about the instance relative to xx. Hence for nested structure yy you have to declare member..

#include main()
{
struct xx {
int x=3;
char name[]="hello";
};
struct xx *s;
printf("%d",s->x);
printf("%s",s->name);
}

Ans: 

Compiler Error

You should not initialize variables in declaration.

#include main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d----%d",*p,*q);
}

Ans: 

:SomeGarbageValue---1

p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access the third 2D(which you are not declared) it will print garbage values. *q=***a starting address of a is assigned integer pointer. Now q is pointing to starting address of a. If you print *q, it will print first element of 3D array..

#include main()
{
char s[]={'a','b','c','\n','c','\0'};
char *p,*str,*str1; p=&s[3]; str=p;
str1=s; printf("%d",++*p + ++*str1-32);
}

Ans: 

77

p is pointing to character '\n'. str1 is pointing to character 'a' ++*p. "p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10, which is then incremented to 11. The value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. Now performing (11 + 98 – 32), we get 77("M");So we get the output 77 :: "M" (Ascii is 77)..

main()
{
int i=10;
i=!i>14;
printf("i=%d",i);
}

Ans: 

i=0

In the expression !i>14 , NOT (!) operator has more precedence than ‘ >' symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero)..

#defineint char main() {
int i=65;
printf("sizeof(i)=%d",sizeof(i));
}

Ans: 

sizeof(i)=1

Since the #define replaces the string int by the macro char .

main() {
printf("%x",-1<<4);
}

Ans: 

fff0

1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are filled with 0's.The %x format specifier specifies that the integer value be printed as a hexadecimal value.

main()
{
int i=3;
switch(i) {
default:printf("zero");
case 1: printf("one");
break;
case 2:printf("two");
break;
case 3: printf("three"); break;
}
}

Ans: 

Answer :three

The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.