473,782 Members | 2,534 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
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.

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

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.

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

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.

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
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.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #15
Joe Wright <jo********@ear thlink.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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #16
Christopher Benson-Manica wrote:

Joe Wright <jo********@ear thlink.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
Joe Wright <jo********@ear thlink.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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #18
Christopher Benson-Manica wrote:

Joe Wright <jo********@ear thlink.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
Joe Wright <jo********@ear thlink.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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #20

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
10313
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
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...
0
6735
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
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
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

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.