473,782 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A quick question

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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05
30 1954
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_tx t,
1,
sizeof(hello_tx t) / 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.l earn.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
In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.net> writes:
Dan Pop wrote:

In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.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
Dan Pop wrote:

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

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

In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.net> writes:
>Dan Pop wrote:
>>
>> In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.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
consideratio n 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
Dan Pop wrote:

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

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

>Dan Pop wrote:
>>
>> In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.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
consideratio n 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.new s.atl.earthlink .net!stamper.ne ws.pas.earthlin k.net!elnk-pas-nf1!newsfeed.ea rthlink.net!new s.maxwell.syr.e du!news-hub.siol.net!ka nja.arnes.si!ir azu.switch.ch!s witch.ch!cern.c h!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**********@c hessie.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.ce rn.ch
User-Agent: nn/6.6.2
Xref: news.earthlink. net comp.lang.c:592 092
X-Received-Date: Fri, 06 Feb 2004 11:12:06 PST
(newsspool2.new s.atl.earthlink .net)

In <bv**********@c hessie.cirr.com > Christopher Benson-Manica
<at***@nospam.c yberspace.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
In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.net> writes:
Dan Pop wrote:

In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.net> writes:
>Dan Pop wrote:
>>
>> In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.net> writes:
>>
>> >Dan Pop wrote:
>> >>
>> >> In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.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
>consideratio n 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.new s.atl.earthlink .net!stamper.ne ws.atl.earthlin k.net!stamper.n ews.pas.earthli nk.net!elnk-pas-nf1!newsfeed.ea rthlink.net!new s.maxwell.syr.e du!news-hub.siol.net!ka nja.arnes.si!ir azu.switch.ch!s witch.ch!cern.c h!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**********@c hessie.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.ce rn.ch
User-Agent: nn/6.6.2
Xref: news.earthlink. net comp.lang.c:592 092
X-Received-Date: Fri, 06 Feb 2004 11:12:06 PST
(newsspool2.ne ws.atl.earthlin k.net)

In <bv**********@c hessie.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
Dan Pop wrote:

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

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

>Dan Pop wrote:
>>
>> In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.net> writes:
>>
>> >Dan Pop wrote:
>> >>
>> >> In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.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
>consideratio n 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.new s.atl.earthlink .net!stamper.ne ws.atl.earthlin k.net!stamper.n ews.pas.earthli nk.net!elnk-pas-nf1!newsfeed.ea rthlink.net!new s.maxwell.syr.e du!news-hub.siol.net!ka nja.arnes.si!ir azu.switch.ch!s witch.ch!cern.c h!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**********@c hessie.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.ce rn.ch
User-Agent: nn/6.6.2
Xref: news.earthlink. net comp.lang.c:592 092
X-Received-Date: Fri, 06 Feb 2004 11:12:06 PST
(newsspool2.ne ws.atl.earthlin k.net)

In <bv**********@c hessie.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
In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.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
Dan Pop wrote:

In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.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
nrk
Joe Wright wrote:
Dan Pop wrote:

In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.net>
writes:
>Dan Pop wrote:
>>
>> In <40***********@ earthlink.net> Joe Wright <jo********@ear thlink.net>
>> writes:
>>
>> >Dan Pop wrote:
>> >>
>> >> In <40***********@ earthlink.net> Joe Wright
>> >> <jo********@ear thlink.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
>consideratio n 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.new s.atl.earthlink .net!stamper.ne ws.pas.earthlin k.net!elnk-pas-nf1!newsfeed.ea rthlink.net!new s.maxwell.syr.e du!news-hub.siol.net!ka nja.arnes.si!ir azu.switch.ch!s witch.ch!cern.c h!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**********@c hessie.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.ce rn.ch
User-Agent: nn/6.6.2
Xref: news.earthlink. net comp.lang.c:592 092
X-Received-Date: Fri, 06 Feb 2004 11:12:06 PST
(newsspool2.new s.atl.earthlink .net)

In <bv**********@c hessie.cirr.com > Christopher Benson-Manica
<at***@nospam.c yberspace.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
9641
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.