?
What will be the output?
What will be the output?
void main(){
int a=10, b=20;
char x=1, y=0;
if(a,b,x,y){
printf("EXAM");
}
}
void main(){
int a=10, b=20;
char x=1, y=0;
if(a,b,x,y){
printf("EXAM");
}
}
Answer & Solution
Correct Answer:
Option
D
The correct answer is D. Nothing is printed.
In the given code, the if statement condition consists of multiple expressions separated by commas (,). The comma operator evaluates each expression from left to right and returns the result of the last expression.
In this case, the expressions a, b, x, and y are used as the condition. Since they are non-zero values (10, 20, 1, and 0 respectively), the condition is considered true. However, the comma operator only returns the result of the last expression (y), which is 0 (false).
Since the condition evaluates to false, the code block within the if statement (printf("EXAM");) is not executed, resulting in nothing being printed.
Join the Discussion
Login to post a comment or share your explanation.
LoginAnd Comma is a binary operator also.
So, due to the property of binary operator.
1) first operands evaluate and discard its result.
2) second operand evaluate and return it.
By which if (a, b, x, y,) it return 0 so.
Equation become if (0).
So, nothing is printed.
Hence, option d is the correct one.
In this case y is the rightmost operand and 0 is its value therefore if(a,b,x,y)-->if y-->if 0-->false hence nothing gets printed
and compiler's work is to check the syntax and symantic error it is not check the logic of program. So here compiler is also not produce any error.
how it is possible??
we have a,b,x,y here the we have the comma operator it has associativity of right to left so it considers the first element from the right and others are ignored so the statement becomes if(y)
her y=0
so the if loop does not execute.
hence nothing will be printed.
please explain with each step...