Is EOF always negative?

后端 未结 6 1925
被撕碎了的回忆
被撕碎了的回忆 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:25

    From wikipedia :

    The actual value of EOF is a system-dependent negative number, commonly -1, which is guaranteed to be unequal to any valid character code.

    But no references ...

    From Secure Coding : Detect and handle input and output errors EOF is negative but only when sizeof(int) > sizeof(char).

    0 讨论(0)
  • 2020-12-11 17:28

    EOF is a condition, rather than a value. The exact value of this sentinel is implementation defined. In a lot of cases, it is a negative number.

    0 讨论(0)
  • 2020-12-11 17:36

    From the online draft n1256, 17.9.1.3:

    EOF

    which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream;

    EOF is always negative, though it may not always be -1.

    For issues like this, I prefer separating error conditions from data by returning an error code (SUCCESS, END_OF_FILE, READ_ERROR, etc.) as the function's return value, and then writing the data of interest to separate parameters, such as

    int getNextWord (FILE *stream, char *buffer, size_t bufferSize, int *lineNumber)
    {
      if (!fgets(buffer, bufferSize, stream))
      {
        if (feof(stream)) return END_OF_FILE; else return READ_ERROR;
      }
      else
      {
        // figure out the line number
        *lineNumber = ...;
      }
      return SUCCESS;
    }      
    
    0 讨论(0)
  • 2020-12-11 17:42

    Have that function return

    • the line number the word was found in
    • or -1 in case the end of the input has been reached

    Problem solved, without a need for relying on any EOF values. The caller can easily test for greater-or-equal-to-zero for a successful call, and assume EOF/IO-error otherwise.

    0 讨论(0)
  • 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'.

    0 讨论(0)
  • 2020-12-11 17:50

    Yes, EOF is always negative.

    The Standard says:

    7.19 Input/output
    7.19.1 Introduction

    3 The macros are [...] EOF which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream;

    Note that there's no problem with "plain" char being signed. The <stdio.h> functions which deal with chars, specifically cast the characters to unsigned char and then to int, so that all valid characters have a positive value. For example:

    int fgetc(FILE *stream)
    

    7.19.7.1
    ... the fgetc function obtains that character as an unsigned char converted to an int ...

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