473,769 Members | 6,126 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 #1
30 1952
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)cybers pace.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
Christopher Benson-Manica <at***@nospam.c yberspace.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
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
Hello,

"Christophe r Benson-Manica" <at***@nospam.c yberspace.org> a écrit dans le
message de news: bv**********@ch essie.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)cybers pace.org | don't, I need to know. Flames welcome.

Nov 14 '05 #5
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
Eric Sosman <Er*********@su n.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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #7
Mark A. Odell wrote:
Christopher Benson-Manica <at***@nospam.c yberspace.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
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
Joe Wright <jo********@ear thlink.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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #10

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

Similar topics

0
9423
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
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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...
0
8872
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
7409
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
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
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.