By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,677 Members | 1,272 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,677 IT Pros & Developers. It's quick & easy.

A quick question

P: n/a
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.)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
Share this Question
Share on Google+
30 Replies


P: n/a
On Thu, 5 Feb 2004, Christopher Benson-Manica wrote:
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 );
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #2

P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in
news:bv**********@chessie.cirr.com:
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?

--
- Mark ->
--
Nov 14 '05 #3

P: n/a
Christopher Benson-Manica wrote:
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).

--
Thomas.

Nov 14 '05 #4

P: n/a
Hello,

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> a écrit dans le
message de news: bv**********@chessie.cirr.com...
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
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Nov 14 '05 #5

P: n/a
Christopher Benson-Manica wrote:

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.

--
Er*********@sun.com
Nov 14 '05 #6

P: n/a
Eric Sosman <Er*********@sun.com> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #7

P: n/a
Mark A. Odell wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in
news:bv**********@chessie.cirr.com:

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

--
Artie Gold -- Austin, Texas

"Yeah. It's an urban legend. But it's a *great* urban legend!"
Nov 14 '05 #8

P: n/a
Christopher Benson-Manica wrote:

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"
^^^
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #9

P: n/a
Joe Wright <jo********@earthlink.net> spoke thus:
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );
'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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #10

P: n/a
In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
Christopher Benson-Manica wrote:

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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11

P: n/a
In <bv**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12

P: n/a
Dan Pop wrote:

In <bv**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
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.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #13

P: n/a
Christopher Benson-Manica wrote:

Joe Wright <jo********@earthlink.net> spoke thus:
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

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

While sizeof s is 15, strlen(s) is 14 (NUL is not included). Your printf
and fprintf examples print 14 characters.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #14

P: n/a
Dan Pop wrote:

In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
Christopher Benson-Manica wrote:

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.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #15

P: n/a
Joe Wright <jo********@earthlink.net> spoke thus:
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...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #16

P: n/a
Christopher Benson-Manica wrote:

Joe Wright <jo********@earthlink.net> spoke thus:
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...

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.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #17

P: n/a
Joe Wright <jo********@earthlink.net> spoke thus:
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 ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #18

P: n/a
Christopher Benson-Manica wrote:

Joe Wright <jo********@earthlink.net> spoke thus:
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.
:-)
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #19

P: n/a
Joe Wright <jo********@earthlink.net> spoke thus:
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" :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #20

P: n/a
Christopher Benson-Manica wrote:
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.)


In my own experience, I try to use fwrite for text that
doesn't require formatting. The printf family has an overhead
of parsing the format specifier string and handling a variable
number of arguments. In all the compilers that I've used the
printf family also brings in a whole bunch of code that may
not be necessary. If you have a simple program:
/* 1 */
int main(void)
{
printf("hello\n");
return 0;
}

I have found that the printf instruction causes the
floating point library to be hauled in. Build an
executable for the above program and compare with:
/* 2 */
int main(void)
{
puts("hello"); /* Did you forget about this one? */
return 0;
}

/* 3 */
const char * const hello_txt[] = "hello\n";

int main(void)
{
fwrite(hello_txt,
1,
sizeof(hello_txt) / sizeof(char) - 1,
stdout);
return 0;
}

They all should have identical behavior, but execution
speeds and sizes will differ.

I have tested this with:
Borland Compilers (Turbo C to 6.0)
Microsoft Compilers
Gnu
Metaware High C/C++
GreenHills
Solaris
Arm Compiler
All of these show that the execution sizes and
speeds will differ.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #21

P: n/a
In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
Dan Pop wrote:

In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
>Christopher Benson-Manica wrote:
>>
>> 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.


Not if s is defined reasonably:

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

Now sizeof s - 1 is *guaranteed* to produce the right answer with 0
runtime overhead, because it is a constant expression (optimising away the
strlen call is seldom performed by compilers, because C doesn't have
the concept of pure functions).

You can even use sizeof s when declaring another array of char that
needs to have the same size as s, without resorting to VLAs.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #22

