What will be output of the following program? #include <stdio.h> int…
What will be output of the following program?
#include <stdio.h>
int main(void)
{
int x;
x = 10, 20, 30;
printf("%d", x);
return 0;
}
Answer: B. 10 — Understanding the comma operator and precedence in C The relevant statement is: x = 10, 20, 30; The assignment operator (=) has higher precedence than the…
- A.
20
- B.
10
- C.
30
- D.
error
Attempted by 44 students.
Show answer & explanation
Correct answer: B
Understanding the comma operator and precedence in C
The relevant statement is:
x = 10, 20, 30;
The assignment operator (=) has higher precedence than the comma operator. So the expression is grouped as:
(x = 10), 20, 30;
First, 10 is assigned to x.
Then 20 and 30 are evaluated as comma-separated expressions, but their results are ignored.
So the value stored in x remains 10.
If the statement were written with parentheses as:
x = (10, 20, 30);
then the comma expression would return the rightmost value, 30, and x would become 30. But that is not the given statement.
Therefore, printf("%d", x); prints 10.
Final Answer: 10.