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


Relevant Pages

  • Re: cannot understand the character handling Program
    ... program fron "The C Programming Language" by Dennis Ritchie, I tried to run the following program.I am using Dev++ as a compiler software. ... able to reach the EOF as specified in the While statement even after ... Whatever the input we are typing in is appearing in the window. ... Also, for C99 compliance, the 'implicit int' rule is no longer ...
    (comp.std.c)
  • Re: scanf behaviour
    ... char, i have to reread the input until I get the needed pos. ... user input a number that's too large to be stored in an integer. ... static int ignoreblks ... which may be \n or EOF ...
    (comp.lang.c)
  • Re: How to check for errors when inputting a number
    ... int read_num{ ... detect the character that terminated a numerical field. ... which may be \n or EOF ... Skipblks returns the char that getc will next return, ...
    (comp.lang.c)
  • Re: How to check for errors when inputting a number
    ... The numerical input routines *NEVER* absorb a terminating ... readxint a further getc will return the character ... static int ignoreblks ... which may be \n or EOF ...
    (comp.lang.c)
  • Re: String to integer
    ... detect the character that terminated a numerical field. ... Skipblks returns the char that getc will next return, or EOF. ... int skipwhite; ...
    (comp.lang.c)