P: n/a
Dan Pop wrote:

In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
Dan Pop wrote:

In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:

>Christopher Benson-Manica wrote:
>>
>> 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.


Not if s is defined reasonably:

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

Now sizeof s - 1 is *guaranteed* to produce the right answer with 0
runtime overhead, because it is a constant expression (optimising away the
strlen call is seldom performed by compilers, because C doesn't have
the concept of pure functions).

You can even use sizeof s when declaring another array of char that
needs to have the same size as s, without resorting to VLAs.

But it wasn't defined that way. It was ..

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

Because strlen(s) is 14 and sizeof s -1 is 14 we get lucky. ..

fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

... is ugly, depends on s[15] being defined just so, and is ugly.

And you said putc() got called 15 times in all three cases. What
happened to backing off of that? And since when is runtime overhead a
consideration in treating a short string? Come on Dan, you're reaching.
I've never resorted to a VLA in my life. :-)

--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #23

P: n/a
In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
Dan Pop wrote:

In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
>Dan Pop wrote:
>>
>> In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
>>
>> >Christopher Benson-Manica wrote:
>> >>
>> >> 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.
Not if s is defined reasonably:

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

Now sizeof s - 1 is *guaranteed* to produce the right answer with 0
runtime overhead, because it is a constant expression (optimising away the
strlen call is seldom performed by compilers, because C doesn't have
the concept of pure functions).

You can even use sizeof s when declaring another array of char that
needs to have the same size as s, without resorting to VLAs.

But it wasn't defined that way. It was ..

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

Because strlen(s) is 14 and sizeof s -1 is 14 we get lucky. ..


And I explained that, in well written code, using sizeof instead of
strlen provides reliable results and can be used in places where strlen
cannot. There are, of course, cases when only strlen can be used (e.g.
the array definition is not in scope or the array is deliberately
oversized WRT its initialiser).
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

.. is ugly, depends on s[15] being defined just so, and is ugly.
What's wrong with this code if s is defined as I have shown above?
Of course, the division by sizeof(char) is sheer nonsense, but this is
not the point of this discussion.
And you said putc() got called 15 times in all three cases.
Nope, I didn't say that. Either quote me correctly or not at all.
What
happened to backing off of that? And since when is runtime overhead a
consideration in treating a short string?


Are you genuinely being dense or merely pretending? The merits of a
certain programming construct are not judged based on one deliberately
contrived example.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #24

P: n/a
Dan Pop wrote:

In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
Dan Pop wrote:

In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:

>Dan Pop wrote:
>>
>> In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
>>
>> >Christopher Benson-Manica wrote:
>> >>
>> >> 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.

Not if s is defined reasonably:

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

Now sizeof s - 1 is *guaranteed* to produce the right answer with 0
runtime overhead, because it is a constant expression (optimising away the
strlen call is seldom performed by compilers, because C doesn't have
the concept of pure functions).

You can even use sizeof s when declaring another array of char that
needs to have the same size as s, without resorting to VLAs.
But it wasn't defined that way. It was ..

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

Because strlen(s) is 14 and sizeof s -1 is 14 we get lucky. ..


And I explained that, in well written code, using sizeof instead of
strlen provides reliable results and can be used in places where strlen
cannot. There are, of course, cases when only strlen can be used (e.g.
the array definition is not in scope or the array is deliberately
oversized WRT its initialiser).
fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );

.. is ugly, depends on s[15] being defined just so, and is ugly.


What's wrong with this code if s is defined as I have shown above?
Of course, the division by sizeof(char) is sheer nonsense, but this is
not the point of this discussion.
And you said putc() got called 15 times in all three cases.


Nope, I didn't say that. Either quote me correctly or not at all.
What
happened to backing off of that? And since when is runtime overhead a
consideration in treating a short string?


Are you genuinely being dense or merely pretending? The merits of a
certain programming construct are not judged based on one deliberately
contrived example.


