Home / C Programming / Expressions :: Discussion

Discussion :: Expressions

  1. What will be the output of the program?

    #include 
    int main() 
    {    
        int i=2;    
        int j = i + (1, 2, 3, 4, 5);   
        printf("%d\n", j);     
        return 0;
     }
    

  2. A.

    4

    B.

    7

    C.

    6

    D.

    5

    View Answer

    Workspace

    Answer : Option B

    Explanation :

    Because, comma operator used in the expression i (1, 2, 3, 4, 5). The comma operator has left-right associativity. The left operand is always evaluated first, and the result of evaluation is discarded before the right operand is evaluated. In this expression 5 is the right most operand, hence after evaluating [removed]1, 2, 3, 4, 5) the result is 5, which on adding to i results into 7.


Be The First To Comment