m_a_t_t wrote:
Ok, I'm reading "The C Programming Language: 2nd Edition" and I'm on
chapter 1.5.1 and here's the program you're sposed to make:
#include <stdio.h>
/* copy input to output; 1st version */
main()
{
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}
Ok, now here's what I'm confused about: I read it all and everything
and I'm not sure what it's sposed to do. I tried it and say if I type:
a
Then it'll mimic it, so if I type "a" and press enter it'll do this:
a
a
Is that what it's supposed to do? And if so then why doesn't it make a
difference if I take out the "!"(not equal to(I think)).
Any help is greatly appreciated, thanks!
You're confused because the echo of your input
and the output of the program are showing up mingled
together on the same screen, and it's hard to tell
them apart. Try taking the input from some other
source and/or sending the output to a different
destination. On Unix you'd do something like
myprogram <inputfile >outputfile
to copy the contents of "inputfile" to "outputfile," or
myprogram <inputfile
to copy "inputfile" to the screen (more correctly,
"to the standard output").
You may also encounter trouble in generating the
"end of file" indication from the keyboard; different
systems have different conventions. On most Unixes
you enter the CTRL-D key combination at the start of
a line to say you're through (this is typical, although
it can be changed), and on Windows you use CTRL-Z.
By the way, there are a few infelicities in the code
itself. `main()' should be `int main()' or better yet
`int main(void)', and there should be a `return 0;' just
before the final closing brace. (Ignore any pedants who
tell you that you need one or the other of these but not
both, depending on which version of the C Standard you're
using: such people are right, but it's a bad idea to be
guided by hair-splittin' nit-pickin' Philadelphia lawyers.)
--
Er*********@sun.com