Question:What will be output when you will execute following c code?#include<stdio.h>
void main(){
int a=5,b=10;
clrscr();
if(a<++a||b<++b)
printf("%d %d",a,b);
else
printf("John Terry");
}
Choose all that apply:
+ ExplanationConsider the following expression:
a<++a||b<++b
In the above expression || is logical OR operator. It divides any expression in the sub expressions. In this way we have two sub expressions:
(1) a<++a
(2) b<++b
In the expression: a< ++a
There are two operators. There precedence and associate are:Precedence | Operator | Associate |
---|
1 | ++ | Right to left |
2 | < | Left to right |
From table it is clear first ++ operator will perform the operation then < operator.
One important property of pre-increment (++) operator is: In any expression first pre-increment increments the value of variable then it assigns same final value of the variable to all that variables. So in the expression: a < ++a
Initial value of variable a is 5.
Step 1: Increment the value of variable a in whole expression. Final value of a is 6.
Step 2: Now start assigning value to all a in the expression. After assigning 6 expression will be:
6 < 6
Since condition is false .So second expression i.e. b<++b will be evaluated. Again 11 < 11 is false. So || will operator will return zero and else clause will execute.