I'm not trying to start a fight, only to hold your feet to the fire for
an otherwise trivial error. I have been 'reading' Dan Pop for years and
have certain respect for you, your politeness notwithstanding. Accusing
me of being dense is impolite but not neccessarily wrong. But telling me
I didn't quote you correctly..

The following is the post I was referring to. Is it yours? If not, I
apologise. If it is, I would like a retraction. Please.

Path:
newsspool2.news.atl.earthlink.net!stamper.news.atl .earthlink.net!stamper.news.pas.earthlink.net!elnk-pas-nf1!newsfeed.earthlink.net!news.maxwell.syr.edu!ne ws-hub.siol.net!kanja.arnes.si!irazu.switch.ch!switch .ch!cern.ch!news
From: Da*****@cern.ch (Dan Pop)
Newsgroups: comp.lang.c
Subject: Re: A quick question
Date: 6 Feb 2004 18:45:49 GMT
Organization: DESY Zeuthen
Lines: 20
Message-ID: <c0***********@sunnews.cern.ch>
References: <bv**********@chessie.cirr.com>
NNTP-Posting-Host: lxplus007.cern.ch
X-Trace: sunnews.cern.ch 1076093149 28094 (None) 137.138.4.240
X-Complaints-To: ne**@sunnews.cern.ch
User-Agent: nn/6.6.2
Xref: news.earthlink.net comp.lang.c:592092
X-Received-Date: Fri, 06 Feb 2004 11:12:06 PST
(newsspool2.news.atl.earthlink.net)

In <bv**********@chessie.cirr.com> Christopher Benson-Manica
<at***@nospam.cyberspace.org> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #25

P: n/a
In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
Dan Pop wrote:

In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
>Dan Pop wrote:
>>
>> In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
>>
>> >Dan Pop wrote:
>> >>
>> >> In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
>> >>
>> >> >Christopher Benson-Manica wrote:
>> >> >>
>> >> >> 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.
>>
>> Not if s is defined reasonably:
>>
>> char s[]="Hello, world!\n";
>>
>> Now sizeof s - 1 is *guaranteed* to produce the right answer with 0
>> runtime overhead, because it is a constant expression (optimising away the
>> strlen call is seldom performed by compilers, because C doesn't have
>> the concept of pure functions).
>>
>> You can even use sizeof s when declaring another array of char that
>> needs to have the same size as s, without resorting to VLAs.
>>
>But it wasn't defined that way. It was ..
>
> char s[15]="Hello, world!\n";
>
>Because strlen(s) is 14 and sizeof s -1 is 14 we get lucky. ..
And I explained that, in well written code, using sizeof instead of
strlen provides reliable results and can be used in places where strlen
cannot. There are, of course, cases when only strlen can be used (e.g.
the array definition is not in scope or the array is deliberately
oversized WRT its initialiser).
> fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );
>
>.. is ugly, depends on s[15] being defined just so, and is ugly.


What's wrong with this code if s is defined as I have shown above?
Of course, the division by sizeof(char) is sheer nonsense, but this is
not the point of this discussion.
>And you said putc() got called 15 times in all three cases.


Nope, I didn't say that. Either quote me correctly or not at all.
>What
>happened to backing off of that? And since when is runtime overhead a
>consideration in treating a short string?


Are you genuinely being dense or merely pretending? The merits of a
certain programming construct are not judged based on one deliberately
contrived example.


I'm not trying to start a fight, only to hold your feet to the fire for
an otherwise trivial error. I have been 'reading' Dan Pop for years and
have certain respect for you, your politeness notwithstanding. Accusing
me of being dense is impolite but not neccessarily wrong. But telling me
I didn't quote you correctly..


Was the pure truth, as you're demonstrating yourself below.
The following is the post I was referring to. Is it yours? If not, I
apologise. If it is, I would like a retraction. Please.
It is my text and it doesn't say anywhere that "putc() gets called 15
times in all three cases" or any paraphrase of this text. So, what
*exactly* should I have to retract?

