Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: EOF and getchar/fgetc

Reply
Thread Tools

Re: EOF and getchar/fgetc

 
 
Martin Dickopp
Guest
Posts: n/a
 
      02-14-2004
This question seems to be off-topic in comp.std.c, since it is a
question about the C /language/. Answer cross-posted to comp.lang.c,
Followup-To set.


(Mantorok Redgormor) writes:

> unsigned char will always be a byte and getchar is equivalent to getc
> and getc is equivalent to fgetc.


`getchar()' is equivalent to `getc(stdin)'. The difference between
`getc' and `fgetc' is that the former, unlike the latter, may be
implemented as a macro which evaluates its argument (i.e. the stream to
read from) more than once. This only makes a difference if the argument
is an expression with side-effects.

> fgetc gets an unsigned char that is converted to an int.


It reads a character represented as an `unsigned char', and converts it
to `int'.

> so we can say fgetc is a function that reads in a byte at a time.


Yes, `fgetc' reads at most one byte, i.e. it either reads exactly one
byte and returns that (converted to `int'), or it reads zero bytes in
the case of error or end-of-file condition. In the latter case, it
returns `EOF'.

> now, EOF expands to a negative integer of type int and fgetc returns
> EOF on error.


....or on end-of-file condition.

> so if fgetc is reading in characters of type unsigned char then
> converting them to int, this would mean EOF would have to be in the
> range representable by unsigned char.


No, `fgetc' returns `EOF' if it has read /no/ character. `EOF' is not
a character which can be found in a stream, it is used to represent a
/condition/ (error or end-of-file).

Also note that `EOF' is guaranteed to be negative, so it cannot be
represented by an unsigned type.

(Incidentally, on systems where `sizeof(unsigned char) == sizeof(int)',
there could be a character that has the same value as `EOF' when
converted to `int'. In practise, you can often ignore that possibility.)

> Which means that the value of EOF may never be larger than a byte?


That question makes no sense: You cannot compare a /value/ to a
/storage unit/. But, as I said, the value of `EOF' is negative and
can therefore not be represented by `unsigned char'.

Martin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Windows] Any way to distinguish ^C Induced EOF from ^Z EOF? Jan Burse Java 67 03-14-2012 12:21 AM
ifstream eof not reporting eof? SpreadTooThin C++ 10 06-15-2007 08:49 AM
question about filebuf and istreams and eof.... SpreadTooThin C++ 1 06-08-2007 10:19 PM
if EOF = -1, can't a valid character == EOF and cause problems? Kobu C Programming 10 03-04-2005 10:40 PM
I am having a problem in my asp in the recorsets running at iis 6.0 and sql 2000. Sometimes it return Either BOF or EOF is True with no reason. and then start working again Gabriel Mejía ASP General 17 02-22-2005 08:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57