Q32.Marks: +2.0UGC NET Paper 2: Computer Science and Application 26th June 2025 Shift 1
What will be the output of the following C Programming code:
void main ()
{
int *i, a = 12 b = 2 c;
c=( a = a + b , b = a / b, a =a * b, b = a - b )
i =&c;
printf("%d", --(*i));
}
1.91
2.90✓ Correct
3.98
4.92
Solution
The correct answer is 90 (Option 2)
Key Points
The expression on the right side of c = ( ... ) uses the comma operator. It evaluates sub-expressions from left to right and returns the value of the last sub-expression.
Assignments inside that comma expression change a and b step by step; the final value (of the last assignment) is stored in c.
--(*i) is a pre-decrement on the value pointed by i. Since i = &c, it decreases c by 1 and then prints it.
Given code (formatted):
void main() {
int *i, a = 12, b = 2, c;
c = ( a = a + b, /* step 1 */
b = a / b, /* step 2 */
a = a * b, /* step 3 */
b = a - b ); /* step 4 (value of comma expr) */
i = &c;
printf("%d", --(*i));
}
Step-by-step evaluation of the comma expression:
a = a + b → a = 12 + 2 = 14
b = a / b → b = 14 / 2 = 7 (integer division)
a = a * b → a = 14 * 7 = 98
b = a - b → b = 98 - 7 = 91
The comma expression’s value is the value of this last assignment, i.e., 91. Therefore, c = 91.
Printing:i = &c so *i refers to c. --(*i) pre-decrements c to 90 and then prints it.
Important Points
Comma operator: Evaluates operands left→right; only the last value is returned, but all side effects occur.
Pre vs Post decrement:--x decrements then yields the new value; x-- would print the old value and decrement later.
Why other options are wrong:
91 – would be printed if there were printf("%d", *i--) or no decrement; but here we pre-decrement to 90.
98 – comes from the intermediate value of a (step 3), not what we store in c.
92 – results from an incorrect arithmetic/ordering assumption; correct final c is 91, then decremented to 90.