Re: I'm very confused(learning C)
- From: phaywood@xxxxxxxxxxxxxxxxxxxxxxxx (Peter "Shaggy" Haywood)
- Date: Sat, 23 Apr 2005 02:16:52 GMT
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"?
.
- References:
- I'm very confused(learning C)
- From: m_a_t_t
- I'm very confused(learning C)
- Prev by Date: Re: change users shell programmatically
- Next by Date: Re: Add pointer to a double linked-list?
- Previous by thread: Re: I'm very confused(learning C)
- Next by thread: About casts (and pointers)
- Index(es):
Relevant Pages
|
|