ExamVeda
Login
Home
Operators and Expressions
?

What will be the output?
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.

Examveda
Question posted by Examveda
Community

Join the Discussion

14 Comments
Vivek Prakash
Vivek Prakash 3 years ago
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.
Kakon Chattaraj
Kakon Chattaraj 6 years ago
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
Sudheer Sudheer
Sudheer Sudheer 6 years ago
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..
MITTU SHARMA
MITTU SHARMA 8 years ago
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.
GAMERS SPOT
GAMERS SPOT 9 years ago
in case of c=a,b it evalutes first variable but if it is c=(a,b) it evaluates last one?
Assi At
Assi At 9 years ago
comma operator produces the result as second operand so a,b,x,y will produce 'y' which is equal to zero
Suyash Hedaoo
Suyash Hedaoo 9 years ago
How this answer
Ajinkya Londhe
Ajinkya Londhe 9 years ago
i cant uderstand
how it is possible??
Harish Reddy
Harish Reddy 9 years ago
how it is possible
Devanahalli Shravya
Devanahalli Shravya 9 years ago
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.
Naresh Patel
Naresh Patel 10 years ago
D answers
Palash Jain
Palash Jain 10 years ago
WHAT IS THE ACTUAL LOGIC BEHIND THIS PROGRAM
Namit Sahu
Namit Sahu 10 years ago
logic ???
Arun Kumar
Arun Kumar 10 years ago
i cant understand clearly...
please explain with each step...