1

Up to now, i have found out that, we use getchar() as int because at EOF this function returns -1. I was wondering can't char hold -1 ? i think it can,because it can also range from -128 to 127.

I searched through the top list of google, but the answer i got didn't satisfy me.

  • 2
    So what happens when someone actually types the character whose code is `-1` – M.M Sep 06 '16 at 05:29
  • just prints -1, i tried that too.. I think when getChar() returns -1, it's different then just typing -1 through the keyboard! @M.M – Hilton Khadka Sep 06 '16 at 05:31
  • 2
    I said the character whose code is `-1`, not the characters `-` and `1` – M.M Sep 06 '16 at 05:32
  • Oh! my bad.. I can't find a character that represents -1! So is that what you are trying to convey? that there is no representation of -1 when we use char as type? @M.M – Hilton Khadka Sep 06 '16 at 05:36
  • 1
    There might be one, and if there is, then you can't tell whether they typed that one, or EOF happened. Some systems give `ÿ` the code `-1` – M.M Sep 06 '16 at 05:38
  • Ok, i'm getting something now, Thank you. – Hilton Khadka Sep 06 '16 at 05:39

3 Answers3

7

First of all, just to clear something up, EOF is not required to be specifically -1. The ISO C standard requires it to have a negative value.

Also note that the type char can be signed or unsigned; that is implementation-defined.

The function fgetc, getc and getchar can be used to process binary streams, not only text streams. Bytes have values from 0 to UCHAR_MAX. The natural type for this range is unsigned char. The return value of getc couldn't be unsigned char, because EOF wouldn't be representable. It couldn't be signed char either, because signed char cannot hold the values from 0 to UCHAR_MAX. So, a wider type is chosen: the type int. On mainstream platforms, int is wider than char: it has a size of at least 2. And so it it is capable of representing some negative value that can be used for EOF, and all the byte values in the range 0 to UCHAR_MAX.

In some C implementations (for systems such as some DSP chips) this is not true: int is one byte wide. That represents challenges. On such a C implementation, a range of valid byte values returned by getc just has to be negative, and one of those values clashes with EOF. Carefully written code can tell that this is the case: a value equal to EOF was returned, yet the feof and ferror functions report false: the stream is not in error, and end-of-file has not occurred. Thus, the value which looks like EOF is actually a valid byte.

Kaz
  • 55,781
  • 9
  • 100
  • 149
4

getchar() and family return an integer so that the EOF -1 is distinguishable from (char)-1 or (unsigned char)255.

J Earls
  • 1,792
  • 8
  • 12
  • i'm a bit confused right now, i tried to search for (char) of -1 but i can't find it? i just see positive numbers on the table! @ J Earls – Hilton Khadka Sep 06 '16 at 05:44
  • If by "table" you mean the US ASCII chart or something, yes, those will all be positive numbers. However, the C `char` type is an 8-bit **signed** integer type. It represents numbers from 0 to 127 and from -128 to -1 rather than from 0 to 127 and 128 to 255. – J Earls Sep 06 '16 at 05:53
0

Chars, using Ascii encoding, is stored using a byte, which is unsigned 8-bit integer. Thus chars can take the numeric value 0-255.

getchar returns a signed int to allow for -1, which is the magic number for end-of-file (EOF)

Jonas
  • 737
  • 1
  • 8
  • 20