Thread: getchar() problem from K&R book

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    getchar() problem from K&R book

    hi, im very new to C programming so this will probably look obvious to you. I Searched for this problem but couldnt find the same one although i did find one about getchar() but it looked more complicated than the bit of code im learning so i dont think it wouldve helped.
    Anyway ive just got the 2 books i ordered from Amazon called "The C Programming Language second Edition" and "The C Answer Book Second Edition" which provides solutions for the exercises provided in the first one i mentioned.

    On page 18 this is the code, yet it repeats getchar() every time the loop starts again, Therefore the printed code is never reached. Does anybody know how to fix this? It says all the bits of code were compiled and tested but why did they miss this bit? Its meant to count the characters on input and output the number of characters used. I have checked for mistypes at least twenty times over, this is the code taken from the source:

    Code:
    #include <stdio.h>
    
    main()
    {
    	long nc;
    
        nc = 0;
        while (getchar() != EOF)
              ++nc;
        printf("%1d\n", nc);
    }
    ive also tried it like this:

    Code:
    #include <stdio.h>
    
    main()
    {
    	long nc;
    
        nc = 0;
        while (getchar() != EOF)
    {
              ++nc;
    } 
       printf("%1d\n", nc);
    }
    i have also tried the for loop version and that, as expected had no effect either.

    i also once tried changing little bits like this at any stupid attempt to fix it even if i expect it not to work:
    Code:
    long nc, c;
    
    while ((c = getchar()) != EOF)
    Before on an internet tutorial i was learning the input technique scanf, but now im also learning the way this book uses so i can continue with this book understanding everything.

    help is greatly appreciated, thanku.

    NOTE: I am very new to C so plz try not to use complicated C terms that i would not understand, thnx.

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    The while loop will continue until getchar() returns EOF. On UNIX platforms (the examples in K&R were tested on UNIX), EOF is usually generated by pressing Control-D.

    There is nothing wrong with the code example.
    Jason Deckard

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    How are you testing it?

    If you are using Windows:

    From a command line, enter:

    progname

    (Assuming your program is progname.exe, for example)

    type anything you want, then, when you are through, type

    ctrl-z

    (this returns EOF to getchar())

    Try redirecting input:

    from command line, enter:

    progname < progname.c

    It works for me


    Dave
    Last edited by Dave Evans; 03-22-2004 at 08:43 AM.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    k

    thnx for the replys, yes the Ctrl-z does work for me, thnx. Only one small problem tho, it counts the ^Z aswell as a character, oh well i s'pose there is probably a more efficient way to make this program. i can't think of what tho.

    Btw im a reall n00b with command lines, i have windows so is it DOS command prompt? is it also with DEV compiler under: tools/DOS shell? I tried the DOS shell then typing the path of the exe without the .exe extension but it didnt seem to work.

    anway i kno with some programs it has command line under properties of the file but my exe doesnt. Can someone give me some instructions on how to find and use my command line plz, thnx. my msn is daryl4321(at)msn.com if anybody would be kind enough to help via msn. take away the (at) bit and replace it with @. wow thnx for responding so quick after my post. It must've been only seconds after making this thread, well not quite seconds but not longer than 15 minutes ;-).

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536

    Re: k

    Originally posted by anemicrose

    Btw im a reall n00b with command lines, i have windows so is it DOS command prompt? is it also with DEV compiler under: tools/DOS shell? I tried the DOS shell then typing the path of the exe without the .exe extension but it didnt seem to work.
    (I don't think it's counting your ctrl-z. It is, however counting the newline that results when you press "Enter" at the end of a text line.)

    I always put a shortcut on my start menu, so I can't remember exactly where it is in a default Windows installation.

    I think you can do the following
    From your start menu, navigate to programs->accessories. You should see an icon named "Command prompt" (Windows XP) or something similar.

    Right-click the icon, and drag it to your desktop. Then click "copy here".

    Now when you double-click the icon, you will have a command prompt.

    Using "cd" you can navigate to the directory where your Dev-cpp project was created.
    For example:

    cd \Dev-Cpp\myprojects\projname

    Then you should find the projname.exe file by doing a

    dir

    command at the command line prompt.

    This is very un-windowsy behavior (and is why windows got so popular --- just point-and-click, no typing, no assembly required).

    Dave

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    k

    k thanku, yeh, that brings back memories, my brother used to be a dos programmer but that was on windows 3.1 where the dos screen took up the whole screen.

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    For a Windows command prompt you want cmd.exe
    For a DOS command prompt you want command.com

    cmd.exe is better
    Last edited by major_blagger; 03-22-2004 at 12:53 PM.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Originally posted by major_blagger
    For a Windows command prompt you want cmd.exe
    For a DOS command prompt you want command.com

    cmd.exe is better
    cmd.exe is what you get when you navigate to start->programs->accessories and see the "command prompt" icon
    (Windows XP).

    Dave

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    yes

    thats what i noticed aswell when i typed it (cmd.exe) in run, it opened command prompt.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Can't find EOF in OS X

    I'm going through K & R myself -- I reached this thread through a search, but I still can't seem to identify EOF in the K & R code listing prior to the one the original poster cited:

    Code:
    #include <stdio.h>
    
    /* copy input to output, first version */
    main()
    {
        int c;
    
        c = getchar();
        while (c != EOF) {
            putchar(c);
            c = getchar();
        }
    }
    While I can break the while() loop with Ctrl-c, I can never seem to create a function that will print the value of EOF. Based on the above posts I would assume that Ctrl-c is, in fact, the value of EOF; and I would ignore the issue if the next exercises weren't (1-6) Verify that the expression getchar() != EOF is 0 or 1, and (1-7) Write a program that will print the value of EOF. What am I missing?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I still can't seem to identify EOF in the K & R code listing prior to the one the original poster cited
    That could very well be because EOF is first introduced in that section (1.5.1, pp. 16).

    >While I can break the while() loop with Ctrl-c
    You're more likely terminating the program than breaking from the loop.

    >I can never seem to create a function that will print the value of EOF
    Code:
    #include <stdio.h>
    
    void gimmeEOF ( void )
    {
      printf ( "%d\n", EOF );
    }
    >Based on the above posts I would assume that Ctrl-c is, in fact, the value of EOF
    Possibly, but EOF is more often given by the meta-command Ctrl-d or Ctrl-z.

    >(1-6) Verify that the expression getchar() != EOF is 0 or 1
    The expression is guaranteeed to be 0 or 1 by the language definition. Boolean expressions using the relational operators will always return either 0 or 1.

    >Write a program that will print the value of EOF
    See above. EOF is simply a macro hiding an integral value that is not any valid char.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    if that's the case, write a program that does this:
    if(temp == EOF)
    printf("%c", temp);

    and then start hitting that keyboard of yours with as many ctrl combinations until you hit the EOF...

    alternatively, printf("%d %c", EOF,EOF);
    should give you both the numerical value of EOF as well as what it really looks like when printed out (although IIRC, EOF prints out an empty space or something like that)

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if(temp == EOF)
    printf("%c", temp);
    Except that again, you can't use a char to hold EOF.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    2
    I riffed on tzuchan's code fragment like this:

    Code:
    #include <stdio.h>
    
    main()
    {
        int i;
        int temp;
    
        temp = getchar();
        while (temp != EOF) {
               ;
        }
    
        if (temp == EOF) {
             printf("\nThe number value of EOF is %d, represented by %c\n", EOF, EOF);
             printf(("The number value of -1 if %d, represented by %c\n", -1, -1);
        }
    }
    Both lines return the integer "-1" and the character "?" on my system (OS X) respectively. The former value comports with K&R, pp. 151-152:
    The symbolic constant of EOF is defined in <stdio.h>. The value is typically -1, but test should be written in terms of EOF so as to be independent of the specific value.
    So much for that.

    Prelude wrote:
    The expression [getchar() != EOF is 0 or 1] is guaranteeed to be 0 or 1 by the language definition. Boolean expressions using the relational operators will always return either 0 or 1.


    Thanks. I was too fixated on the value of EOF to see that what was being asked for was the truth value of the expression. Time to take a walk . . .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 10:45 AM
  2. Help with K&R Book Exercise
    By Alejandrito in forum C Programming
    Replies: 5
    Last Post: 03-11-2008, 01:24 PM
  3. URGENT: Help wanted...in C
    By iamjimjohn in forum C Programming
    Replies: 16
    Last Post: 05-18-2007, 05:46 AM
  4. ascii backspace, K&R book exercises
    By jjohhn in forum C Programming
    Replies: 10
    Last Post: 12-29-2005, 08:33 AM
  5. getchar() problem.
    By caroundw5h in forum C Programming
    Replies: 11
    Last Post: 06-17-2004, 07:10 PM