printf conversion specifications are
% followed by flags, width, precision, length modifier and conversion specifier. Is there practical limit to size of a conversion specification?
%s is 2 chars long, while
%08.2f is 6 chars long. My question is, what is the length of the maximal single specification in a format string that can be created, according to C99 standard?
There is no such conversion specification of maximum length. If you think you’ve found such a spec, I can come up with one that is one char longer.
For example, consider field width and precision. The standard says they are decimal integers but does not specify their range. Therefore you can write conversion specifiers with arbitrarily large integers as field width or precision.
If you mean a literal string, it’s 4095 characters
126.96.36.199 Translation limits
— 4095 characters in a character string literal or wide string literal (after concatenation)
I’ve been bitten by C89 limit of 509 characters (not for printf/scanf format strings), so this is one of the good changes brought on by C99 🙂
Edit: glibc implementation (not Standard definition)
glibc implementation gets the width from a
So, for this implementation, apparently, maybe, the limit is INT_MAX (I haven’t searched for the
printf conversion specifications are % followed by flags, width, precision, length modifier and conversion specifier. Is there practical limit to size of a conversion specification?
I had to deal in past with several standard
printf implementations and my general impression that there is no particular limit imposed.
The format string generally is parsed character by character. (Think simple FSM.) Most
printf implementations avoid buffering anything internally and even for numbers use the char by char conversion to decimal (not even
You can check for example how the
printf is implemented inside the FreeBSD kernel (where from many other implementations often lift the code). That is surely simplified implementation (with couple kernel-specific tweaks), yet it reflects how the format string is often handled.
N.B. Just checked glibc’s
vfprintf() implementation and they allocate internally a buffer (if needed) with
malloc(). So neither particular limit there.
My question is, what is the length of the maximal single specification in a format string that can be created, according to C99 standard?
The format specifier is a part of a string and string length to my knowledge isn’t limited by the standard. And as I mention above, neither I have ever seen an implementation with any such limit.