Dan
Path:
newsspool2.news.atl.earthlink.net!stamper.news.at l.earthlink.net!stamper.news.pas.earthlink.net!eln k-pas-nf1!newsfeed.earthlink.net!news.maxwell.syr.edu!ne ws-hub.siol.net!kanja.arnes.si!irazu.switch.ch!switch .ch!cern.ch!news
From: Da*****@cern.ch (Dan Pop)
Newsgroups: comp.lang.c
Subject: Re: A quick question
Date: 6 Feb 2004 18:45:49 GMT
Organization: DESY Zeuthen
Lines: 20
Message-ID: <c0***********@sunnews.cern.ch>
References: <bv**********@chessie.cirr.com>
NNTP-Posting-Host: lxplus007.cern.ch
X-Trace: sunnews.cern.ch 1076093149 28094 (None) 137.138.4.240
X-Complaints-To: ne**@sunnews.cern.ch
User-Agent: nn/6.6.2
Xref: news.earthlink.net comp.lang.c:592092
X-Received-Date: Fri, 06 Feb 2004 11:12:06 PST
(newsspool2.news.atl.earthlink.net)

In <bv**********@chessie.cirr.com> Christopher Benson-Manica
<at***@nospam.cyberspace.org> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #26

P: n/a
Dan Pop wrote:

In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
Dan Pop wrote:

In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:

>Dan Pop wrote:
>>
>> In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
>>
>> >Dan Pop wrote:
>> >>
>> >> In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
>> >>
>> >> >Christopher Benson-Manica wrote:
>> >> >>
>> >> >> 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.
>>
>> Not if s is defined reasonably:
>>
>> char s[]="Hello, world!\n";
>>
>> Now sizeof s - 1 is *guaranteed* to produce the right answer with 0
>> runtime overhead, because it is a constant expression (optimising away the
>> strlen call is seldom performed by compilers, because C doesn't have
>> the concept of pure functions).
>>
>> You can even use sizeof s when declaring another array of char that
>> needs to have the same size as s, without resorting to VLAs.
>>
>But it wasn't defined that way. It was ..
>
> char s[15]="Hello, world!\n";
>
>Because strlen(s) is 14 and sizeof s -1 is 14 we get lucky. ..

And I explained that, in well written code, using sizeof instead of
strlen provides reliable results and can be used in places where strlen
cannot. There are, of course, cases when only strlen can be used (e.g.
the array definition is not in scope or the array is deliberately
oversized WRT its initialiser).

> fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );
>
>.. is ugly, depends on s[15] being defined just so, and is ugly.

What's wrong with this code if s is defined as I have shown above?
Of course, the division by sizeof(char) is sheer nonsense, but this is
not the point of this discussion.

>And you said putc() got called 15 times in all three cases.

Nope, I didn't say that. Either quote me correctly or not at all.

>What
>happened to backing off of that? And since when is runtime overhead a
>consideration in treating a short string?

Are you genuinely being dense or merely pretending? The merits of a
certain programming construct are not judged based on one deliberately
contrived example.


I'm not trying to start a fight, only to hold your feet to the fire for
an otherwise trivial error. I have been 'reading' Dan Pop for years and
have certain respect for you, your politeness notwithstanding. Accusing
me of being dense is impolite but not neccessarily wrong. But telling me
I didn't quote you correctly..


Was the pure truth, as you're demonstrating yourself below.
The following is the post I was referring to. Is it yours? If not, I
apologise. If it is, I would like a retraction. Please.


It is my text and it doesn't say anywhere that "putc() gets called 15
times in all three cases" or any paraphrase of this text. So, what
*exactly* should I have to retract?

