Thread: newbie with an old book

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    5

    newbie with an old book

    I am taking Comp 1200 (introduction to C programming for engineers) my freshman year here at AU. I have the old book called "The C Programming Language" by Kernighan and Ritchie. I have come across a code example that doesn't seem to work right. Here it is:
    #include <stdio.h>

    /*count the characters in input*/
    main()
    {
    double nc;

    for (nc = 0; getchar() != EOF; ++nc)
    ;
    printf("#%.0f\n", nc);
    }

    I thought that this program was supposed to tell me how many characters I input, but it doesn't seem to do anything. Am I missing something here?

  2. #2
    Registered User Tazar's Avatar
    Join Date
    Aug 2001
    Posts
    13
    I fixed the code, it didn't work becose EOF is a file define and you should use '\n' for strings or EOL I used '\n' so it's easy to see the code and how it works =)

    #include <stdio.h>

    /*count the characters in input*/
    main()
    {
    double nc;

    for (nc = 0; getchar() != '\n'; ++nc)
    ;
    printf("#%.0f\n", nc);
    }

    Hope this helps. =)

    Tazar The Demon
    Tazar The Demon
    -------------------------------------
    Please check my site out at: http://www.lrs.8m.com thanks =)
    -------------------------------------
    Note:
    I will help newbies and any other person that I can just ask I'll try to help you out =)

    My MSN Messager name is: [email protected] see if I'm online maybe we can talk. =)

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    5
    Hey, thanks. So EOF only works for files? I guess that makes sense, but why do they use EOF in the book?

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you can simulate EOF from the keyboard on most systems its ctrl and z
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    5
    This is what happens when I run the program with EOF in the code and hit ctrl-z. :

    $ a.exe
    alfdjaslfj
    [1]+ Stopped a.exe

    I am using the cygwin bash shell. Am I doing something wrong?

    Thanks for the help. I really do appreciate it.

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    control-d is how you do it unix, try control-d I'm not sure how far cygwin
    has gone to simulate unix.

  7. #7
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Yea exact same behavior with linux and bash.
    Control-z suppends the program so you can restore
    it with % or fg.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    For future referrence, it may be a good idea to use a while loop instead of the for that you use in your code. By the way, the book may be old but it is by far the best c resource that I own.

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    5

    Thumbs up

    Control-D does work. I should have known since that is what I use all the time to log out. I didn't say it wasn't a good book; I just said that it was old. It's not the book we are actually using for the class. It was recommended to me, and I thought it might help. I have enjoyed it so far. Thanks a lot for all of your help.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > "The C Programming Language" by Kernighan and Ritchie.
    I would suggest you get the slightless less old 2nd edition with a big ANSI-C stamped on the front cover.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    5
    I do have the second edition. I'm sorry for not clarifying that, but it is still old (13 years).
    I'd like to thank you all again, and I am sure that I will post here later on. Hopefully, I will learn enough quickly so that I will be able to help others as well.

  12. #12
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    if you don't have the ANSI-C version, then you're not gonna wind up helpin anyone (no offense), because the language has evolved so much since then.

  13. #13
    Registered User Tazar's Avatar
    Join Date
    Aug 2001
    Posts
    13
    Keep at it C++ is one of the most powerfull and most used languages of all time so learn and most of all have fun =) if you need help just ask on this board and somebody will help you this board rocks becose of the great people that use it and help others out will they need it hope to see you here more =)

    Tazar The Demon
    Tazar The Demon
    -------------------------------------
    Please check my site out at: http://www.lrs.8m.com thanks =)
    -------------------------------------
    Note:
    I will help newbies and any other person that I can just ask I'll try to help you out =)

    My MSN Messager name is: [email protected] see if I'm online maybe we can talk. =)

  14. #14
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    but he wants to learn C, not C++ - so there! hehehe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Book for Newbie trying to learn C
    By uthscsa19 in forum C Programming
    Replies: 23
    Last Post: 12-24-2005, 11:02 AM
  2. newbie | k&r book
    By xion in forum C Programming
    Replies: 4
    Last Post: 07-17-2003, 03:10 AM
  3. Newbie trying to work w/ KR book!?!
    By Slimcracker in forum C Programming
    Replies: 3
    Last Post: 05-01-2003, 02:04 PM
  4. Newbie - MFC code from a book in VC++.Net
    By Guardian in forum Windows Programming
    Replies: 2
    Last Post: 04-27-2002, 07:17 PM