gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:16:02 27 4
gpt4 key购买 nike

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() 唯一的负值可以返回。我看到它通常定义为 (-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 Limits of other integer types 5)
如果wint_t(见7.29)被定义为有符号整数类型,WINT_MIN的值应不大于−32767,WINT_MAX的值> 不得小于32767;否则,wint_t定义为无符号整数类型,WINT_MIN的值应为0,WINT_MAX的值应不小于65535.

关于c - 是否有 EOF != -1 或 WEOF != -1 的常见 C 环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40717563/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com