About the EOF

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

    #1

    About the EOF

    Hi everyone! I am a beginner in C. I knew the EOF is -1, but can anyone
    tell me how can I type the EOF on the screen using the keyboard?

  • nobody

    #2
    Re: About the EOF

    On Sat, 21 Jan 2006 00:28:00 -0800, Kies Lee wrote:
    [color=blue]
    > Hi everyone! I am a beginner in C. I knew the EOF is -1, but can anyone
    > tell me how can I type the EOF on the screen using the keyboard?[/color]

    Type <Ctrl>+d .


    Comment

    • Malcolm

      #3
      Re: About the EOF


      "Kies Lee" <lleelm@gmail.c om> wrote in message[color=blue]
      > Hi everyone! I am a beginner in C. I knew the EOF is -1, but can anyone
      > tell me how can I type the EOF on the screen using the keyboard?
      >[/color]
      It depends on your sytem.
      Try CTRL-Z to generate an EOF from the keyboard in DOS.

      You cannot print an EOF chararacter on the screen (normally). If you try,
      what will probably happen is that it interprets a -1 as 255, which may be a
      printable character or may not be.
      To do this

      printf("%***%c* **\n", EOF);

      it's probably technically undefined behaviour, but you will see the result.


      Comment

      • Vladimir S. Oka

        #4
        Re: About the EOF

        Malcolm wrote:
        [color=blue]
        >
        > "Kies Lee" <lleelm@gmail.c om> wrote in message[color=green]
        >> Hi everyone! I am a beginner in C. I knew the EOF is -1, but
        >> can anyone tell me how can I type the EOF on the screen using
        >> the keyboard?
        >>[/color]
        > It depends on your sytem.
        > Try CTRL-Z to generate an EOF from the keyboard in DOS.[/color]

        And CTRL-D on Linux/Unix... YMMV
        [color=blue]
        >
        > You cannot print an EOF chararacter on the screen (normally).[/color]

        True, but you can print its value, as it's guaranteed to be an
        int. Store your return value into an int variable, and once you
        get an EOF print it out as an integer.
        [color=blue]
        > If you try, what will probably happen is that it interprets a
        > -1 as 255, which may be a printable character or may not be.
        > To do this
        >
        > printf("%***%c* **\n", EOF);
        >
        > it's probably technically undefined behaviour, but you will
        > see the result.[/color]

        It's wrong in the sense that you're trying to print an int using
        a char format specifier. If you used %d, you'd get the value of
        EOF as defined on your system (it's not necessarily a valid
        character).

        Cheers

        Vladimir

        Comment

        • Flash Gordon

          #5
          Re: About the EOF

          Kies Lee wrote:[color=blue]
          > Hi everyone! I am a beginner in C. I knew the EOF is -1,[/color]

          I'm sorry, but you misunderstand. EOF is a macro that expands to a
          negative integer, and on many systems that integers is -1, but that is
          not guaranteed. Whatever value EOF expands to is the value that various
          IO functions return to indicate and end-of-file or error condition.
          [color=blue]
          > but can anyone
          > tell me how can I type the EOF on the screen using the keyboard?[/color]

          It depends on your system and is thus not topical here. It could be
          control+Z, control+D or anything else.

          You should have read the comp.lang.c FAQ before posting, in which case
          you would have found http://c-faq.com/stdio/eofval.html
          --
          Flash Gordon
          Living in interesting times.
          Although my email address says spam, it is real and I read it.

          Comment

          • Emmanuel Delahaye

            #6
            Re: About the EOF

            Kies Lee a écrit :[color=blue]
            > Hi everyone! I am a beginner in C. I knew the EOF is -1,[/color]

            It's not. The standard says that EOF is a negative int. That said, it is
            possible that your implementation choose the value of -1, as recommended
            by POSIX (which is not part of the C-language, but a strong standard
            either).
            [color=blue]
            > but can anyone
            > tell me how can I type the EOF on the screen using the keyboard?[/color]

            Badly worded, but you probably want to know how to trig an end of
            reading when reading from the keyboard. It's a system issue:

            MS-DOS/Windows : crtl-z<enter>
            Unixoid : ctrl-d

            etc.

            --
            A+

            Emmanuel Delahaye

            Comment

            • Martin Ambuhl

              #7
              Re: About the EOF

              Kies Lee wrote:[color=blue]
              > Hi everyone! I am a beginner in C. I knew the EOF is -1,[/color]

              No, you know no such thing. EOF is a macro which expands to an integer
              constant expression, with type int and a negative value. There is no
              reason to suppose that that negative value is -1.
              [color=blue]
              > but can anyone
              > tell me how can I type the EOF on the screen using the keyboard?[/color]

              You need to check the FAQ before posting questions. The question you
              need to check in particular is found at
              <http://c-faq.com/stdio/eofval.html>, "I have a simple little program
              that reads characters until EOF, but how do I actually enter that
              ``EOF'' value from the keyboard? I see that EOF is defined by <stdio.h>
              to be -1; am I supposed to enter -1?". Notice the similarity to your
              question?

              Comment

              • Keith Thompson

                #8
                Re: About the EOF

                "Kies Lee" <lleelm@gmail.c om> writes:[color=blue]
                > Hi everyone! I am a beginner in C. I knew the EOF is -1, but can anyone
                > tell me how can I type the EOF on the screen using the keyboard?[/color]

                No EOF isn't necessarily -1; the standard merely says that it's a
                negative value. (Most, possibly all, implementations use -1, but you
                shouldn't depend on it.)

                Keep in mind that end-of-file is a condition, not a character; the
                value EOF is used to indicate that you've reached the end of the file.

                How you cause and end-of-file condition varies from one system to
                another. On Unix-like systems control-D is most common (once at the
                beginning of a line, twice in the middle of a line). On DOS and
                Windows systems, control-Z is most common.

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

                Comment

                Working...