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 2 of 2.
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.
Neeti said:
1 decade ago
I don't understand this. Can anybody explain!
Prasant said:
1 decade ago
In Let Us C book it describe that the comma operator has Right_Left associative. Please discuss this
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers