0

I am digging around C++ I/O documentation.

I can find EOF definition in stdio.h of VC++ but can't find it in stdio.h of llvm https://github.com/llvm-mirror/libcxx/blob/master/include/stdio.h

enter image description here


By the way, from https://en.cppreference.com/w/cpp/string/char_traits it says enter image description here

So for char, std::char_traits::eof returns EOF which is -1. Also from https://en.cppreference.com/w/cpp/string/char_traits/eof, it says :

Returns a value not equivalent to any valid value of type char_type.

So here's what I deduce: -1 is not a valid value of type char_type(char)?

How come? In VC++, char is signed, so -1 should be valid for char.

Rick
  • 7,007
  • 2
  • 49
  • 79
  • Hi, please define `valid`, As far as I know, EOF is not a "char" as in a character in a string (like `\0`), it's a return code. – Stefan Sep 18 '18 at 10:18
  • @Stefan Yes, I am also wondering what `valid` means from the doc. It simply says *Returns a value not equivalent to any **valid** value of type char_type.* – Rick Sep 18 '18 at 10:20
  • 2
    Possible duplicate of [What is the ascii value of EOF in c.?](https://stackoverflow.com/questions/7622699/what-is-the-ascii-value-of-eof-in-c). [...]`The actual value of EOF is system defined and not part of the standard.`[...] – user1810087 Sep 18 '18 at 10:22
  • Ah, yes... I miss-read that part a bit. – Stefan Sep 18 '18 at 10:22
  • Nevertheless, I think something is not completely right: -1, 8bit ANSI (depending on code page) is: `ÿ`, see: https://en.wikipedia.org/wiki/Windows-1252 – Stefan Sep 18 '18 at 10:26
  • 1
    @Stefan see https://en.cppreference.com/w/cpp/string/char_traits/to_int_type the integer representation of characters typically uses `unsigned char` so for `char` the integer values for characters are 0-255 with EOF being a negative number (usually -1) – Alan Birtles Sep 18 '18 at 11:05
  • @user1810087 its not a duplicated. I am not asking the value of EOF but want to know the meaning of *valid* from the doc – Rick Sep 18 '18 at 11:07
  • @Rick **possible** duplicate is not a duplicate. EOF is not part of the standard means there is no **the** doc. I've linked the other question because you can get there some decent answers (at least partially) covering your question. E.g: the answer from [R..](https://stackoverflow.com/a/7622741/1810087) [...]`because the character read could have any value in the range of unsigned char`[...] – user1810087 Sep 18 '18 at 11:21
  • Looking at the header file itself (linked in the question), it is easy to see that it `#include`s other files. The definition of `EOF` is probably in one of those other files. Note this is specific to that particular implementation - the standard says nothing about how standard headers achieve their required effects (definitions, etc). – Peter Sep 18 '18 at 11:39
  • @AlanBirtles The link to `to_int_type` is very helpful. Thank you. Now I understand a bit more.So whether `char` is signed or not, and whether if negative value of signed `char` maps to real character , it eventually converts to `unsigned char` which is between `0-255` (then converts to `int`) , so define `EOF` as a negative value can distinguish them. – Rick Sep 18 '18 at 16:08

1 Answers1

1

The file you mention, https://github.com/llvm-mirror/libcxx/blob/master/include/stdio.h , picks up EOF from the C library using this line:

#include_next <stdio.h>

LLVM does not provide a C library, so it will be whatever you have on your system. For example, on a Linux, it will typically be GNU libc, which defines EOF here

/* The value returned by fgetc and similar functions to indicate the
   end of the file.  */
#define EOF (-1)
Cubbi
  • 46,567
  • 13
  • 103
  • 169