是否有 EOF != -1 或 WEOF != -1 的常见 C 环境

Are there any common C environments where EOF != -1 or WEOF != -1

C 标准使用以下语言定义 EOFWEOF

7.21.1 Input/output<stdio.h> - Introduction

The header <stdio.h> defines several macros, and declares three types and many functions for performing input and output.

...

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;

...

7.21.1 Extended multibyte and wide character utilities <wchar.h> - Introduction:

The header <wchar.h> defines four macros, and declares four data types, one tag, and many functions.

...

wint_t

which is an integer type unchanged by default argument promotions that can hold any value corresponding to members of the extended character set, as well as at least one value that does not correspond to any member of the extended character set

WEOF

which expands to a constant expression of type wint_t whose value does not correspond to any member of the extended character set.(328) It is accepted (and returned) by several functions in this subclause to indicate end-of-file, that is, no more input from a stream. It is also used as a wide character value that does not correspond to any member of the extended character set.


  1. The value of the macro WEOF may differ from that of EOF and need not be negative.

EOF 是一个负值,它是 getc() 可以 return 的唯一负值。我看到它通常定义为 (-1),类似地 WEOF 定义为 ((wint_t)-1).

是否有任何常见的 C 环境将这些宏定义为不同的东西?

标准委员会保留不同值的可能性,尤其是 WEOF 的非负值的理由是什么?

What is the rationale for the Standard Committee to leave open the possibility of different values and especially a non-negative value for WEOF?

int类型总是有符号的,负值总是包含在范围内,因此EOF宏可以被标准定义为-1。

然而类型wint_t可能是有符号或无符号的1,所以宏WEOF不能被标准定义为特定值。实现必须选择它,因为实现定义了类型 wint_t 及其符号,它还必须为 WEOF.

选择一个值

1(引自:ISO/IEC 9899:201x 7.20.3 其他整数类型的极限5)
如果wint_t(见7.29)定义为有符号整数类型,则WINT_MIN的值不大于-32767,WINT_MAX的值不小于32767;否则,wint_t被定义为无符号整数类型,WINT_MIN的值应为0,WINT_MAX的值应不小于65535。

EOF 的 -1 值允许简单有效地实现 ctype 宏(对于小 char 的常见情况,比如 8 位左右)。典型的实现可能如下所示:

unsigned __ctypes[257] = { 0 /* for EOF */, ... };

#define isalpha(c) (__ctypes[(c)+1] & _ALPHA_BITS)

将 EOF 定义为任何其他整数没有特别的好处,因此 -1 很可能用于任何具有小 char 类型的合理实现。

对于大 wchar_t,table 会太大,因此 wctype 函数的实现可能不同。因此,给 WEOF 任何特定值(包括 -1)的动机就会减少。

在西努OSEOF is defined as -2. See

OTOH wint_t 可以是无符号类型,因此有很多实际实现 WEOF != -1。例如在 MSVC 中 wint_tunsigned shortWEOF(wint_t)(0xFFFF)。从技术上讲,U+FFFF isn't a valid Unicode character 因此它可以用于 WEOF,就像 -1sizeof(char) == sizeof(int) 的实现中用于 EOF 一样。另见

  • Is wint_t always at least as large as wchar_t? And how can unsigned short satisfy reqirements of wint_t?
  • Can sizeof(int) ever be 1 on a hosted implementation?
  • Can an implementation that has sizeof (int) == 1 "fully conform"?