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

Standard C defines EOF and WEOF with the following language:

7.21.1 I / O - Introduction

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

...

EOF

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

...

7.21.1 Advanced Multibyte and Wide Format Utilities - Introduction:

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

...

wint_t

which is an integer type, unchanged by default, for advancement by default, which can contain any value corresponding to members of the extended character set, as well as at least one value that does not match any member of the extended character set

WEOF

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


328) The value of the WEOF macro may differ from the value of EOF and must not be negative.

EOF is a negative value, and this is the only negative value that getc() can return. I saw that it is usually defined as (-1) , and similarly WEOF is defined as ((wint_t)-1) .

Are there any common C environments where any of these macros are defined by something else?

What is the point of the standard committee to leave the possibility of different values ​​and especially non-negative values ​​for WEOF ?

+2
source share
2 answers

What is the rationale for the Standard Committee to leave the opportunity to distinguish values ​​and especially non-negative values ​​for WEOF?

The int type is always signed, a negative value is always included in the range, so the EOF macro can be defined by the standard as -1.

However, the wint_t type can be signed with or without unsigned 1; therefore, the WEOF macro cannot be defined by the standard as a specific value. The implementation must choose it. Since the implementation defines the type wint_t and its signature, it must also select a value for WEOF.


1 (Quoted from: ISO / IEC 9899: 201x 7.20.3 Limits of other integer types 5)
If wint_t (see 7.29) is defined as a signed integer type, the value of WINT_MIN must be no more than -32767, and the value WINT_MAX must be at least 32767; otherwise, wint_t is defined as an unsigned integer type, and the value of WINT_MIN must be 0, and the value of WINT_MAX must be at least 65535.

+2
source

A value of -1 for EOF allows a simple efficient implementation of ctype macros (for the usual case with a small char , say, 8 bits or so). A typical implementation might look like this:

 unsigned __ctypes[257] = { 0 /* for EOF */, ... }; #define isalpha(c) (__ctypes[(c)+1] & _ALPHA_BITS) 

There is no particular advantage in defining EOF as any other integer, so -1 is likely to be used in any reasonable implementation with a small char type.

With large wchar_t table will be too large, so wctype functions are likely to be implemented in different ways. Consequently, there is less incentive to provide WEOF with any particular value, including -1. A.

+3
source

Source: https://habr.com/ru/post/1275132/


All Articles