printf() and write()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ericunfuk

    printf() and write()

    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

  • Ian Collins

    #2
    Re: printf() and write()

    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.

    Comment

    • Nelu

      #3
      Re: printf() and write()

      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...)

      Comment

      • Martin Ambuhl

        #4
        Re: printf() and write()

        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.

        Comment

        • santosh

          #5
          Re: printf() and write()

          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.progr ammer.

          Comment

          • Mark McIntyre

            #6
            Re: printf() and write()

            On 9 Mar 2007 19:19:01 -0800, in comp.lang.c , "ericunfuk"
            <xuwenduan2010@ gmail.comwrote:
            >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

            Comment

            • Joachim Schmitz

              #7
              Re: printf() and write()

              "Mark McIntyre" <markmcintyre@s pamcop.netschri eb im Newsbeitrag
              news:m785v2l9gh nkurlc87l2q1ud9 3tovckaph@4ax.c om...
              On 9 Mar 2007 19:19:01 -0800, in comp.lang.c , "ericunfuk"
              <xuwenduan2010@ gmail.comwrote:
              >
              >>printf("hello ");
              >
              try printf("hello\n ");
              try is C++

              Sorry, could not resists...

              Bye, Jojo


              Comment

              Working...