Re: some "newbie" questions

From: The Real OS/2 Guy (os2guy_at_pc-rosenau.de)
Date: 12/22/03


Date: Mon, 22 Dec 2003 12:49:06 +0000 (UTC)

On Sun, 21 Dec 2003 23:03:19 UTC, Buck Rogers <who@cares.com.au>
wrote:

> Hi guys!
>
> I've just bought The C Programming Language, K&R, 2nd Ed.

That's fine - when you own already programming experience with one or
more other languages. K&R is THE book to learn C - but it is not
designed to learn programming.
 
> I am having some trouble with the earlier excercises and some
> questions about the related sample programs. I've hunted through
> the FAQ, with no luck.
>
> ==========================================================
> 1. I've added int before main(), and return 0 to the below
> sample from p.17.
>
> #include <stdio.h>
>
> int main( void )
> {

Fine!

> int c;

Fine. You knows that getchar() returns an int, not a char!

 
> while ((c = getchar()) != EOF)
> putchar(c);
>
> return 0;
> }
>
> Why does this program not end when I enter a blank line?
> It just keeps running until I press Ctr-C. Doesn't a
> blank line signify EOF? Will this program only end if
> Ctr-C is inputed by the user?

That is because EOF is neither a blank line nor Ctrl+c. EOF is
definened as a flag generated from the stream handler behind eg.
getc() for the event that one tries to read behind the end of a file.
To genererate that event from kkeyboard you has to type a special key
freqence:

on some systems that is Ctrl+d, on others that is Ctrl+z, on others
that is something else.

Ctri+c generates an even known as break. This will break the program
immediately independant what the program does.

> excercise 1-6: Verify that the expression getchar() != EOF is 0
> or 1.
>
> excercise 1-7: Write a program to print the value of EOF.
> =============================================================
>
> I've tried c = (getchar() != EOF) and then printing the value
> of c, which comes up with rubbish. I've spent a day on this and
> cannot answer the two above questions - logic is not my strong
> point. Any hints would be appreciated.

printf("%d\n", c);

and then press Ctrl+d or Ctrl+Z depending on your system.
 
>
> ========================================================
> 2. I've added int before main(), and return 0 to the below
> sample from p.18.
>
> int main( void )
> {
> double nc;
>
> for (nc = 0; getchar() != EOF; ++nc)
> ;

That is an endless loop. It eats and throws away anything typed in
until Ctrl+D/Z is typed. The ';' is an empty statement, so as you
doesn't use brackedts to group multiple statements to the for loop the
body of it is empty and the printf gets only reached when getchar()
returns EOF.

As you breaks the program with Ctrl+c the printf gets never reached,
so nothing is printed.

> printf("%.0f\n", nc);
>
> return 0;
> }
> ============================================================
>
> I personally would have used nc++, but the book reckons the
> results would be the same. Anyway, why does the above sample
> not increment the value of nc in any way? All the program
> does is keep on accepting values that I type in ie.

That's right. The bug sits in front of your screen.
 
> 5
> 6
> 7
> ajalal
>
> The program ends when I press Ctr-C.

Works as designed.
 

-- 
Tschau/Bye
Herbert
Visit http://www.ecomstation.de the home of german eComStation