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;
}
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 1 of 2.
Shubham Saha said:
7 years ago
Thanks for the explanation @Arindam Paul.
Mohammed Rafeeq said:
7 years ago
But ++ operator has higher precedence over logical && so why x, y, z are incremented first?
(1)
ARINDAM PAUL said:
7 years ago
@All.
The explanation is;
1) && has higher priority than ||
2) so in ++x||++y&&++z , it becomes (++x||++y)&&(++z)
3) now , in && operator , the evaluation is from left to right, i.e the left is evaluated first, ++x||++y
4) In (++x)||(++y) again the left part is evaluated first i.e ++x, so it gives x= 2, 2||++y , since its OR, if left is non zero then the right is not evaluated and it returns TRUE
5) hence (TRUE)&&(++z) , now since its AND operator the right part is also evaluated, so it gives z=2,
TRUE||2 which also gives TRUE, hence z=++x||++y&&++z , gives z= TRUE i.e z=1,
So, x=2,y=1,z-1 as y was never evaluated.
The explanation is;
1) && has higher priority than ||
2) so in ++x||++y&&++z , it becomes (++x||++y)&&(++z)
3) now , in && operator , the evaluation is from left to right, i.e the left is evaluated first, ++x||++y
4) In (++x)||(++y) again the left part is evaluated first i.e ++x, so it gives x= 2, 2||++y , since its OR, if left is non zero then the right is not evaluated and it returns TRUE
5) hence (TRUE)&&(++z) , now since its AND operator the right part is also evaluated, so it gives z=2,
TRUE||2 which also gives TRUE, hence z=++x||++y&&++z , gives z= TRUE i.e z=1,
So, x=2,y=1,z-1 as y was never evaluated.
(1)
Reema said:
9 years ago
&& should be executed first right?
Sagar said:
1 decade ago
Between ||, && operators first && will be evaluated first.
Why is is not evaluated first? Anybody please answer thank you advance.
Why is is not evaluated first? Anybody please answer thank you advance.
Anil said:
1 decade ago
As && operator has higher precedence than || operator,
Why it is not evaluated first?
Why it is not evaluated first?
Harry said:
1 decade ago
Postfix increment/decrement have high precedence, but the actual increment or decrement of the operand is delayed (to be accomplished sometime before the statement completes execution).
So in the statement y = x * z++; the current value of z is used to evaluate the expression (i.e., z++ evaluates to z) and z only incremented after all else is done.
So in the statement y = x * z++; the current value of z is used to evaluate the expression (i.e., z++ evaluates to z) and z only incremented after all else is done.
Guarvkumar said:
1 decade ago
Still the Question is what happen to the presidency why ++ are not evaluated first even though they having highest rank among operator given here. !
Manju said:
1 decade ago
@Lakshmi.
The logical-AND operator produces the value 1 if both operands have nonzero values. If either operand is equal to 0, the result is 0. If the first operand of a logical-AND operation is equal to 0, the second operand is not evaluated.
The logical-OR operator performs an inclusive-OR operation on its operands. The result is 0 if both operands have 0 values. If either operand has a nonzero value, the result is 1. If the first operand of a logical-OR operation has a nonzero value, the second operand is not evaluated.
The logical-AND operator produces the value 1 if both operands have nonzero values. If either operand is equal to 0, the result is 0. If the first operand of a logical-AND operation is equal to 0, the second operand is not evaluated.
The logical-OR operator performs an inclusive-OR operation on its operands. The result is 0 if both operands have 0 values. If either operand has a nonzero value, the result is 1. If the first operand of a logical-OR operation has a nonzero value, the second operand is not evaluated.
Manju said:
1 decade ago
Why we can't take it as ((++x || ++y) && (++z) ) ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers