A quick question

  • Thread starter Christopher Benson-Manica
  • Start date
T

Thomas Matthews

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

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
 
D

Dan Pop

In said:
Dan said:
In said:
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
 
J

Joe Wright

Dan said:
In said:
Dan said:
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. :)
 
D

Dan Pop

In said:
Dan said:
In said:
Dan Pop wrote:


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
 
J

Joe Wright

Dan said:
In said:
Dan said:
Dan Pop wrote:


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!news-hub.siol.net!kanja.arnes.si!irazu.switch.ch!switch.ch!cern.ch!news
From: (e-mail address removed) (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: <[email protected]>
References: <[email protected]>
NNTP-Posting-Host: lxplus007.cern.ch
X-Trace: sunnews.cern.ch 1076093149 28094 (None) 137.138.4.240
X-Complaints-To: (e-mail address removed)
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 <[email protected]> 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 );

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
 
D

Dan Pop

In said:
Dan said:
In said:
Dan Pop wrote:


Dan Pop wrote:


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.atl.earthlink.net!stamper.news.pas.earthlink.net!elnk-pas-nf1!newsfeed.earthlink.net!news.maxwell.syr.edu!news-hub.siol.net!kanja.arnes.si!irazu.switch.ch!switch.ch!cern.ch!news
From: (e-mail address removed) (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: <[email protected]>
References: <[email protected]>
NNTP-Posting-Host: lxplus007.cern.ch
X-Trace: sunnews.cern.ch 1076093149 28094 (None) 137.138.4.240
X-Complaints-To: (e-mail address removed)
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 <[email protected]> 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 );

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:
Dan said:
Dan Pop wrote:


Dan Pop wrote:


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.atl.earthlink.net!stamper.news.pas.earthlink.net!elnk-pas-nf1!newsfeed.earthlink.net!news.maxwell.syr.edu!news-hub.siol.net!kanja.arnes.si!irazu.switch.ch!switch.ch!cern.ch!news
From: (e-mail address removed) (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: <[email protected]>
References: <[email protected]>
NNTP-Posting-Host: lxplus007.cern.ch
X-Trace: sunnews.cern.ch 1076093149 28094 (None) 137.138.4.240
X-Complaints-To: (e-mail address removed)
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 <[email protected]> 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 );

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

You said..

And I said 14 times in a subsequent post.

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

Dan Pop

In said:
You said..


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?
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
 
J

Joe Wright

Dan said:
And I have *never* challenged this statement of yours, have I?
So, what *exactly* is your point?


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

nrk

Joe said:
Dan said:
Dan Pop wrote:

In <[email protected]> Joe Wright <[email protected]>
writes:

Dan Pop wrote:

In <[email protected]> Joe Wright

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!news-hub.siol.net!kanja.arnes.si!irazu.switch.ch!switch.ch!cern.ch!news
From: (e-mail address removed) (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: <[email protected]>
References: <[email protected]>
NNTP-Posting-Host: lxplus007.cern.ch
X-Trace: sunnews.cern.ch 1076093149 28094 (None) 137.138.4.240
X-Complaints-To: (e-mail address removed)
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 <[email protected]> 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 );

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

Dan Pop

In said:
Joe said:
Dan said:
In <[email protected]> Joe Wright <[email protected]>
writes:

Dan Pop wrote:

In <[email protected]> Joe Wright <[email protected]>
writes:

Dan Pop wrote:

In <[email protected]> Joe Wright

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!news-hub.siol.net!kanja.arnes.si!irazu.switch.ch!switch.ch!cern.ch!news
From: (e-mail address removed) (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: <[email protected]>
References: <[email protected]>
NNTP-Posting-Host: lxplus007.cern.ch
X-Trace: sunnews.cern.ch 1076093149 28094 (None) 137.138.4.240
X-Complaints-To: (e-mail address removed)
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 <[email protected]> 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 );

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
 

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,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top