#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Australia
    Posts
    9
    Rep Power
    0

    old code troubles


    The book I am using - "The C Programming Language (second edition)" was written in 1988 so I am presuming that reason this isnt working is because it is so old =\ -
    Code:
    #include <stdio.h>
    
       /* count characters in input; 2nd version */
       main()
       {
           double nc;
    
           for( nc = 0; getchar() != EOF; ++nc )
                   ;
           printf("%.0f\n", nc);
       }
    or this one:
    Code:
    #include <stdio.h>
    
       /* count characters in input; 1st version */
       main()
       {
           long nc;
    
           nc = 0;
           while (getchar() != EOF)
               ++nc;
           printf("%ld\n", nc);
       }
    any help into why this isnt working - so that I can take not of it for other code in this book - would be greatly appriected =)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    What do you mean "isnīt working"?

    If you run the compiled program and it just does not end waiting for input, you have to press "CTRL-D" (CTRL-Z for Win/Dos). this is the EOF getchar() it is waiting for...

    The book you refer to, is this the "Kernighan & Ritchie"? then it IS valid since they invented the C language. It should say "ANSI C" on the title too for the second (and up to date) edition.

    I learned C with this book and i can tell you, it is quite good! (Mine is from 1988 too)

    I guess your compiler is not compatible. Which one do you use? And which OS do you write your code on?

    A lot of stuff does not work eg. for MS-DOS, since DOS is crap... Same for Win9X-ME.

    Use Linux and GCC. IMHO Itīs your best choice for learning C.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Australia
    Posts
    9
    Rep Power
    0
    Yeah the book is the K&R one and looks great so far and im using Dev-C++4 on Win2k - oh and ctrl+Z works thanks =)

IMN logo majestic logo threadwatch logo seochat tools logo