The instruction

`printf("%4.2f", num);`

displays four digits including two decimal places?

No, that is not how it works.

A format string is specified in the following format:

`%[flags][width][.precision][size]type`

Specifiers in `[]`

are *optional*.

The *width* specifier indicates the *minimum number of characters* to output **total** (it may be more!). If not specified, the *width* defaults to 1.

The *precision* specifier indicates how many digits the `f`

*type* prints after a decimal point (the *precision* has other meanings for different *type*s). If not specified, the *precision* defaults to 6 for the `f`

*type*.

In your example format string: `%4.2f`

, `4`

is the *width*, `2`

is the *precision*, and `f`

is the *type*.

So, for example, given `double num = 123.0;`

:

```
printf("%f", num);
```

Has a *width* of 1 and a *precision* of 6, so it prints `"123.000000"`

– *at least* 1 character, including the decimal and 6 digits following the decimal.

```
printf("%4.2f", num);
```

Has a *width* of 4 and a *precision* of 2, so it prints `"123.00"`

– *at least* 4 characters, including the decimal and 2 digits following the decimal.

```
printf("%10.2f", num);
```

Has a *width* of 10 and a *precision* of 2, so it prints `" 123.00"`

– *at least* 10 characters, including 4 leading spaces added as padding, the decimal, and 2 digits following the decimal.

Live Demo

Refer to this `printf`

reference for more details about the format string and its inputs and outputs.