Search code examples
cprintflanguage-lawyerlibcformat-string

Interpreting the format specifier in printf("%.-1f", 34.14)


Consider the following invocation of printf():

printf("%.-1f", 34.14);

it specifies a negative precision value of -1. Making this call with glibc, we get:

%0.-1f

is this correct? If so, why?


Solution

  • The wording changed between C11 and C17/18. In C17/18 it says:

    §7.21.6.1 The fprintf function
    4. [...] The precision takes the form of a period (.) followed either by an asterisk * (described later) or by an optional nonnegative decimal integer; [...]

    The way I read it, the behavior when using a negative integer is not defined.

    This is not a breaking change since it does not make previously valid programs invalid. The intent was never to allow negative values as pointed out in DR220 - Definition of "decimal integer" / N32079:

    The term "decimal integer" is defined neither in the Standard nor in ISO 2382-1. Therefore it is not possible to tell whether, in each case:

    • the value may be zero
    • a non-significant leading zero digit may be used
    • the value may be negative.
    Suggested Technical Corrigendum

    Add a new paragraph to 7.1.1:
    [#x] A decimal integer is a sequence of digits which may begin with one or more zeros, but is nonetheless interpreted as decimal, not octal.

    Technical Corrigendum

    In 7.19.6.1P4, which reads in part: "[...] or by an optional decimal integer [...]"
    change "decimal integer" to "non-negative decimal integer".