Representing EOF in C code?

后端 未结 10 526
夕颜
夕颜 2020-11-29 00:24

The newline character is represented by \"\\n\" in C code. Is there an equivalent for the end-of-file (EOF) character?

相关标签:
10条回答
  • 2020-11-29 00:44

    I've read all the comments. It's interesting to notice what happens when you print out this:

    printf("\nInteger =    %d\n", EOF);             //OUTPUT = -1
    printf("Decimal =    %d\n", EOF);               //OUTPUT = -1
    printf("Octal =  %o\n", EOF);                   //OUTPUT = 37777777777
    printf("Hexadecimal =  %x\n", EOF);             //OUTPUT = ffffffff
    printf("Double and float =  %f\n", EOF);        //OUTPUT = 0.000000
    printf("Long double =  %Lf\n", EOF);            //OUTPUT = 0.000000
    printf("Character =  %c\n", EOF);               //OUTPUT = nothing
    

    As we can see here, EOF is NOT a character (whatsoever).

    0 讨论(0)
  • 2020-11-29 00:53

    EOF is not a character (in most modern operating systems). It is simply a condition that applies to a file stream when the end of the stream is reached. The confusion arises because a user may signal EOF for console input by typing a special character (e.g Control-D in Unix, Linux, et al), but this character is not seen by the running program, it is caught by the operating system which in turn signals EOF to the process.

    Note: in some very old operating systems EOF was a character, e.g. Control-Z in CP/M, but this was a crude hack to avoid the overhead of maintaining actual file lengths in file system directories.

    0 讨论(0)
  • 2020-11-29 00:55

    The EOF character recognized by the command interpreter on Windows (and MSDOS, and CP/M) is 0x1a (decimal 26, aka Ctrl+Z aka SUB)

    It can still be be used today for example to mark the end of a human-readable header in a binary file: if the file begins with "Some description\x1a" the user can dump the file content to the console using the TYPE command and the dump will stop at the EOF character, i.e. print Some description and stop, instead of continuing with the garbage that follows.

    0 讨论(0)
  • 2020-11-29 00:56

    No. EOF is not a character, but a state of the filehandle.

    While there are there are control characters in the ASCII charset that represents the end of the data, these are not used to signal the end of files in general. For example EOT (^D) which in some cases almost signals the same.

    When the standard C library uses signed integer to return characters and uses -1 for end of file, this is actually just the signal to indicate than an error happened. I don't have the C standard available, but to quote SUSv3:

    If the end-of-file indicator for the stream is set, or if the stream is at end-of-file, the end-of-file indicator for the stream shall be set and fgetc() shall return EOF. If a read error occurs, the error indicator for the stream shall be set, fgetc() shall return EOF, and shall set errno to indicate the error.

    0 讨论(0)
  • 2020-11-29 00:56

    This is system dependent but often -1. See here

    0 讨论(0)
  • 2020-11-29 00:56

    The value of EOF can't be confused with any real character.

    If a= getchar(), then we must declare a big enough to hold any value that getchar() returns. We can't use char since a must be big enough to hold EOF in addition to characters.

    0 讨论(0)
提交回复
热议问题