Google Search

Friday, November 13, 2015

C Aptitude Question: 1

‪#‎include‬<stdio.h>
int main()
{
int x, y, z;
x=y=z=-1;
z = ++x || ++y && ++z;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
What will be the output?
Answer: x=0 y=0 z=0
Step 1: x=y=z=-1; here the variables x ,y, z are initialized to value '-1'.
Step 2: z = ++x || ++y && ++z; becomes z = ( (++x) || (++y && ++z) ). Here ++x becomes 0. So there is no need to check the other side because ||(Logical OR) condition is satisfied.(z = (0 || ++y && ++z)). There is no need to process ++y && ++z. Hence it returns '0'. So the value of variable z is '0'
Step 3: printf("x=%d, y=%d, z=%d\n", x, y, z); It prints "x=0, y=0, z=0". here x is increemented in previous step. y and z are not increemented.

No comments:

Post a Comment