C Programming - Expressions - Discussion

Discussion Forum : Expressions - Find Output of Program (Q.No. 15)
15.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=2;
    int j = i + (1, 2, 3, 4, 5);
    printf("%d\n", j);
    return 0;
}
4
7
6
5
Answer: Option
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 expression (1, 2, 3, 4, 5) the result is 5, which on adding to i results into 7.
Discussion:
13 comments Page 2 of 2.

Ammu said:   8 years ago
Please anyone explain this clearly.

Rajkumar R said:   7 years ago
It's like this,

x=1, 2, 3;
x output will be 1.
y=(1, 2, 3);
y output will be 3.

Shubham T said:   4 years ago
CASE 1: x=1,2,3;

Then o/p x will be 1,
But in CASE 2: when there are brackets like this x=(1,2,3)
I think it acts as a function and function has right-left.
So, it will give o/p as 3 in case 2.


Post your comments here:

Your comments will be displayed after verification.