A quick question

Discussion in 'C Programming' started by Christopher Benson-Manica, Feb 5, 2004.

  1. 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.
     
    Christopher Benson-Manica, Feb 5, 2004
    #1
    1. Advertising

  2. 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
     
    Darrell Grainger, Feb 5, 2004
    #2
    1. Advertising

  3. Christopher Benson-Manica

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, Feb 5, 2004
    #3
  4. 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.
     
    Thomas Stegen CES2000, Feb 5, 2004
    #4
  5. Christopher Benson-Manica <> wrote in
    news:bvu8v2$6di$:

    > 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 ->
    --
     
    Mark A. Odell, Feb 5, 2004
    #5
  6. Hello,

    "Christopher Benson-Manica" <> a écrit dans le
    message de news: bvu8v2$6di$...
    > 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.
     
    Régis Troadec, Feb 5, 2004
    #6
  7. Eric Sosman <> 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.
     
    Christopher Benson-Manica, Feb 5, 2004
    #7
  8. Christopher Benson-Manica

    Artie Gold Guest

    Mark A. Odell wrote:
    > Christopher Benson-Manica <> wrote in
    > news:bvu8v2$6di$:
    >
    >
    >>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!"
     
    Artie Gold, Feb 5, 2004
    #8
  9. Christopher Benson-Manica

    Joe Wright Guest

    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 ---
     
    Joe Wright, Feb 6, 2004
    #9
  10. Joe Wright <> 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.
     
    Christopher Benson-Manica, Feb 6, 2004
    #10
  11. Christopher Benson-Manica

    Dan Pop Guest

    In <> Joe Wright <> 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:
     
    Dan Pop, Feb 6, 2004
    #11
  12. Christopher Benson-Manica

    Dan Pop Guest

    In <bvu8v2$6di$> Christopher Benson-Manica <> 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:
     
    Dan Pop, Feb 6, 2004
    #12
  13. Christopher Benson-Manica

    Joe Wright Guest

    Dan Pop wrote:
    >
    > In <bvu8v2$6di$> Christopher Benson-Manica <> 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 ---
     
    Joe Wright, Feb 6, 2004
    #13
  14. Christopher Benson-Manica

    Joe Wright Guest

    Christopher Benson-Manica wrote:
    >
    > Joe Wright <> 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 ---
     
    Joe Wright, Feb 6, 2004
    #14
  15. Christopher Benson-Manica

    Joe Wright Guest

    Dan Pop wrote:
    >
    > In <> Joe Wright <> 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 ---
     
    Joe Wright, Feb 6, 2004
    #15
  16. Joe Wright <> 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.
     
    Christopher Benson-Manica, Feb 7, 2004
    #16
  17. Christopher Benson-Manica

    Joe Wright Guest

    Christopher Benson-Manica wrote:
    >
    > Joe Wright <> 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 ---
     
    Joe Wright, Feb 7, 2004
    #17
  18. Joe Wright <> 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.
     
    Christopher Benson-Manica, Feb 8, 2004
    #18
  19. Christopher Benson-Manica

    Joe Wright Guest

    Christopher Benson-Manica wrote:
    >
    > Joe Wright <> 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 ---
     
    Joe Wright, Feb 8, 2004
    #19
  20. Joe Wright <> 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.
     
    Christopher Benson-Manica, Feb 9, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jack
    Replies:
    1
    Views:
    920
  2. jack

    Quick perl question 2

    jack, Aug 25, 2003, in forum: Perl
    Replies:
    1
    Views:
    918
  3. Jorell

    Quick Question

    Jorell, Aug 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    321
    Jorell
    Aug 8, 2003
  4. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    698
    Iain Norman
    Feb 16, 2005
  5. JKop
    Replies:
    11
    Views:
    937
Loading...

Share This Page