What type does eof return?

Both cppreference.com and cplusplus.com say it’s int_type. But they don’t explain (at least I couldn’t find it, an neither could Bing Chat) what type it is. (I suppose it can be any signed integer type at least as wide as char and bool.)

>Solution :

The special value EOF needs to be of integer type that is wider than the corresponding character type, as it cannot be the same value as any character.

In common scenarios, but not all scenarios(!), it is -1.

The traits class template std::char_traits provides a corresponding type for the respective character types: https://en.cppreference.com/w/cpp/string/char_traits

For example, for char it is int. Which is platform-dependent, but at least 16 bits wide.

Leave a Reply