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;
}
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.
Prasant said:
1 decade ago
In Let Us C book it describe that the comma operator has Right_Left associative. Please discuss this
Neeti said:
1 decade ago
I don't understand this. Can anybody explain!
Raja said:
1 decade ago
Which means this operator select the values from right to left ie(here it takes the right most value 5 and ommit the others).
so i+5=7.
so i+5=7.
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.
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
#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
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.
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.
@vvv said:
1 decade ago
#include<stdio.h>
void main()
{
int x;
x=10,20,30;
printf("%d",x);
return 0;
}
Why x = 10?
void main()
{
int x;
x=10,20,30;
printf("%d",x);
return 0;
}
Why x = 10?
Ravali said:
10 years ago
i+5 means 2+5=7.
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.
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.
Emigo said:
9 years ago
@Vvv.
Try and change one statement to,
x=(10, 20, 30);
And see result yourself.
Try and change one statement to,
x=(10, 20, 30);
And see result yourself.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers