Difficulty: Easy
Correct Answer: 1, 2, 3, 4, 5,
Explanation:
Introduction / Context:
Understanding pre-increment (++i) inside assignments and how it interacts with loop control is a foundational C topic. This program fills an array while incrementing the index.
Given Data / Assumptions:
Concept / Approach:
With pre-increment, ++i increments i first, then the incremented value is used. Because the index in arr[i] is evaluated before ++i changes i, the assignment stores values 1..5 into arr[0..4] respectively as the loop advances.
Step-by-Step Solution:
Start i=0: condition true → arr[0] = ++i → i=1; arr[0]=1.Next: i=1: arr[1] = ++i → i=2; arr[1]=2.Repeat until i=4: arr[4] = ++i → i=5; arr[4]=5.Loop ends when i becomes 5; printing yields 1, 2, 3, 4, 5,.
Verification / Alternative check:
Change to arr[i++] = i; and observe a different sequence; or use explicit increments to trace values.
Why Other Options Are Wrong:
They assume post-increment behavior or initial garbage values. There is no syntax error.
Common Pitfalls:
Confusing arr[i] = ++i with arr[++i] = i; mixing pre vs post increment side effects.
Final Answer:
1, 2, 3, 4, 5,
Discussion & Comments