C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Find Output of Program (Q.No. 20)
20.
What will be the output of the program?
#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;
}
x=2, y=1, z=1
x=2, y=2, z=1
x=2, y=2, z=2
x=1, y=2, z=1
Answer: Option
Explanation:

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 2. So there is no need to check the other side because ||(Logical OR) condition is satisfied.(z = (2 || ++y && ++z)). There is no need to process ++y && ++z. Hence it returns '1'. So the value of variable z is '1'

Step 3: printf("x=%d, y=%d, z=%d\n", x, y, z); It prints "x=2, y=1, z=1". here x is increemented in previous step. y and z are not increemented.

Discussion:
19 comments Page 2 of 2.

Nagaganesh said:   1 decade ago
How ++x become 2?

How should you assign ++x value to z?

How is ++x value true?

PRERNA said:   1 decade ago
&& operator has a higher precedence over ||
Hence the latter part of the expression will be evaluated first.
y and z will also be incremented to 2 each.
Therefore x=y=z=2 must be the answer!!

Lakshmi said:   1 decade ago
How to use && and ||? What is the result of bitwise operators? Please tell me.

Sumit said:   1 decade ago
In books it is written that && and || have the same precedence and left to right associativity ... but here explained that && has higher precedence than || is it right ????

Surender said:   1 decade ago
z = ++x || ++y && ++z;
step 1 :
Operator && having higher precedence compare to ||.
So, z = (++x) || (++y && ++z);
Step 2 :
Click the link & read first, second points.
http://msdn.microsoft.com/en-US/library/azk8zbxd(v=VS.80).aspx

According to this left side of || operator should be executed first, if its nonzero then reamining expression is not evaluted.
So, z = (2) || (++y && ++z);

Step 3:
Any nonzero value is considered as true.
So, z = (TRUE) || (++y && ++z);
Hence z = TRUE.
false means 0
true means 1
So, z=1.

Remaining right part of || is remains unevaluted.

Rupinder said:   1 decade ago
But high precedence is of && operator so obviously it will processed first followed by ||.So why not to consider ++y and ++z.

Rashmi said:   1 decade ago
y is also increment.. y will be 2..

Pras said:   1 decade ago
This is compiler Dependant, between two sequence point the z is getting modified twice. Thus the output is unpredictable.

Kavita said:   1 decade ago
The statement : z = ( (++x) || (++y && ++z) )

explains z = (2 || not compulsory to evaluate)

Now, ( (++x) || (++y && ++z) ) gives the result as Boolean 1 or 'true' because the first condition is satisfied and || operator is used, hence, Z = 1.

But still has a confusion in evaluating the expression because it starts from right hand side which means that y and z should be incremented first.


Post your comments here:

Your comments will be displayed after verification.