string length limitation of printf

S

Seong-Kook Shin

C FAQ Q 13.2 says that sprintf() is guaranteed to work only for n <= 509:

sprintf(dest, "%.*s", n, source);

Does 509 appear in any C standard? If not, where it came from?
I looked over C99 (ISO/IEC 9899:1999) but didn't find yet.

If that limitation is old standard (perhaps ANSI?), does it limit the total
length of the string that sprintf() would print, or just for one '%s'?

I heard that on some old system does have a limitation on the total output
length of printf() family. If this limitation is not covered by (any) C
standard,
where can I get the information? (perhaps POSIX?)

Thanx.
 
M

Mike Wahler

Seong-Kook Shin said:
C FAQ Q 13.2 says that sprintf() is guaranteed to work only for n <= 509:

That's funny, I see that Q 13.2 is about 'strncpy()' function.
sprintf(dest, "%.*s", n, source);

Does 509 appear in any C standard? If not, where it came from?
I looked over C99 (ISO/IEC 9899:1999) but didn't find yet.
If that limitation is old standard (perhaps ANSI?), does it limit the total
length of the string that sprintf() would print, or just for one '%s'?

I heard that on some old system does have a limitation on the total output
length of printf() family. If this limitation is not covered by (any) C
standard,
where can I get the information? (perhaps POSIX?)

From the 1999 standard:
--------------------------------------------------------------
7.19.6.1 The fprintf function

Returns

14 The fprintf function returns the number of characters
transmitted, or a negative value if an output or encoding
error occurred.

Environmental limits

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



From the 1989 standard:
--------------------------------------------------------------
4.9.6.1 The fprintf function

Returns

The fprintf function returns the number of characters transmitted,
or a negative value if an output error occurred.

"Environmental limit"

The minimum value for the maximum number of characters produced by
any single conversion shall be 509.
 
M

Mike Wahler

From the 1999 standard:

Anticipating a possible objection that you asked about 'sprintf()'
rather than 'fprintf()':

7.19.6.6 The sprintf function

Description

2 The sprintf function is equivalent to fprintf, except that the
output is written into an array (specified by the argument s)
rather than to a stream. A null character is written at the end
of the characters written; it is not counted as part of the
returned value. If copying takes place between objects that
overlap, the behavior is undefined.

Returns

3 The sprintf function returns the number of characters written in
the array, not counting the terminating null character, or a neg-
ative value if an encoding error occurred.

-Mike
 
P

pete

Mike said:
That's funny, I see that Q 13.2 is about 'strncpy()'
function.



From the 1999 standard:
--------------------------------------------------------------
7.19.6.1 The fprintf function

Returns

14 The fprintf function returns the number of characters
transmitted, or a negative value if an output or encoding
error occurred.

Environmental limits

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

From the 1989 standard:
--------------------------------------------------------------
4.9.6.1 The fprintf function

Returns

The fprintf function returns the number of characters transmitted,
or a negative value if an output error occurred.

"Environmental limit"

The minimum value for the maximum number of characters produced by
any single conversion shall be 509.
--------------------------------------------------------------

There's also an environmental limit on line length.

N869
7.19.2 Streams
Environmental limits
[#7] An implementation shall support text files with lines
containing at least 254 characters, including the
terminating new-line character.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top