15

Does the number of values printed by printf depend on the memory allocated for a specific program or it can keep on printing the values?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user123
  • 271
  • 1
  • 10
  • 5
    The C standard guarantees that you can use 127 arguments to `printf` and a literal format string of up to 4095 characters , anything beyond that will depend on your compiler – M.M Feb 27 '16 at 14:40
  • 5
    @M.M That's a reasonable answer. Comments are not for answering. – edmz Feb 27 '16 at 14:42
  • @black I find the question unclear – M.M Feb 27 '16 at 14:43
  • 1
    Well, of course it *also* depends on the memory allocated. You can't print stuff that doesn't fit in the buffer. (Or, you *can*, but undefined behavior.) – Cody Gray - on strike Feb 27 '16 at 14:51
  • @CodyGray Well, it depends on what you're doing. If you are using fprintf - print to a file descriptor, or printf, as specified by the question, you CAN print lengths that don't fit in the RAM/buffer, because you print to the printer (or terminal emulator), not the RAM directly. Or at least as far as I understand it. – jg6 Mar 31 '21 at 10:59

1 Answers1

17

The C Standard documents the minimum number of arguments that a compiler should accept for a function call:

C11 5.2.4.1 Translation limits

The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits:

  • ...

  • 127 arguments in one function call

  • ...

Therefore, you should be able to pass at least 126 values to printf after the initial format string, assuming the format string is properly constructed and consistent with the actual arguments that follow.

If the format string is a string literal, the standard guarantees that the compiler can handle string literals at least 4095 bytes long, and source lines at least 4095 characters long. You can use string concatenation to split the literal on multiple source lines. If you use a char array for the format string, no such limitation exists.

The only environmental limit documented for the printf family of functions is this:

The number of characters that can be produced by any single conversion shall be at least 4095

This makes the behavior of format %10000d at best defined by the implementation, but the standard does not mandate anything.

A compliant compiler/library combination should therefore accept at least 126 values for printf, whether your environment allows even more arguments may be defined by the implementation and documented as such, but is not guaranteed by the standard.

Community
  • 1
  • 1
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Is this the limit even for any user-defined function as well ? – user123 Feb 27 '16 at 14:51
  • Where does it say that it is implementation-defined? – M.M Feb 27 '16 at 14:57
  • 3
    @user123: the limit is the lower bound for the behavior defined by the standard, for user-defined functions as well. If you write functions that have more arguments, the compiler may or may not handle your program correctly and still comply with the standard. – chqrlie Feb 27 '16 at 15:02
  • 1
    @M.M: agreed. It is not implementation defined, it is just not defined by the standard. The implementation may define the behavior or not. – chqrlie Feb 27 '16 at 15:03
  • 2
    "implementation defined" means that the standard requires the implementation to document the behaviour (see C11 3.4.1) – M.M Feb 27 '16 at 15:04
  • So it is implementation defined right after taking 127 arguments – user123 Feb 27 '16 at 15:38
  • 2
    @user123: no, you should split your `printf` call into multiple calls, each with at most 126 values after the format string. A single call with more values might work as expected with your environment, but you should not rely on it. – chqrlie Feb 27 '16 at 15:45
  • 1
    *"at least one program that contains at least one instance"* -- that seems to imply that not *every* function call with 127 arguments need to be supported. For example, could a compliant compiler be able to handle functions with 127 explicit parameters, but for some reason only 63 maximum vararg-style parameters? There would then exist at least one program with at least one instance of a 127-argument function call that the compiler could handle, but a printf call would not be it. – R.M. Feb 27 '16 at 18:47
  • 3
    @R.M.: that's a good question. I have always wondered why the standard uses this formulation. IMHO variadic functions are specific enough that the standard should be more precise, especially for the `printf` like functions. You should post a new question. – chqrlie Feb 27 '16 at 18:54
  • @ chqrlie , sir so it signifies that till 126 arguments we will never have any memory overflow right ? – user123 Feb 28 '16 at 12:04
  • 1
    @user123: more or less: it means that `printf` is supposed to handle at least 126 arguments after the format string. It also means that if your C library `printf` function fails for more than 126 arguments, that would not be a cause for non compliance. In practice, it is quite probable that `printf` will handle any number of arguments as long as they are consistent with the format string. Most `printf` implementations do not allocate memory to handle the arguments, the stack space used to hold the arguments should be able to handle an insanely large number of arguments. – chqrlie Feb 28 '16 at 12:24