views:

83

answers:

3

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.e. %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?

+2  A: 

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.

laalto
that's why asked "practical" in question, after all this width is limited and large values arent valid. even if 64-bit, it's practically limited
zaharpopov
While `printf` probably does not (and is not required to) enforce any limit, the fact that `printf` returns the number of characters printed means you'll get unspecified/undefined (not clear which) behavior if you use insanely large field widths/precisions. A more realistic reason the format specifier could be unlimited length is redundant flags, e.g. `%+++++++++++++++++++++++d`.
R..
@zaharpopov: Well, there are two approaches in the question 1) practical 2) according to standard. I took the latter approach. The former depends on compiler and library implementation.
laalto
A: 
pmg
yes, but this is not what my question is about. format may be not a literal - my question is about length of maximal specifier after `%`
zaharpopov
+1  A: 

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 atoi).

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.

Dummy00001