EOF from printf

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

    EOF from printf

    When "printf" function returns "EOF"? Will "printf" ever fail?

  • Keith Thompson

    #2
    Re: EOF from printf

    "v4vijayaku mar" <vijayakumar.su bburaj@gmail.co mwrites:
    When "printf" function returns "EOF"? Will "printf" ever fail?
    C99 7.19.6.3p3:

    The printf function returns the number of characters transmitted,
    or a negative value if an output or encoding error occurred.

    The negative value isn't necessarily the same as EOF, and you
    shouldn't assume that it is.

    Any decent C textbook, or your system's documentation, should answer
    this for you.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

    Comment

    • v4vijayakumar

      #3
      Re: EOF from printf

      On Mar 27, 1:12 pm, Keith Thompson <k...@mib.orgwr ote:
      "v4vijayaku mar" <vijayakumar.su bbu...@gmail.co mwrites:
      When "printf" function returns "EOF"? Will "printf" ever fail?
      >
      C99 7.19.6.3p3:
      >
      The printf function returns the number of characters transmitted,
      or a negative value if an output or encoding error occurred.
      >
      The negative value isn't necessarily the same as EOF, and you
      shouldn't assume that it is.
      >
      Thanks. I tried, "man 3 printf" in cygwin. It says,

      ---
      RETURNS
      `sprintf' and `asprintf' return the number of bytes in
      the output
      string, save that the concluding `NULL' is not counted.
      `printf' and
      `fprintf' return the number of characters transmitted. If
      an error
      occurs, `printf' and `fprintf' return `EOF' and `asprintf'
      returns -1.
      No error returns occur for `sprintf'.
      ---

      I just wanted to know, in which cases it can fail.

      Why it returns negative value, when "EOF" would suffice? Does the
      negative
      return value has any more information for the failure?

      Comment

      • Servé Laurijssen

        #4
        Re: EOF from printf


        "v4vijayaku mar" <vijayakumar.su bburaj@gmail.co mwrote in message
        news:1174979387 .734000.8370@r5 6g2000hsd.googl egroups.com...
        When "printf" function returns "EOF"? Will "printf" ever fail?
        On some systems printf causes data to be printed on a serial line. printf
        doesnt need to print to a computer screen, on some systems it can be really
        useful to test for the return value of printf


        Comment

        • Malcolm McLean

          #5
          Re: EOF from printf

          "v4vijayaku mar" <vijayakumar.su bburaj@gmail.co mwrote in message
          When "printf" function returns "EOF"? Will "printf" ever fail?
          >
          Not printf() never fails. It is like my Volvo. It has never been for a
          repair in over five years.

          Seriously, there are all sorts of things that could possibly go wrong with
          printf(), such as the operating system running out of memory, or the
          terminal crashing. But on a modern system they won't and the possibility can
          be discounted.

          If you are running on some sort of 4-bit digital watcvh with a teletype
          attrached for debugging it might be a different story.

          --
          Free games and programming goodies.



          Comment

          • =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=

            #6
            Re: EOF from printf

            Malcolm McLean wrote:
            "v4vijayaku mar" <vijayakumar.su bburaj@gmail.co mwrote in message
            When "printf" function returns "EOF"? Will "printf" ever fail?
            Not printf() never fails. It is like my Volvo. It has never been for a
            repair in over five years.
            >
            Seriously, there are all sorts of things that could possibly go wrong with
            printf(), such as the operating system running out of memory, or the
            terminal crashing. But on a modern system they won't and the possibility can
            be discounted.
            >
            If you are running on some sort of 4-bit digital watcvh with a teletype
            attrached for debugging it might be a different story.
            On modern systems, printf can easily fail when output is not directed
            to any sort of terminal. An easy example is running out of disk space.

            Comment

            • CBFalconer

              #7
              Re: EOF from printf

              v4vijayakumar wrote:
              >
              When "printf" function returns "EOF"? Will "printf" ever fail?
              When "printf" returns EOF the function has already failed. From
              N869:

              [#14] The fprintf function returns the number of characters
              transmitted, or a negative value if an output or encoding
              error occurred.

              However, it may never return EOF.

              --
              Chuck F (cbfalconer at maineline dot net)
              Available for consulting/temporary embedded and systems.
              <http://cbfalconer.home .att.net>



              --
              Posted via a free Usenet account from http://www.teranews.com

              Comment

              • Flash Gordon

                #8
                Re: EOF from printf

                Malcolm McLean wrote, On 27/03/07 20:42:
                "v4vijayaku mar" <vijayakumar.su bburaj@gmail.co mwrote in message
                >When "printf" function returns "EOF"? Will "printf" ever fail?
                >>
                Not printf() never fails. It is like my Volvo. It has never been for a
                repair in over five years.
                >
                Seriously, there are all sorts of things that could possibly go wrong
                with printf(), such as the operating system running out of memory, or
                the terminal crashing. But on a modern system they won't and the
                possibility can be discounted.
                >
                If you are running on some sort of 4-bit digital watcvh with a teletype
                attrached for debugging it might be a different story.
                Or someone has redirected standard output to a file and the disk has
                filed up (this does happen). Or standard output is redirected to a
                TCP/IP port and the connection has disappeared (this does happen). Or
                someone is running the program using a remote shell and the connection
                has dropped (this does happen). I'm sure if I tried I could come up with
                more examples where printf would fail, these are just a few REAL
                examples where printf CAN fail on a MODERN system.
                --
                Flash Gordon

                Comment

                • Mark L Pappin

                  #9
                  Re: EOF from printf

                  CBFalconer <cbfalconer@yah oo.comwrites:
                  v4vijayakumar wrote:
                  >When "printf" function returns "EOF"? Will "printf" ever fail?
                  >
                  When "printf" returns EOF the function has already failed.
                  ....
                  However, it may never return EOF.
                  To clarify for others: this final statement is almost certainly
                  intended to mean

                  It is possible for printf() to never return EOF.

                  (There are many other negative values it could return, all of which
                  indicate occurrence of an error.)


                  (Chuck's final statement _could_ be read as

                  It is not possible for printf() to return EOF.

                  This reading would be incorrect, but the ambiguity of "may never VERB"
                  leads as validly to this as to the previous parsing.)

                  mlp

                  Comment

                  • Richard Bos

                    #10
                    Re: EOF from printf

                    "v4vijayaku mar" <vijayakumar.su bburaj@gmail.co mwrote:
                    On Mar 27, 1:12 pm, Keith Thompson <k...@mib.orgwr ote:
                    "v4vijayaku mar" <vijayakumar.su bbu...@gmail.co mwrites:
                    When "printf" function returns "EOF"? Will "printf" ever fail?
                    C99 7.19.6.3p3:

                    The printf function returns the number of characters transmitted,
                    or a negative value if an output or encoding error occurred.

                    The negative value isn't necessarily the same as EOF, and you
                    shouldn't assume that it is.
                    >
                    Thanks. I tried, "man 3 printf" in cygwin.
                    That gives you the Ganuck definition of printf(), which may include
                    more, or even fewer, restrictions than the ISO C one.
                    I just wanted to know, in which cases it can fail.
                    Any number of causes which can stop a program from reading a file. For
                    example, if the file was on a USB stick and you just pulled it out; or
                    if the disk has a read error; well, you name it, really.
                    Why it returns negative value, when "EOF" would suffice?
                    Contrariwise, why demand that it return EOF (not "EOF"; printf() returns
                    an int, so it cannot possibly return "EOF", which is a string literal),
                    when allowing it to return any negative value works fine?

                    Richard

                    Comment

                    • Richard Bos

                      #11
                      Re: EOF from printf

                      rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
                      "v4vijayaku mar" <vijayakumar.su bburaj@gmail.co mwrote:
                      >
                      Thanks. I tried, "man 3 printf" in cygwin.
                      >
                      That gives you the Ganuck definition of printf(), which may include
                      more, or even fewer, restrictions than the ISO C one.
                      >
                      I just wanted to know, in which cases it can fail.
                      >
                      Any number of causes which can stop a program from reading a file. For
                      example, if the file was on a USB stick and you just pulled it out; or
                      if the disk has a read error; well, you name it, really.
                      Erm. s/reading/writing/ and s/read/write/, obviously. The point being,
                      that printf() prints to stdout, which may be a screen, but also may be
                      any other default output device, including (possibly the most often used
                      alternative) files. But never an input file... *Slaps self*

                      Richard

                      Comment

                      • CBFalconer

                        #12
                        Re: EOF from printf

                        Mark L Pappin wrote:
                        CBFalconer <cbfalconer@yah oo.comwrites:
                        >v4vijayakuma r wrote:
                        >>
                        >>When "printf" function returns "EOF"? Will "printf" ever fail?
                        >>
                        >When "printf" returns EOF the function has already failed.
                        ...
                        >
                        >However, it may never return EOF.
                        >
                        To clarify for others: this final statement is almost certainly
                        intended to mean
                        >
                        It is possible for printf() to never return EOF.
                        >
                        (There are many other negative values it could return, all of
                        which indicate occurrence of an error.)
                        Correct. I suspect this is an outgrowth of existing practice in
                        the days of preparing C89. EOF satisfies the requirements of
                        printf, but there probably existed systems that did otherwise. So
                        you have to apply the < 0 test.

                        --
                        Chuck F (cbfalconer at maineline dot net)
                        Available for consulting/temporary embedded and systems.
                        <http://cbfalconer.home .att.net>



                        --
                        Posted via a free Usenet account from http://www.teranews.com

                        Comment

                        • Default User

                          #13
                          Re: EOF from printf

                          Mark L Pappin wrote:
                          CBFalconer <cbfalconer@yah oo.comwrites:
                          >
                          v4vijayakumar wrote:
                          When "printf" function returns "EOF"? Will "printf" ever fail?
                          When "printf" returns EOF the function has already failed.
                          ...
                          >
                          However, it may never return EOF.
                          >
                          To clarify for others: this final statement is almost certainly
                          intended to mean
                          >
                          It is possible for printf() to never return EOF.
                          >
                          (There are many other negative values it could return, all of which
                          indicate occurrence of an error.)
                          >
                          >
                          (Chuck's final statement could be read as
                          >
                          It is not possible for printf() to return EOF.
                          >
                          This reading would be incorrect, but the ambiguity of "may never VERB"
                          leads as validly to this as to the previous parsing.)
                          I think "it might never return EOF" would be less ambiguous.




                          Brian

                          Comment

                          • Keith Thompson

                            #14
                            Re: EOF from printf

                            rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
                            "v4vijayaku mar" <vijayakumar.su bburaj@gmail.co mwrote:
                            [...]
                            >Thanks. I tried, "man 3 printf" in cygwin.
                            >
                            That gives you the Ganuck definition of printf(), which may include
                            more, or even fewer, restrictions than the ISO C one.
                            A mostly off-topic quibble:

                            I assume that "Ganuck" is meant to refer to gcc. In fact, there is no
                            gcc definition of printf. In Cygwin, the proided printf is part of
                            glibc, which is a separate package.

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                            "We must do something. This is something. Therefore, we must do this."
                            -- Antony Jay and Jonathan Lynn, "Yes Minister"

                            Comment

                            • Keith Thompson

                              #15
                              Re: EOF from printf

                              Keith Thompson <kst-u@mib.orgwrites :
                              [...]
                              I assume that "Ganuck" is meant to refer to gcc. In fact, there is no
                              gcc definition of printf. In Cygwin, the proided printf is part of
                              glibc, which is a separate package.
                              I meant "provided", of course.

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                              "We must do something. This is something. Therefore, we must do this."
                              -- Antony Jay and Jonathan Lynn, "Yes Minister"

                              Comment

                              Working...