Find The Outputs for Questions:
Que 1.
int main()
{
int x,y=2,z;
if ( x = y%2)
z =2;
a=2;
printf("%d %d ",z,x);
return 0;
}
Output: Garbage 0
Explanation:
This question has some stuff for operator precedence. If the condition of if is met, then z will be initialized to 2 otherwise z will contain garbage value. But the condition of if has two operators: assignment operator and modulus operator. The precedence of modulus is higher than assignment. So y%2 is zero and it’ll be assigned to x. So the value of x becomes zero which is also the effective condition for if. And therefore, condition of if is false.
Que 2.
int main()
{
int a[10];
printf("%d",*a+1-*a+3);
return 0;
}
Output: 4
Explanation:
From operator precedence, de-reference operator has higher priority than addition/subtraction operator. So de-reference will be applied first. Here, a is an array which is not initialized. If we use a, then it will point to the first element of the array. Therefore *a will be the first element of the array. Suppose first element of array is x, then the argument inside printf becomes as follows. It’s effective value is 4.a + 1 – a + 3 = 4
Que 3.
int main()
{
static int i=5;
if(--i){
main();
printf("%d ",i);
}
}
Output: 0 0 0 0
Explanation:
Since i is a static variable and is stored in Data Section, all calls to main share same i.
Que 4.
int main()
{
int x;
printf("%d",scanf("%d",&x));
/* Suppose that input value given
for above scanf is 2 */
return 1;
}
Output: 1
Explanation:
scanf returns the no. of inputs it has successfully read.
Que 5.
int main()
{
unsigned int i=65000;
while ( i++ != 0 );
printf("%d",i);
return 0;
}
Output: 1
Explanation:
It should be noticed that there’s a semi-colon in the body of while loop. So even though, nothing is done as part of while body, the control will come out of while only if while condition isn’t met. In other words, as soon as i inside the condition becomes 0, the condition will become false and while loop would be over. But also notice the post-increment operator in the condition of while. So first i will be compared with 0 and i will be incremented no matter whether condition is met or not. Since i is initialized to 65000, it will keep on incrementing till it reaches highest positive value. After that roll over happens, and the value of i becomes zero. The condition is not met, but i would be incremented i.e. to 1. Then printf will print 1.