I'm very confused(learning C)

M

m_a_t_t

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!
 
E

Eric Sosman

m_a_t_t said:
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.)
 
J

Jason

m_a_t_t said:
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:

Excellent choice of books, IMHO.
#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?

The first 'a' is yours, and comes from the keyboard. The second 'a' is
printed by the program using putchar. This is exactly what the program
is supposed to do.
And if so then why doesn't it make a
difference if I take out the "!"(not equal to(I think)).

If you take out the '!' (leaving just =, and not ==) then the while
loop will simply save EOF to the char every time you press the
keyboard.

Notice that c is declared as int, and not char. This is because the
operating system sends what is called and End Of File marker (EOF) as
the last piece of data when the 'file' has been completely read. This
marker cannot be a char because it has to be distinguishable from all
possible char data contained in the file. In this case the 'file' is
standard input, and comes from the keyboard. To simulate the EOF
character you need to use some sort of a special character sequence. On
Unix (and Unix type systems, like Linux) this the the CONTROL-D
sequence. On Windows it is CONTROL-Z.

Good luck learning C. [ brings back memories... ahhh. ]

-Jason
 
O

osmium

"Dag-Erling Smørgrav" said:
You do realize that this book is over 15 years old and that the C
standard has been substantially revised since it was written? Try
this instead:

http://www.amazon.com/exec/obidos/ASIN/013089592X
http://www.careferencemanual.com/

What's your point? Yes, there is a new C standard. Is it your guess that
the OP actually *has* one of these curosities? If he doesn't (which I would
guess at 99% certain) don't you think he would be better off with a book
that matches his compiler? IOW, the one he already has?

That is not meant to imply (nor does it), that the book you are touting is
not a fine book, for it's intended audience.
 
P

Peter Shaggy Haywood

Groovy hepcat m_a_t_t was jivin' on 18 Apr 2005 14:48:02 -0700 in
comp.lang.c.
I'm very confused(learning C)'s a cool scene! Dig it!
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();
}

General comment about the state of the World (not directed
specifically to Matt): I'm surprised code with things like "main()"
(as opposed to "int main(void)") and the lack of a return statement
appear in K&R2. But anyhow...
}

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.

The bit that says "copy input to output" should give you some clue.
If that doesn't, then try reading the text above this code (in the
book). It says, "Given getchar and putchar, you can write a surprising
amount of useful code without knowing anything more about input and
output. The simplest example is a program that copies its input to its
output one character at a time." It then gives a pseudocode breakdown
of the algorithm, thus:

read a character
while (character is not end-of-file indicator)
output the character just read
read a character

I don't mean to be mean, but if you can't figure out the purpose of
such an incredibly simple program, even when it is written in plain
English on the same page, then you'll have no hope of being able to
write and debug more sophisticated programs.
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)).

If you mean that you simply removed the "!" character to make this:

while (c = EOF)

then it does make a difference. It makes a big difference, because now
you're assigning the value EOF to c. You now have no way out of the
loop. And you're sending EOF to the output stream. Not good.
If, on the other hand, you mean that you replaced the inequality
operator (!=) with the equality operator (==), then it should print
nothing unless the first getchar() call returned EOF; in which case
writing it may have strange results.
If you mean something else, please clarify.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top