Dan
Path:
newsspool2.news.atl.earthlink.net!stamper.news.at l.earthlink.net!stamper.news.pas.earthlink.net!eln k-pas-nf1!newsfeed.earthlink.net!news.maxwell.syr.edu!ne ws-hub.siol.net!kanja.arnes.si!irazu.switch.ch!switch .ch!cern.ch!news
From: Da*****@cern.ch (Dan Pop)
Newsgroups: comp.lang.c
Subject: Re: A quick question
Date: 6 Feb 2004 18:45:49 GMT
Organization: DESY Zeuthen
Lines: 20
Message-ID: <c0***********@sunnews.cern.ch>
References: <bv**********@chessie.cirr.com>
NNTP-Posting-Host: lxplus007.cern.ch
X-Trace: sunnews.cern.ch 1076093149 28094 (None) 137.138.4.240
X-Complaints-To: ne**@sunnews.cern.ch
User-Agent: nn/6.6.2
Xref: news.earthlink.net comp.lang.c:592092
X-Received-Date: Fri, 06 Feb 2004 11:12:06 PST
(newsspool2.news.atl.earthlink.net)

In <bv**********@chessie.cirr.com> Christopher Benson-Manica
<at***@nospam.cyberspace.org> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de


You said..
In the abstract machine, all three of them result in fputc being called
15 times, to output the same sequence of characters, to stdout.
And I said 14 times in a subsequent post.
>And you said putc() got called 15 times in all three cases.

Nope, I didn't say that. Either quote me correctly or not at all.


Ok, what am I missing. You said 15 times and the truth is 14 times. I'm
trying to get you to admit that 14 is the right number. If you are
unwilling to do that, then so be it. I tire of this. Thank you.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #27

P: n/a
In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
You said..
>In the abstract machine, all three of them result in fputc being called
>15 times, to output the same sequence of characters, to stdout.
And I said 14 times in a subsequent post.


And I have *never* challenged this statement of yours, have I?
So, what *exactly* is your point?
>> >And you said putc() got called 15 times in all three cases.
>>
>> Nope, I didn't say that. Either quote me correctly or not at all.


Ok, what am I missing. You said 15 times and the truth is 14 times. I'm
trying to get you to admit that 14 is the right number.


Have I ever challenged your statement that 14 is the right number?

Yes, I was wrong when I wrote 15 and you were right when you wrote 14,
if this is making you any happier.

But this doesn't change the fact that you *misquoted* my statement in your
later post and I have yet to see any sign of repentance on your part.

The thing is that you seem to blissfully ignore is that my mistake is
entirely irrelevant to the *substance* of the discussion, i.e. whether
the three calls are entirely equivalent, in the given context or not.

That's why I didn't bother to post a correction to the 15 number: it
simply didn't matter in context, the part of my statement that did matter
was entirely correct: all three function calls have the same effect in the
abstract machine.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #28

P: n/a
Dan Pop wrote:

In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net> writes:
You said..
>In the abstract machine, all three of them result in fputc being called
>15 times, to output the same sequence of characters, to stdout.
And I said 14 times in a subsequent post.


And I have *never* challenged this statement of yours, have I?
So, what *exactly* is your point?
>> >And you said putc() got called 15 times in all three cases.
>>
>> Nope, I didn't say that. Either quote me correctly or not at all.


Ok, what am I missing. You said 15 times and the truth is 14 times. I'm
trying to get you to admit that 14 is the right number.


Have I ever challenged your statement that 14 is the right number?

Yes, I was wrong when I wrote 15 and you were right when you wrote 14,
if this is making you any happier.

Bingo! Yes, that will do it. Thank you very much.
But this doesn't change the fact that you *misquoted* my statement in your
later post and I have yet to see any sign of repentance on your part.
I don't understand what was misquoted. 'getc' vs 'fgetc' ?
The thing is that you seem to blissfully ignore is that my mistake is
entirely irrelevant to the *substance* of the discussion, i.e. whether
the three calls are entirely equivalent, in the given context or not.
That's what started this for me. The array was contrived to be the
length of the string making (strlen(s) == sizeof s -1). I consider this
unusual at least.
That's why I didn't bother to post a correction to the 15 number: it
simply didn't matter in context, the part of my statement that did matter
was entirely correct: all three function calls have the same effect in the
abstract machine.

