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 1 of 2.

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.

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.

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

Emigo said:   9 years ago
@Vvv.

Try and change one statement to,
x=(10, 20, 30);

And see result yourself.

Chetan Agarwal said:   10 years ago
int a=1, b=2, c=3, i=0; commas act as separators in this line, not as an operator.
a=1, b=2, c=3, i=0.

i=(a, b); stores b into i.
a=1, b=2, c=3, i=2.

i=a, b; stores a into i equivalent to (i = a), b;
a=1, b=2, c=3, i=1.

i=(a += 2, a + b); increases a by 2, then stores a+b = 3+2 into i.
a=3, b=2, c=3, i=5.

i = a += 2, a + b; increases a by 2, then stores a to i, and discards unused.
a+b r value equivalent to (i=(a += 2)), a + b;
a=5, b=2, c=3, i=5.

i = a, b, c; stores a into i, discarding the unused b and c r values.
a=5, b=2, c=3, i=5.

i = (a, b, c); stores c into i, discarding the unused a and b r values.
a=5, b=2, c=3, i=3.

return a=4, b=5, c=6; returns 6, not 4, since comma operator sequence points.

Following the keyword 'return' are considered a single.

Expression evaluating to r value of final sub expression c=6.

return 1, 2, 3; returns 3, not 1, for same reason as previous example.

return (1), 2, 3; returns 3, not 1, still for same reason as above.

This example works as it does because return is a keyword, not a function call. Even though most compilers will allow for the construct return (value), the parentheses are syntactic sugar that get stripped out without syntactic analysis.

Ravali said:   10 years ago
i+5 means 2+5=7.

@vvv said:   1 decade ago
#include<stdio.h>
void main()
{
int x;
x=10,20,30;
printf("%d",x);
return 0;
}

Why x = 10?

Parvathy said:   1 decade ago
@Prasant.

The let us C book has given the associativity wrong. The associativity of ", " is left to right. Hence 5 is taken and 5+2 = 7.

Bhanu said:   1 decade ago
// cpp_comma_operator.cpp.

#include <stdio.h>
int main () {
int i = 10, b = 20, c= 30;
i = b, c;
printf("%i\n", i);

i = (b, c);
printf("%i\n", i);
}

Output:
20
30

Shashi said:   1 decade ago
The associativity of comma is left to right. So it will take the first value from set i.e. 2 and put it to j i.e. j = 3. Now it will move to next element i.e., 3 and calculates i+3 = 4. But j is an integer which can only take one value and it will discard the present value and updated to j= 4. This process goes on and at last we left with j = 2+5 = 7.


Post your comments here:

Your comments will be displayed after verification.