<C new> - question about an example in C: The Programming Language

This is a discussion on <C new> - question about an example in C: The Programming Language within the C Programming forums, part of the General Programming Boards category; I'm a total newb to C. I bought C: The Programming Language to start learning, but I have a question ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    <C new> - question about an example in C: The Programming Language

    I'm a total newb to C. I bought C: The Programming Language to start learning, but I have a question about a couple examples, one to count characters, the other to count lines. They're not printing anything, and I'm not sure why. I've double and triple checked everything, making sure I have exactly what's in the book, but to no avail. I'm using Code Blocks 8.02 and Vista.

    Code:
    #include <stdio.h>
    
    /* count characters in input; 1st version */
    main()
    {
        long nc;
        
        nc = 0;
        while (getchar() != EOF)
            ++nc;
        printf("%1d\n", nc);
    }
    The second example, the second version, doesn't work either.

    Code:
    #include <stdio.h>
    
    /* count characters in input; 2nd version */
    main()
    {
        double nc;
        
        for (nc = 0; getchar() != EOF; ++nc)
            ;
        print("%.0f\n", nc);
    }
    The third is the line counting one

    Code:
    #include <stdio.h>
    
    /* count lines in input */
    main()
    {
        int c, n1;
    
        n1 = 0;
        while ((c = getchar()) != EOF)
            if (c == '\n')
                ++n1;
        printf("%d\n", n1);
    }
    Any help is appreciated, of course. Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    They both rely on End-Of-File (EOF) being presented to the application. If you are typing by hand, then you need to end it with CTRL-Z (on windows, it's CTRL-D on Linux/Unix [and probably recent versions of MacOS too]).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    2
    Awesome, that's exactly what I was missing. Thanks a ton. I'll be sure to bring my questions as I learn C to this forum.

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Also you're missing all your curly braces in those for, while and if statements.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    But they are not necessary for one-line code. Good practice or not, I really don't see a problem when people write a single line without the { and }.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Yeah, teaching newbies bad habits is a really good idea...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    That's if you consider it bad practice. I don't
    I digress anyway. It's not like I'll force any ideas upon someone.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I'm in the same boat by saying it's not bad practice

    You have to learn somehow, plus indentation should help you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assembly Language Question
    By John_L in forum Tech Board
    Replies: 2
    Last Post: 03-13-2008, 07:44 PM
  2. Why C Matters
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 136
    Last Post: 01-16-2008, 08:09 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. Language - When To Use What?
    By tjinr in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 08-06-2006, 02:04 AM
  5. What do you prefer??
    By girlzone in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 05-21-2003, 01:03 AM

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