Multiple choice

What will be the output of the following program? void main() { int i; float a[5]; for (i=0; i<5; i++) a[i] = (printf, ("%d",i/10.0)); for (i=0; i<5; i++) printf("%.1f ",a[i]); }

  1. Compile-Time Error

  2. 0.0 0.0 0.0 0.0 0.0

  3. 0.0 0.1 0.2 0.3 0.4

  4. 1.0 1.0 1.0 1.0 1.0

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The code contains a comma operator quirk in the assignment expression, but more importantly, integer division i/10.0 promotes the result to float. For i from 0 to 4, i/10.0 evaluates to 0.0, 0.1, 0.2, 0.3, and 0.4 respectively. Printing these floats with format %.1f produces the expected sequence.