What is the output when the following segment of ‘C’ code is executed? void…

2017

What is the output when the following segment of ‘C’ code is executed?

void main()

{

float a = 123.456;

printf("%.2f, %7.3f, %12e", a, a, a);

}

  1. A.

    123.450, 123.4560, 1.234560e+02

  2. B.

    123.46, 123.456, 1.234560e+02

  3. C.

    123.456000, 123.456, 0.1234560e+03

  4. D.

    123.45, 123.4560, 1.234560e+02

Attempted by 776 students.

Show answer & explanation

Correct answer: B

  • %.2f rounds 123.456 to 2 decimal places, giving 123.46.

  • %7.3f prints with a minimum total width of 7 characters and 3 decimal places, matching the length of 123.456 perfectly.

  • %12e prints in scientific notation with 6 default decimal places, padded with spaces to fit 12 characters: 1.234560e+02.

Thus, the correct option is B.

Explore the full course: Coal India Management Trainee