k & r question

This is a discussion on k & r question within the C Programming forums, part of the General Programming Boards category; Hello, I recently purchased K & R's book on C. I have a couple of questions. Here is the code ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    6

    k & r question

    Hello, I recently purchased K & R's book on C. I have a couple of questions. Here is the code that I have questions about:

    Code:
    #include <stdio.h>
    
    /* count characters in input; 2nd version */
    main()
    {
          double nc;
    
          for (nc = 0; getchar() != EOF; ++nc)
               ;
          printf("%.0f\n", nc);
    }
    my question is, what is this supposed to do? I mean I know it is supposed to count the input, but when I run this program if I type something in, what will it do? I get nothing and wasn't sure if I had missed something or not. I have read this part numerous times and just can't wrap my head around it. I know it is something simple and I am probably brain dead, but can anyone explain this?

    I understand what the code does, I just don't know the result...

    Thanks for any help,

    /asenchi

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    This code will count the number of entered characters, untill receiving EOF.

    Depending on your OS, you can send EOF by using ctrl+d in UNIX and ctrl+z in windows.

    HTH,
    $ENV: FreeBSD, gcc, emacs

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    6

    UNIX

    Thanks a lot, that helped a lot.

    /asenchi

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    I 've heard too much about K & R book - the book written by the creators of C, that it is very good, perfect and blah..blah.

    However this program says the opposit ( note: i don't judge a whole book by only one program, but at least the basicest things should be right ).
    Loading.....
    ( Trying to be a good C Programmer )

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    6

    curious

    How would you write this, if I catch your implication?

    /asenchi

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Well, to give a right answer to your question, i should know the purpose of writing this program.
    For example i don't see any reason for nc to be of type double. Even if they wanted to intoduce type double, they shouldn't have written this example to do it.
    But i think it was written to show how the charater '\n' influences a program.

    However, this isn't the real reason for telling that. basicaly i 'm talking about function main, which is just:
    main()
    without telling what it will return or anything.

    I prefer a thousand times function main to be like:
    void main()
    instead of that - although this is also "wrong", and even better, to have at the end of the function the statement exit(0);
    Loading.....
    ( Trying to be a good C Programmer )

  7. #7
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    I didn't know you could increment a double like that...

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    6
    This program was an example of character counting. It is the 2nd version of a program that used a while statement rather than the for statement you see here.

    I have read a little on the different ways to write function main(). I will do some more reading on that. Also you should know that this is only the first Chapter which is a tutorial to kind of show some aspects of C.

    Thanks

    /asenchi

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    > This program was an example of character counting.

    Well.... i don't really like it... If the user wants to see the number of characters he entered, he now sees the double number of the characters he entered. It's just my opinion...

    >Also you should know that this is only the first Chapter which is a tutorial to kind of show some aspects of C.
    Personally, i believe that the books and the teachers should use int main() in their first lesson - first programm. I don't say that they have to explain about the different kinds of main to the students, but just use int main() in every example - program.

    P.S: Do you find that book good ?( does it explain good what it writes etc ) ?
    Loading.....
    ( Trying to be a good C Programmer )

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>he now sees the double number of the characters he entered. <<
    What are you talking about??
    Using that program, entering the word "four" followed by EOF gives the output 4, just as expected. What is "double" about that? The only reason I see to use a double is to allow for a greater number of characters than other variables can hold, but personally I think an unsigned long int would have better (or a size_t variable).

    >>main()
    This is a conversation that is held all too often, and is discussed in the FAQ.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    6

    somethings...

    I just bought it 2 days ago, but so far it is pretty good. There are somethings that I don't understand, they really don't lay it out for you. It does take some research sometimes (reason for finding this board).

    I do like how it is cut and dry though. I would rather have to figure some things out myself, however this was recommended as _THE_ C book, and for a beginner (which I am, in all programming) people might want to look somewhere else. I eyed the C Primer (can't find a link right now), that looks good too.

    So you are saying everytime that someone uses the function main() they should express it as:

    Code:
    int main()
    {
    ...
    }
    thanks for your help,

    /asenchi

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Hammer,
    >Using that program, entering the word "four" followed by EOF gives the output 4, just as expected.
    OK....... but what happens if the user enters some characketers like:
    R
    A
    N
    D
    O
    M
    ? Then it won't print the result the user ecpected(6), but 12.

    >What is "double" about that?
    Absolutely nothing. It's one of the worst examples to use double. They could just use "long".

    asenchi,
    I would also like to find that book and read it, just to have a taste of it.

    >So you are saying everytime that someone uses the function main() they should express it as:
    Like most people say here, it should be expressed as
    Code:
    int main( /*void*/ )
    {
    .....
    return 0;
    }
    ( you forgot the return statement )
    Loading.....
    ( Trying to be a good C Programmer )

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>but what happens if the user enters some characketers like
    >>Then it won't print the result the user ecpected(6), but 12.
    But 12 is the expected result. A new line is a valid character, just like any other white space character (space, tab)

    >>It's one of the worst examples to use double. They could just use "long".
    Just because you see a different way of doing things doesn't necessarily mean the original way is wrong. Sure, offer alternatives, but this small example is hardly the "worst example".

    >>>So you are saying everytime that someone uses the function main() they should express it as
    Why not just read the FAQ link I already posted? It answers your question correctly.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    >But 12 is the expected result. A new line is a valid character, just like any other white space character (space, tab)
    Ok, i know that a new line is a valid character. But i think that the user would expect to see the number of characters he entered without the newline character. But ok, it's just how i view it.

    >Just because you see a different way of doing things doesn't necessarily mean the original way is wrong.
    E.... i said that it would be the worst if with this example they would intoduce the type double.
    Now, it is just... awful. But, ok, this is the way i see it, you don't have to agree.
    Loading.....
    ( Trying to be a good C Programmer )

  15. #15
    Registered User
    Join Date
    Jul 2003
    Posts
    6

    i know

    I did visit that link in the FAQ. However, I was wondering about money?'s way of doing it. And as you can see he did it just a bit different... I didn't see any examples of putting void in a /* */ in the FAQ... I am new, just gathering info...

    /asenchi

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21