printf() and write()

Discussion in 'C Programming' started by ericunfuk, Mar 10, 2007.

  1. ericunfuk

    ericunfuk Guest

    printf("hello");
    write(1,"hello",5);

    Are these two have the same effect?Only the 2nd one work for me
    sometimes?Are there situations that I can only use write() instead of
    printf()?


    Thanks
    ericunfuk, Mar 10, 2007
    #1
    1. Advertising

  2. ericunfuk

    Ian Collins Guest

    ericunfuk wrote:
    > printf("hello");
    > write(1,"hello",5);
    >
    > Are these two have the same effect?Only the 2nd one work for me
    > sometimes?Are there situations that I can only use write() instead of
    > printf()?
    >

    write isn't standard C, its POSIX. You should use fwrite if you want
    your code to be portable. You would use (f)write to write to a stream
    other than the standard output.

    --
    Ian Collins.
    Ian Collins, Mar 10, 2007
    #2
    1. Advertising

  3. ericunfuk

    Nelu Guest

    ericunfuk wrote:
    > printf("hello");
    > write(1,"hello",5);
    >
    > Are these two have the same effect?Only the 2nd one work for me
    > sometimes?Are there situations that I can only use write() instead of
    > printf()?
    >


    write is not part of the C standard.

    What do you mean by "Only the 2nd one work for me"?
    printf("hello") should work as long as you have a correctly
    written C program that include <stdio.h>. It may not print
    anything on the screen but that's because you need to add a new
    line so the output stream gets flushed.

    Try these two programs and let me know which one works:
    P1:
    #include <stdio.h>
    int main(void) {
    printf("hello");
    return 0;
    }

    P2:
    #include <stdio.h>
    int main(void) {
    printf("hello\n");
    return 0;
    }

    The standard C functions use buffered I/O operations so there are
    no guarantees that anything will be output on the screen until
    the stdio buffer is flushed.
    The fwrite function is similar to printf but there are no
    guarantees you will see anything on the screen until the buffer
    is flushed.

    <OT>
    I don't think the write function is buffered (I may be wrong
    though and you should ask about the function in a POSIX group) so
    what you write shows up on the screen right away.
    </OT>

    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
    Nelu, Mar 10, 2007
    #3
  4. ericunfuk wrote:
    > printf("hello");
    > write(1,"hello",5);
    >
    > Are these two have the same effect?Only the 2nd one work for me
    > sometimes?Are there situations that I can only use write() instead of
    > printf()?


    There is no standard C function called 'write'. There is no way to tell
    what your implementation defines 'write' to mean without seeing that
    implementation's documentation. Draw your own conclusion.
    Martin Ambuhl, Mar 10, 2007
    #4
  5. ericunfuk

    santosh Guest

    ericunfuk wrote:
    > printf("hello");
    > write(1,"hello",5);
    >
    > Are these two have the same effect?


    Depends on the definition of write, which is not defined by the C
    Standard. There's a POSIX function by that name though.

    > Only the 2nd one work for me sometimes?


    Any such limitation is likely to involve details of system specific
    nature. You should pursue that angle in a POSIX, UNIX or system
    specific group like comp.unix.programmer.
    santosh, Mar 10, 2007
    #5
  6. On 9 Mar 2007 19:19:01 -0800, in comp.lang.c , "ericunfuk"
    <> wrote:

    >printf("hello");


    try printf("hello\n");

    Without a newline, printf is not guaranteed to flush the output buffer
    to screen, so nothing may appear.

    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Mar 10, 2007
    #6
  7. "Mark McIntyre" <> schrieb im Newsbeitrag
    news:...
    > On 9 Mar 2007 19:19:01 -0800, in comp.lang.c , "ericunfuk"
    > <> wrote:
    >
    >>printf("hello");

    >
    > try printf("hello\n");

    try is C++

    Sorry, could not resists...

    Bye, Jojo
    Joachim Schmitz, Mar 10, 2007
    #7
    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. Peter Hansen
    Replies:
    1
    Views:
    369
    Joona I Palaste
    Aug 24, 2003
  2. Peter Hansen
    Replies:
    1
    Views:
    410
    Joona I Palaste
    Aug 24, 2003
  3. ben
    Replies:
    4
    Views:
    599
    Martin Ambuhl
    Jun 26, 2004
  4. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,221
  5. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    422
Loading...

Share This Page