Is there a complete online guide for C format specifiers for every type of data and for all cases? I only found partial and contrasting references that doesn't explain all possible cases.
-
1Anything wrong with [printf(3)](http://man7.org/linux/man-pages/man3/printf.3.html) and [scanf(3)](http://man7.org/linux/man-pages/man3/scanf.3.html)? – David C. Rankin Dec 18 '18 at 08:40
-
1David, there's a bit of a difference between what Linux or glibc allows and what C allows. I would hope the former was a superset of the latter but I'm not sure. In any case, were you to use the extended stuff, it wouldn't necessarily be portable to other C implementations. – paxdiablo Dec 18 '18 at 11:23
-
bytes_written += printf (argument listing here) ; sometimes it's helpful to check the number of bytes, just to make sure the formatting is not only syntactically correct, but also produces the right number of bytes. – Krassi Em Dec 18 '18 at 21:05
-
Look also into the [C reference](https://en.cppreference.com/w/c/io). But of course, *standard* `printf` can't handle *every* type. – Basile Starynkevitch Dec 19 '18 at 07:06
1 Answers
The definitive guide for this is the actual ISO standard itself. Any other source suffers from the potential flaw that it may be incorrect or incomplete. The standard is, by definition, both correct and complete(a).
And, while standards documents can sometimes be dry and difficult to read, the sections covering the format specifiers is reasonably clear, both in terms of what all the specifiers mean (including flags, width/precision specifiers, and length modifiers), and the data types you're allowed to use with those specifiers.
For example, C11(b) details all the format specifiers in 7.21.6.1
and 7.21.6.2
for the printf
and scanf
family of functions respectively. The last free draft of this iteration of the standard is the N1570 document.
That is, practically speaking, the C11 standard - officially, it is the latest draft of C11 and, to get the real standard, you need to buy it from the standards body of your country. However, the differences are minor and tend to be administrative in nature.
(a) I don't mean to imply the standard is totally coherent or bug-free, just that it is the standard. That means, pending authorised changes, implementations must follow said standard in order to be considered C. If an implementation does that, it's valid, regardless of what lunacy the standard may have in it :-)
(b) Although C11 (the iteration we use and are therefore most familiar with) may have been officially replaced by C18, the changes were only incorporations of TCs and defect fixes. There were no substantial changes to the "meat" of the standard, in particular for this question, the format specifiers.

- 854,327
- 234
- 1,573
- 1,953
-
1C 2011 has been withdrawn and replaced by C 2018. [This question](https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents) tracks versions of the C and C++ standards, and a late draft of the C 2018 standard is [here](http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf). – Eric Postpischil Dec 18 '18 at 13:03
-
@Eric, I believe C18 is little more than C11 with the TCs and DRs incorporated, simply because there's a limit to how many of those they're allowed to do to a standard before re-issuing. Since there's no real functional change in C18, it's unlikely the format specifiers will have changed. – paxdiablo Dec 18 '18 at 13:51
-
C 2011 has been canceled. It is not, as you write, “The *definitive* guide” and is not “the actual ISO standard itself.” – Eric Postpischil Dec 18 '18 at 14:14
-
1@Eric, I never stated that c11 was the current standard and therefore definitive, just that the *ISO standard* is definitive (and it is). I then went on to use c11 as an *example*. By all means, use c18 if you prefer but, as explained in previous comment, the format specifiers are the same. I'll try to make that clearer. – paxdiablo Dec 18 '18 at 20:46
-
Consider it this way: Why does your answer mention C 2011 at all? Is there a reason it is better to refer people to C 2011 rather than 2018? Or did you just write about it out of habit? If there is no reason to prefer C 2011, why not edit your answer to present just the current standard, which is the official version? – Eric Postpischil Dec 18 '18 at 20:54
-
@Eric, I appreciate your input but I think I'll let it stand as is. I've explained the rationale in the footnote. If you think an answer referencing c18 would be better, you should probably add one and let SO decide. – paxdiablo Dec 18 '18 at 21:00
-
The specification of printf in the standard has a lot of inconsistencies and ambiguities (e.g. it says that `printf("%u", 1);` is undefined, but `printf("%lx", -1);` is not undefined) , I would hesitate to call it "correct and complete". But it's certainly better than any of the other available options. – M.M Dec 18 '18 at 23:21
-
1@M.M: I meant correct and complete inasmuch as it *is* the standard, hence conforming implementations must do what it says. I make no comment as to the sanity *behind* the document, preferring the route taken in the C99 rationale (paraphrased): "This rationale is not a rationale for the C language as a whole: no attempt is made to defend the language" :-) – paxdiablo Dec 19 '18 at 04:08