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);
}

Answer: B. 123.46, 123.456, 1.234560e+02Concept — how a printf conversion is built A conversion specification here has the form %[flags][width][.precision]conversion; optional flags may also appear…

  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 1145 students.

Show answer & explanation

Correct answer: B

Concept — how a printf conversion is built

A conversion specification here has the form %[flags][width][.precision]conversion; optional flags may also appear right after the % sign, and none of the three conversions in this call uses one. The width is a minimum total field width: with no flag, if the converted text is shorter, spaces are added on the left, and if it is already that long or longer nothing is added — printf never truncates a number to make it fit. The .precision is the count of digits printed after the decimal point for both f and e; it defaults to 6, and the value is rounded to that many digits, never cut. An e conversion also normalises the number as d.dddddde±dd, that is, one non-zero digit before the point.

Application — the three conversions in this call, with a = 123.456

  1. %.2f: no width is given and the precision is 2, so 123.456 is rounded to two decimal digits — the third digit 6 rounds the 5 up — giving 123.46.

  2. %7.3f: the precision 3 gives 123.456, and that text is already 7 characters long (three digits, the point, three more digits), so the minimum width of 7 is exactly met and no spaces are added.

  3. %12e: no precision is written, so the default 6 applies — mantissa 1.234560 and exponent 2, that is 1.234560 × 102, written 1.234560e+02. Count its characters: one digit, the point, six digits, the letter e, the sign and two exponent digits — 12 in all. The minimum width of 12 is exactly met, so here too no spaces are added.

Cross-check — what each width actually did

Conversion

Width asked for

Text produced

Its length

Spaces added

%.2f

none

123.46

6

0

%7.3f

7

123.456

7

0

%12e

12

1.234560e+02

12

0

So the widths 7 and 12 only reserve room; here the text already fills them, which is why no padding appears. Had the call used %15e, the same 12-character text would have been printed with three leading spaces, and %9.3f would print two leading spaces before 123.456. Without a flag, a width can only add spaces on the left — it never adds digits and never cuts; a - flag would left-justify the text instead, and 0 would pad it with zeros.

One more detail: a is declared float, and a float argument is promoted to double before printf reads it. 123.456 is not exactly representable in binary, so the stored value is about 123.456001, but at 2, 3 and 6 decimal digits it still rounds to the digits shown.

Result: the three fields print as 123.46, 123.456, 1.234560e+02.

Explore the full course: Coal India Management Trainee

Loading lesson…