<分区>
根据 the C standard ,fgetc
返回的任何字符都以 unsigned char
值的形式返回,“转换为 int
”(引号来自 C标准,说明确实存在转换)。
当 sizeof (int) == 1
时,许多 unsigned char
值超出范围。因此,其中一些 unsigned char
值可能最终被转换为 EOF 的
,尽管文件实际上并未处于错误或文件结束状态,但仍会返回。int
值(转换结果为 "implementation-defined or an implementation-defined signal is raised" )
我很惊讶地发现这样的实现确实存在。 TMS320C55x CCS manual文档 UCHAR_MAX
对应值为 65535,INT_MAX
对应值为 32767,fputs
和 fopen
支持二进制模式...更令人惊讶的是,它似乎将环境描述为完全符合、完整的实现(减去信号)。
The C55x C/C++ compiler fully conforms to the ISO C standard as defined by the ISO specification ...
The compiler tools come with a complete runtime library. All library functions conform to the ISO C library standard. ...
这样的实现是否可以在没有错误的情况下返回一个指示错误的值,真的完全符合要求?这可以证明在循环的条件部分使用 feof
和 ferror
是合理的吗(看起来很可怕)?例如,while ((c = fgetc(stdin)) != EOF || !(feof(stdin) || ferror(stdin))) { ... }