size limit of printf conversion specification
Asked Answered
C

3

6

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?

Caledonian answered 20/10, 2010 at 12:36 Comment(4)
I'm curious -- is there any reason you are want to know besides curiosity?Thereupon
@nategoose: i'm considering a very special wrapper around snprintf and want to understand whether i can allocate some temporary buffers staticallyCaledonian
What about taking a look at GNU's libc implementation? If you allow as much as it does, you should be good enough.Crag
I think that a simple enough to read implementation I looked at once just interpreted the conversion specification as it went -- using a state machine with a couple of integer variables until it got to the type. The data type handling code then used those integers and the argument to output (handling the default state, of course), so there was no limit outside of integer strings that were too big to fit in integers. I think that this was the Nut/OS implementationThereupon
E
2

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.

Equipoise answered 20/10, 2010 at 12:54 Comment(3)
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 limitedCaledonian
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.Miscue
@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.Equipoise
B
1

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

Benitobenjamen answered 20/10, 2010 at 13:20 Comment(1)
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 %Caledonian
R
1

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.

Reck answered 20/10, 2010 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.