Examveda
Examveda

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


This Question Belongs to C Program >> Operators And Expressions

Join The Discussion

Comments ( 14 )

  1. Vivek Prakash
    Vivek Prakash :
    11 months 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.

  2. Kakon Chattaraj
    Kakon Chattaraj :
    3 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

  3. Sudheer Sudheer
    Sudheer Sudheer :
    4 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..

  4. MITTU SHARMA
    MITTU SHARMA :
    6 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.

  5. GAMERS SPOT
    GAMERS SPOT :
    6 years ago

    in case of c=a,b it evalutes first variable but if it is c=(a,b) it evaluates last one?

  6. Assi At
    Assi At :
    7 years ago

    comma operator produces the result as second operand so a,b,x,y will produce 'y' which is equal to zero

  7. Suyash Hedaoo
    Suyash Hedaoo :
    7 years ago

    How this answer

  8. Ajinkya Londhe
    Ajinkya Londhe :
    7 years ago

    i cant uderstand
    how it is possible??

  9. Harish Reddy
    Harish Reddy :
    7 years ago

    how it is possible

  10. Devanahalli Shravya
    Devanahalli Shravya :
    7 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.

  11. Naresh Patel
    Naresh Patel :
    8 years ago

    D answers

  12. Palash Jain
    Palash Jain :
    8 years ago

    WHAT IS THE ACTUAL LOGIC BEHIND THIS PROGRAM

  13. Namit Sahu
    Namit Sahu :
    8 years ago

    logic ???

  14. Arun Kumar
    Arun Kumar :
    8 years ago

    i cant understand clearly...
    please explain with each step...

Related Questions on Operators and Expressions