GATE / CS ARCHIVE

GATE CSE 2017 SET 2 / QUESTION 54

GATE CSE 2017 Set 2 Q54

Programming in C · Expressions, Operators & Types · Full GATE CSE 2017 Set 2 paper

GATE 2017 Set 2, Q54 2 marks · NAT
Report issue

Expressions, Operators & Types

Question 54, GATE 2017: C program fragment: int m=10; int n,n1; n=++m; n1=m++; n--; --n1; n-=n1; printf("%d",n);. What is printed?
Tap to enlarge
Answer & solution

Official answer: 0.0

Our answer: 0

Step through it (12 steps)

1. Prefix increment changes the operand before producing its value. Thus n=++m first makes m=11 and then assigns n=11.

2. Postfix increment produces the old value and increments the operand by the end of the full expression. Hence n1=m++ assigns n1=11 and leaves m=12.

3. The separate statements n-- and --n1 reduce n and n1 to 10. Their prefix/postfix distinction does not matter when their expression values are discarded. Finally n-=n1 computes n=10-10=0, which is printed. These modifications occur in separate full expressions, so there is no unsequenced-increment ambiguity.

Original paper, page 22 · Answer key