Thank you very much for your explanation. I hope I wasn't being too
petty.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #29

P: n/a
nrk
Joe Wright wrote:
Dan Pop wrote:

In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net>
writes:
>Dan Pop wrote:
>>
>> In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net>
>> writes:
>>
>> >Dan Pop wrote:
>> >>
>> >> In <40***********@earthlink.net> Joe Wright
>> >> <jo********@earthlink.net> writes:
>> >>
>> >> >Christopher Benson-Manica wrote:
>> >> >>
>> >> >> 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.
>>
>> Not if s is defined reasonably:
>>
>> char s[]="Hello, world!\n";
>>
>> Now sizeof s - 1 is *guaranteed* to produce the right answer with 0
>> runtime overhead, because it is a constant expression (optimising away
>> the strlen call is seldom performed by compilers, because C doesn't
>> have the concept of pure functions).
>>
>> You can even use sizeof s when declaring another array of char that
>> needs to have the same size as s, without resorting to VLAs.
>>
>But it wasn't defined that way. It was ..
>
> char s[15]="Hello, world!\n";
>
>Because strlen(s) is 14 and sizeof s -1 is 14 we get lucky. ..
And I explained that, in well written code, using sizeof instead of
strlen provides reliable results and can be used in places where strlen
cannot. There are, of course, cases when only strlen can be used (e.g.
the array definition is not in scope or the array is deliberately
oversized WRT its initialiser).
> fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );
>
>.. is ugly, depends on s[15] being defined just so, and is ugly.


What's wrong with this code if s is defined as I have shown above?
Of course, the division by sizeof(char) is sheer nonsense, but this is
not the point of this discussion.
>And you said putc() got called 15 times in all three cases.


Nope, I didn't say that. Either quote me correctly or not at all.
>What
>happened to backing off of that? And since when is runtime overhead a
>consideration in treating a short string?


Are you genuinely being dense or merely pretending? The merits of a
certain programming construct are not judged based on one deliberately
contrived example.


I'm not trying to start a fight, only to hold your feet to the fire for
an otherwise trivial error. I have been 'reading' Dan Pop for years and
have certain respect for you, your politeness notwithstanding. Accusing
me of being dense is impolite but not neccessarily wrong. But telling me
I didn't quote you correctly..

The following is the post I was referring to. Is it yours? If not, I
apologise. If it is, I would like a retraction. Please.

Path:

newsspool2.news.atl.earthlink.net!stamper.news.atl .earthlink.net!stamper.news.pas.earthlink.net!elnk-pas-nf1!newsfeed.earthlink.net!news.maxwell.syr.edu!ne ws-hub.siol.net!kanja.arnes.si!irazu.switch.ch!switch .ch!cern.ch!news From: Da*****@cern.ch (Dan Pop)
Newsgroups: comp.lang.c
Subject: Re: A quick question
Date: 6 Feb 2004 18:45:49 GMT
Organization: DESY Zeuthen
Lines: 20
Message-ID: <c0***********@sunnews.cern.ch>
References: <bv**********@chessie.cirr.com>
NNTP-Posting-Host: lxplus007.cern.ch
X-Trace: sunnews.cern.ch 1076093149 28094 (None) 137.138.4.240
X-Complaints-To: ne**@sunnews.cern.ch
User-Agent: nn/6.6.2
Xref: news.earthlink.net comp.lang.c:592092
X-Received-Date: Fri, 06 Feb 2004 11:12:06 PST
(newsspool2.news.atl.earthlink.net)

In <bv**********@chessie.cirr.com> Christopher Benson-Manica
<at***@nospam.cyberspace.org> writes:
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


Dan seems to have yanked your chain quite successfully :-) He said *fputc*,
not *putc*.

-nrk.
--
Remove devnull for email
Nov 14 '05 #30

P: n/a
In <y6*****************@nwrddc03.gnilink.net> nrk <ra*********@devnull.verizon.net> writes:
Joe Wright wrote:
Dan Pop wrote:

In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net>
writes:

