Is EOF always negative?

后端 未结 6 1939
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 17:05

Is EOF always negative?

I\'m thinking of writing a function that reads the next word in the input and returns the line number the word was found in or

6条回答
  •  星月不相逢
    2020-12-11 17:48

    EOF is always == EOF. Don't assume anything else.

    On a second reading of the standard (and as per some other comments here) it seems EOF is always negative - and for the use specified in this question (line number or EOF) it would work. What I meant to warn against (and still do) is assuming characters are positive and EOF is negative.

    Remember that it's possible for a standard conforming C implementation to have negative character values - this is even mentioned in 'The C programming language' (K&R). Printing characters are always positive, but on some architectures (probably all ancient), control characters are negative. The C standard does not specify whether the char type is signed or unsigned, and the only character constant guaranteed to have the same value across platforms, is '\0'.

提交回复
热议问题