Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

size limit of printf conversion specification

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?

like image 527
zaharpopov Avatar asked Oct 20 '10 12:10

zaharpopov


2 Answers

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.

like image 142
laalto Avatar answered Sep 24 '22 15:09

laalto


If you mean a literal string, it's 4095 characters

5.2.4.1 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 read_int function.
So, for this implementation, apparently, maybe, the limit is INT_MAX (I haven't searched for the read_int function).

like image 33
pmg Avatar answered Sep 23 '22 15:09

pmg