Tuesday, 23 April 2013

sizeof()


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


________________________________________
Explanation:
Default type of floating point constant is double. So 5.2 is double constant and its size is 8 byte.

for loop example with semicolon.



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


________________________________________
Explanation: 6
Body of for loop is optional. In this question for loop will execute until value of variable x became six and condition became false.

Thursday, 18 April 2013

printf("%d",printf("%s",str));


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) 11Hello world

(B) 10Hello world

(C) Hello world10

(D) Hello world11

(E) Compiler error


________________________________________
Explanation:
Return type of printf function is integer and value of this integer is exactly equal to number of character including white space printf function prints. So, printf(“Hello world”) will return 11.

printf("%d %d %d",a,a++,++a);


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


________________________________________
Explanation:
In c printf function follows cdecl parameter passing scheme. In this scheme parameter is passed from right to left direction.

So first ++a will pass and value of variable will be a=10 then a++ will pass now value variable will be a=10 and at the end a will pass and value of a will be a=12.

Misplaced break statement error


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



Explanation:
Keyword break is not part of if-else statement. Hence it will show compiler error: Misplaced break