H
Herbert Rosenau
>
It says "we have to do two different things with it (test, and
print)". I think this assertion is closely related to the code they
run and explain in the same page.
However, with '\n', we just need to test it and not to print it ? So
why is a int variable needed ? The question should be how come int
variable c can be both in getchar() and !=EOF ?
What's the point of testing a int variable against '\n' ? '\n' is a
character, isn't it?
To make something that looks complicated really simple and easy:
C i/o is stream based. That means that C itself does nothing know
about real devices like keyboard, screen, printer and so on. C sees
simply a stream of characters! getchar() is a primitive function that
does nothing else than to look into the stream it is assigned too to
get the next not already readed character from that stream. This
stream may or may not buffered - it does not matter as the job of
getchar is simply to get the next not already readed character out of
the stream. When there is at least not a single character left in the
stream then getchar signals this fact by returning an int that is
named EOF. Whereas 'nothing left' means exactly that the stream is
closed (dircetly assigned to a device like a keyboard and the devicce
is closed) or in case that the stream is not directly assigned to a
device but a file that the file is readed completely, noch character
unreaded left.
Another case getchar may return EOF is that something went wrong while
getchar was trying to get the next unreaded character from the stream.
That is commonly named a read error occured. Even as read errors are
rare on current hardware you should be pedantic and ditinguish between
EOF and error by asking ferror(stdin) for the occurence of an error or
really EOF.
So normally you would read char by char until you finds one you are
specially interested in like a TAB ('\t'), a forfeed ('\f'), a newline
('\n') or something else you are searchig for. '\n' is in C a special
case as on some systems '\n' is in real a combination of "\r\n" or
"0x0d0a" or '0x0d' or '0x0a' depending on the OS. The C runtime will
know what character(s) the OS uses to xsignal a newline and convert
that singe char or the multiple char combination into the singe char
'\n' in input and the same char back to the system dependant char or
combination.
So normally you'll read/write simply '\n' and anything is done right
in the background - except you are reading/writing in binary mode. In
binary mode to conversion is made!
--
Tschau/Bye
Herbert
Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!