Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a limit on the number of values that can be printed by a single call of printf?

Tags:

c

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?

like image 464
user123 Avatar asked Feb 27 '16 14:02

user123


People also ask

What is the purpose of the printf() function?

The printf() function sends a formatted string to the standard output (the display). This string can display formatted variables and special control characters, such as new lines ('\n'), backspaces ('\b') and tabspaces ('\t'); these are listed in Table 2.1.

How does printf work in assembly?

Printf in Assembly To call printf from assembly language, you just pass the format string in rdi as usual for the first argument, pass any format specifier arguments in the next argument register rsi, then rdx, etc.


1 Answers

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.

like image 119
chqrlie Avatar answered Oct 12 '22 11:10

chqrlie