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");
}
}
A. XAM is printed
B. exam is printed
C. Compiler Error
D. Nothing is printed
Answer: Option D
Solution(By Examveda Team)
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
Comments ( 14 )
What does the ++ operator do in C when applied to a variable?
A. Decrements by 1
B. Adds 1
C. Doubles the value
D. Leaves it unchanged
Comma used as an operator for expression or seperator for function call/declaration/definitinion.
And 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.
Whenever the operands are separated by comma and within parenthesis the rightmost value is considered
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
Comma acts as both separator and operator. In (a,b,x,y) comma is an operator and the value that works is the right most one "y" (if the expression is in this format always choose the right most one ) . Given that 'y' value is 0. So if (0) means it is false then control will go to else block if any. In the given snippet no else was there .Then the final result will be a blank . Therefore Nothing is printed..
Here , if have a condition and if that condition is true the if loop body is printed. but logic of if is nothing So control not going inside the if loop.
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.
in case of c=a,b it evalutes first variable but if it is c=(a,b) it evaluates last one?
comma operator produces the result as second operand so a,b,x,y will produce 'y' which is equal to zero
How this answer
i cant uderstand
how it is possible??
how it is possible
in if
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.
D answers
WHAT IS THE ACTUAL LOGIC BEHIND THIS PROGRAM
logic ???
i cant understand clearly...
please explain with each step...