What Is the Ascii Value of Eof in C

Value of EOF under windows

Entering "-1" as text does not return the integer value -1 but two characters, i.e. a '-' (which corresponds to ASCII value 45) and a '1' (which corresponds to ASCII value 49). Both do not compare equal to EOF (which is -1 in decimal).

By definition, you cannot enter something that is consumed as negative value by getchar(), as negative values are defined to represent the "End-of-file".

If you want to read in an integral value (like -1), use scanf:

int num;
if (scanf("%d", &num)==1) { // successfully read one valid integral value?
printf("you entered number %d:", num);
}

What is EOF in the C programming language?

On Linux systems and OS X, the character to input to cause an EOF is Ctrl-D. For Windows, it's Ctrl-Z.

Depending on the operating system, this character will only work if it's the first character on a line, i.e. the first character after an Enter. Since console input is often line-oriented, the system may also not recognize the EOF character until after you've followed it up with an Enter.

And yes, if that character is recognized as an EOF, then your program will never see the actual character. Instead, a C program will get a -1 from getchar().

Representing EOF in C code?

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.

What is value of EOF and '\0' in C

EOF is a macro which expands to an integer constant expression with type int and an implementation dependent negative value but is very commonly -1.

'\0' is a char with value 0 in C++ and an int with the value 0 in C.

The reason why printf("%d",a==EOF); resulted in 1 was because you didn't assign the value EOF to a. Instead you checked if a was equal to EOF and since that was true (a == -1 == EOF) it printed 1.

EOF symbolic constant

So, why does it appear that ASCII has a character (255) designated for EOF?

It hasn't. More precisely, that's not the EOF "character".

The trick is, getchar() will always return non-negative values if it has something to read. It will only return -1 (that's what EOF appears to be defined on your implementation) if it encounters end-of-file.

The fact that char is:

  1. 8 bits wide,
  2. signed and
  3. uses a 2's complement representation,

is just a quirk of your implementation (although overwhelmingly common nowadays). Thus, if you are using a char to store the return value of getchar(), then reading the input may terminate prematurely: the character with code 255 will be mistaken for -1 a. k. a. EOF, which is an error. This is just what happened to you. It didn't work -- conversely, your second approach was completely broken.

Printing the value of EOF

putchar function prints a character.

But EOF is not a character and is used to indicate the End of a file. So the getchar returns a value which is distinguishable from the character sets so as to indicate there is no more input.

So printing EOF using putchar() wont print any values

printing it as integer

printf("%d",EOF);

gives result -1

Why can't I find the value of EOF in C?

Because if c is EOF, the while loop terminates (or won't even start, if it is already EOF on the first character typed). The condition for running another iteration of the loop is that c is NOT EOF.



Related Topics



Leave a reply



Submit