Sadek Sadek - 4 months ago 20
C Question

Wy printf is not able to handle flags, field width and precisions properly?

I'm trying to discover all capabilities of printf and I have tried this :

printf("Test:%+*0d", 10, 20);

that prints


I have use first the flag
, then the width
and the re-use the flag

Why it's make this output ? I purposely used
in a bad way but I wonder why it shows me the number 100?


This is because, you're supplying syntactical nonsense to the compiler, so it is free to do whatever it wants. Related reading, undefined behavior.

Compile your code with warnings enabled and it will tell you something like

warning: unknown conversion type character ‘0’ in format [-Wformat=]
printf("Test:%+*0d", 10, 20);

To be correct, the statement should be

  printf("Test:%+*.0d", 10, 20);  // note the '.'

Related, quoting the C11, chapter §, (emphasis mine)

An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversions, the number of digits to appear after the decimal-point character for a, A, e, E, f, and F conversions, the maximum number of significant digits for the g and G conversions, or the maximum number of bytes to be written for s conversions. The precision takes the form of a period (.) followed either by an asterisk * (described later) or by an optional decimal integer; if only the period is specified, the precision is taken as zero. If a precision appears with any other conversion specifier, the behavior is undefined.