Why is the language EOF IS -1 in c?

in c I use EOF ..... why is EOF IS -1? why not another value?

+3
source share
5 answers

From Wikipedia :

The actual value of EOF is system dependent (but usually -1, for example, in glibc ) and does not match any valid character code.

There can be no value at 0 - 255, because these are valid values ​​for characters on most systems. For example, if EOF were 0, then you could not tell the difference between reading 0 and reaching the end of the file.

-1 is the obvious remaining choice.

Instead, you can use feof:

EOF , , feof ferror .

+10

. , . , char. -1, C.

-1 , (. < ctype.h > ) . "" unsigned char.

[:] , , -1. , . , , char 8 , . , unsigned char, , int. , ? -1 .

+4
int c;

c = getchar();
while(!feof(stdin) && !ferror(stdin)) {
  ...
  c = getchar();
}

, , . , :

int c;

c = getchar();
while(!isspace(c)) {
  ...
  c = getchar();
}

EOF , ( ). :

int c;

c = getchar();
while(!feof(stdin) && !ferror(stdin) && !isspace(c)) {
  ...
  c = getchar();
}

, , EOF -1, promises , int.

0

EOF. C EOF = -1 , , EOF , c -1;

:

#include <stdio.h>
#define EOF 22
main()
{
   int a;
   a=EOF;
   printf(" Value of a=%d\n",a);
}

:

a = 22

Cause:

At this time, the EOF value changes

0
source

Source: https://habr.com/ru/post/1782897/


All Articles