A quick question

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

(It's the last of these that I'm specifically wondering about.)
 
D

Darrell Grainger

Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

(It's the last of these that I'm specifically wondering about.)

The fact that the standard C89, 7.9.6.3 The printf function, states:

The printf function is equivalent to fprintf with the argument
stdout interposed before the arguments to printf."

Makes it fairly certain that the first two are guaranteed to produce the
same output.

The only limitation I can find is that fprintf and printf may have an
environmental limit but fwrite does not; 7.9.6.1 The fprintf function,
"The minimum value for the maximum number of characters produced by any
single conversion shall be 509."

P.S. sizeof char == 1 therefore you could write it as:

fwrite( s, 1, sizeof(s) - 1, stdout );
 
E

Eric Sosman

Christopher said:
Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

(It's the last of these that I'm specifically wondering about.)

Yes: Assuming `stdout' does not have "wide orientation"
(7.19.2/4-5) and barring I/O errors, all of these produce the
same result as `fputs(s, stdout)'. "The byte output functions
write characters to the stream as if by successive calls to the
fputc function." (7.19.3/12)

Of course, if you mis-counted and wrote `s[14]' or `s[16]',
this wouldn't be true.
 
T

Thomas Stegen CES2000

Christopher said:
Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );

This is basically the definition of printf
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

Yup, from my reading of the standard. All call above are allowed to
fail in different ways, but as long as then completely succeed the
output will be the same. Note that fwrite have to write the output
as if it was written with fputc. That is, for each element to be
written (the third argument) fputc shall write out as many bytes
as specified by the second argument. Each of these bytes shall be
successive. (Of course, an implementation need only behave as-if
fputc is being used).
 
M

Mark A. Odell

Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

I thought sizeof (char) was *guaranteed* to be one?
 
R

Régis Troadec

Hello,

Christopher Benson-Manica said:
Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

(It's the last of these that I'm specifically wondering about.)

I would answer yes, since you don't include the terminating null character
by using fwrite, it acts like using printf() and fprintf() with the %s flag.

best regards, regis
 
C

Christopher Benson-Manica

Eric Sosman said:
char s[15]="Hello, world\n!";
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );
Of course, if you mis-counted and wrote `s[14]' or `s[16]',
this wouldn't be true.

Which is why I'm of the opinion that fixed-width strings, and this
method of dealing with them, are not to be preferred.
 
A

Artie Gold

Mark said:
Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );


I thought sizeof (char) was *guaranteed* to be one?
'Tis.

--ag
 
J

Joe Wright

Christopher said:
Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

(It's the last of these that I'm specifically wondering about.)
Sorry I'm late. It is the last line that is of interest. The first
problem is that 'sizeof s' is of no interest as it might well be 256 or
somesuch. I would write the third line..

#include <string.h>
...
fwrite(s, 1, strlen(s), stdout);

I don't know where the -1 stuff comes from. Also the initializer of s
looks strange as...

"Hello, world\n!"

It is usally...

"Hello, world!\n"
^^^
 
C

Christopher Benson-Manica

'sizeof s' is of no interest as it might well be 256 or
somesuch.

Not so, if other replies are to be believed (as well as a great deal
of code produced by better hands than mine). sizeof s yields 15 here,
although I would be grateful if someone would post the portion of the
Standard, if any, which guarantees that.
I don't know where the -1 stuff comes from.

I specifically wished to avoid printing the NUL terminator.
Also the initializer of s looks strange as...
"Hello, world\n!"

Indeed, typos often grate upon the eye. Sorry.
 
D

Dan Pop

In said:
Christopher said:
Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

(It's the last of these that I'm specifically wondering about.)
Sorry I'm late. It is the last line that is of interest. The first
problem is that 'sizeof s' is of no interest as it might well be 256 or
somesuch.

Please elaborate.

Dan
 
D

Dan Pop

In said:
Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

In the abstract machine, all three of them result in fputc being called
15 times, to output the same sequence of characters, to stdout.

Dan
 
J

Joe Wright

Dan said:
In said:
Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

In the abstract machine, all three of them result in fputc being called
15 times, to output the same sequence of characters, to stdout.
That would be 14 times.
 
J

Joe Wright

Christopher said:
Not so, if other replies are to be believed (as well as a great deal
of code produced by better hands than mine). sizeof s yields 15 here,
although I would be grateful if someone would post the portion of the
Standard, if any, which guarantees that.


I specifically wished to avoid printing the NUL terminator.
While sizeof s is 15, strlen(s) is 14 (NUL is not included). Your printf
and fprintf examples print 14 characters.
 
J

Joe Wright

Dan said:
In said:
Christopher said:
Given the following:

char s[15]="Hello, world\n!";

Are all the following guaranteed to produce the same output?

printf( "%s", s );
fprintf( stdout, "%s", s );
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

(It's the last of these that I'm specifically wondering about.)
Sorry I'm late. It is the last line that is of interest. The first
problem is that 'sizeof s' is of no interest as it might well be 256 or
somesuch.

Please elaborate.
I mean that the length of the string (14) is understood by printf and
fprintf and is returned by strlen(s) in my version of fwrite. The
convolution of 'sizeof s - 1' to achieve 14 is wierd.
 
C

Christopher Benson-Manica

Joe Wright said:
While sizeof s is 15, strlen(s) is 14 (NUL is not included). Your printf
and fprintf examples print 14 characters.

And so does the fwrite(), which was the point of the question...
 
J

Joe Wright

Christopher said:
And so does the fwrite(), which was the point of the question...
My point was that strlen(s) is the answer to the question, 'not sizeof
s'. Consider 'char s[100] = "Hello, world!\n";'. sizeof s is 100,
strlen(s) is 14. The example of 'char s[15] = "Hello, world!\n";' is
contrived to define s to hold the 14 character string exactly.
 
C

Christopher Benson-Manica

Joe Wright said:
My point was that strlen(s) is the answer to the question, 'not sizeof
s'. Consider 'char s[100] = "Hello, world!\n";'. sizeof s is 100,
strlen(s) is 14. The example of 'char s[15] = "Hello, world!\n";' is
contrived to define s to hold the 14 character string exactly.

Although strlen(s) is the *wrong* answer if you consider 'char
s[14]="Hello, world!\n"', which was being advocated in the recent "[C]
strings question" (or something to that effect) thread. The point of
the question was to determine whether such practice was illegal or
merely morally reprehenisble ;)
 
J

Joe Wright

Christopher said:
Joe Wright said:
My point was that strlen(s) is the answer to the question, 'not sizeof
s'. Consider 'char s[100] = "Hello, world!\n";'. sizeof s is 100,
strlen(s) is 14. The example of 'char s[15] = "Hello, world!\n";' is
contrived to define s to hold the 14 character string exactly.

Although strlen(s) is the *wrong* answer if you consider 'char
s[14]="Hello, world!\n"', which was being advocated in the recent "[C]
strings question" (or something to that effect) thread. The point of
the question was to determine whether such practice was illegal or
merely morally reprehenisble ;)
It is *wrong* because 'char s[14]="Hello, world!\n"' is not a C string.
It seems contrived to make some specious point. I'd say reprehensible.
:)
 
C

Christopher Benson-Manica

Joe Wright said:
It is *wrong* because 'char s[14]="Hello, world!\n"' is not a C string.

I said as much... "but my words like silent raindrops fell" :)
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top