>Dan Pop wrote:
>>
>> In <40***********@earthlink.net> Joe Wright <jo********@earthlink.net>
>> writes:
>>
>> >Dan Pop wrote:
>> >>
>> >> In <40***********@earthlink.net> Joe Wright
>> >> <jo********@earthlink.net> writes:
>> >>
>> >> >Christopher Benson-Manica wrote:
>> >> >>
>> >> >> 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.
>>
>> Not if s is defined reasonably:
>>
>> char s[]="Hello, world!\n";
>>
>> Now sizeof s - 1 is *guaranteed* to produce the right answer with 0
>> runtime overhead, because it is a constant expression (optimising away
>> the strlen call is seldom performed by compilers, because C doesn't
>> have the concept of pure functions).
>>
>> You can even use sizeof s when declaring another array of char that
>> needs to have the same size as s, without resorting to VLAs.
>>
>But it wasn't defined that way. It was ..
>
> char s[15]="Hello, world!\n";
>
>Because strlen(s) is 14 and sizeof s -1 is 14 we get lucky. ..

And I explained that, in well written code, using sizeof instead of
strlen provides reliable results and can be used in places where strlen
cannot. There are, of course, cases when only strlen can be used (e.g.
the array definition is not in scope or the array is deliberately
oversized WRT its initialiser).

> fwrite( s, sizeof(char), sizeof(s)/sizeof(char) - 1, stdout );
>
>.. is ugly, depends on s[15] being defined just so, and is ugly.

What's wrong with this code if s is defined as I have shown above?
Of course, the division by sizeof(char) is sheer nonsense, but this is
not the point of this discussion.

>And you said putc() got called 15 times in all three cases.

Nope, I didn't say that. Either quote me correctly or not at all.

>What
>happened to backing off of that? And since when is runtime overhead a
>consideration in treating a short string?

Are you genuinely being dense or merely pretending? The merits of a
certain programming construct are not judged based on one deliberately
contrived example.


I'm not trying to start a fight, only to hold your feet to the fire for
an otherwise trivial error. I have been 'reading' Dan Pop for years and
have certain respect for you, your politeness notwithstanding. Accusing
me of being dense is impolite but not neccessarily wrong. But telling me
I didn't quote you correctly..

The following is the post I was referring to. Is it yours? If not, I
apologise. If it is, I would like a retraction. Please.

Path:

newsspool2.news.atl.earthlink.net!stamper.news.at l.earthlink.net!stamper.news.pas.earthlink.net!eln k-pas-nf1!newsfeed.earthlink.net!news.maxwell.syr.edu!ne ws-hub.siol.net!kanja.arnes.si!irazu.switch.ch!switch .ch!cern.ch!news
From: Da*****@cern.ch (Dan Pop)
Newsgroups: comp.lang.c
Subject: Re: A quick question
Date: 6 Feb 2004 18:45:49 GMT
Organization: DESY Zeuthen
Lines: 20
Message-ID: <c0***********@sunnews.cern.ch>
References: <bv**********@chessie.cirr.com>
NNTP-Posting-Host: lxplus007.cern.ch
X-Trace: sunnews.cern.ch 1076093149 28094 (None) 137.138.4.240
X-Complaints-To: ne**@sunnews.cern.ch
User-Agent: nn/6.6.2
Xref: news.earthlink.net comp.lang.c:592092
X-Received-Date: Fri, 06 Feb 2004 11:12:06 PST
(newsspool2.news.atl.earthlink.net)

In <bv**********@chessie.cirr.com> Christopher Benson-Manica
<at***@nospam.cyberspace.org> writes:
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


Dan seems to have yanked your chain quite successfully :-) He said *fputc*,
not *putc*.


Furthermore, by prefixing my statement with "in the abstract machine"
I made it clear that fputc need not be *actually* called at all.

These aspects are far more important than the 15 vs 14 issue, yet they
were grossly misquoted...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #31

This discussion thread is closed

Replies have been disabled for this discussion.