Thread: the code from my book doesn't work!!

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    18

    the code from my book doesn't work!!

    hi I'm reading a book called the c programming language second edition by dennis ritchie and one of the source codes in section 1.5.4 doesn't work properly.

    here's the code:
    Code:
    #include <stdio.h>
    
    #define IN 1    /* inside a word */
    #define OUT 0   /* outside a word */
    
    /* count lines, words, and characters in input */
    main()
    {
        int c, nl, nw, nc ,state;
    
        state = OUT;
        nl = nw = nc = 0;
        while ((c = getchar()) != EOF) {
            ++nc;
            if (c == '\n')
                ++nl;
            if (c == ' ' || c == '\n' || c == '\t')
                state = OUT;
            else if (state = OUT) {
                state = IN;
                ++nw;
            }
        }
        printf("%d %d %d\n", nl, nw, nc);
    }
    nc increments correctly but also reads '\n' as character too.
    also the main problem is that nw never increases from zero no matter how many words i type. and I'm just beginner i don't know how to debug the code.

    ps: I've tried the code on two operating systems both had this problem (ubuntu with gcc and windows with mingw)

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You probably copied it wrong. Look again at line #19, for example. "=" is assignment, "==" is comparison.
    Devoted my life to programming...

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by GReaper View Post
    You probably copied it wrong. Look again at line #19, for example. "=" is assignment, "==" is comparison.
    @OP, you would have caught this problem if you had your compiler warnings turned on. For example, with gcc you could have done:

    Code:
    gcc -Wall -o someprog someprog.c
    Without -Wall:

    Code:
    ~/cprogs/cboard $ gcc -o count count.c
    ~/cprogs/cboard $
    With -Wall:

    Code:
    ~/cprogs/cboard $ gcc -Wall -o count count.c
    count.c: In function ‘main’:
    count.c:19:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
             else if (state = OUT) {
             ^
    /cprogs/cboard $
    For my own programs, I also use the -Wextra option as well. You can see what these two options do by looking at the gcc man page on your system.

  4. #4
    Registered User
    Join Date
    Nov 2016
    Posts
    18
    i changed
    Code:
    else if (state = OUT)
    to
    Code:
    else if (state == OUT)
    and it now it works thanks for the help and your suggestions

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    -W is the same as -Wextra, and there's also -pedantic. So full warnings are:
    gcc -Wall -W -pedantic ...
    You could make an alias:
    alias wcc='gcc -std=c99 -Wall -W -pedantic' # choose whatever standard you wish
    Then compile like
    wcc -o myprog myprog.c

    To add that alias to the end of your .bashrc:
    echo "alias wcc='gcc -std=c99 -Wall -W -pedantic'" >> ~/.bashrc

    And don't be fooled by the word "pedantic". That's just their (Richard Stallman's?) opinion.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by algorism View Post
    And don't be fooled by the word "pedantic". That's just their (Richard Stallman's?) opinion.
    No. Pedantic means to warn you about things that might violate the Standard. For example -pedantic should warn if you use // ... style comments when compiling with C89 mode.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by c99tutorial View Post
    No. Pedantic means to warn you about things that might violate the Standard. For example -pedantic should warn if you use // ... style comments when compiling with C89 mode.
    I obviously wasn't saying that the warnings it gives are just some dude's opinion, since that would be idiotic. I was saying that calling the warnings that it gives "pedantic" is just some dude's opinion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My code doesn't work, help please
    By BenBusby in forum C Programming
    Replies: 3
    Last Post: 01-24-2013, 06:14 AM
  2. Example from book - time(NULL) doesn't work
    By FernandoBasso in forum C Programming
    Replies: 6
    Last Post: 10-30-2011, 05:59 PM
  3. Example Code doesn't work!
    By Kayl669 in forum C Programming
    Replies: 8
    Last Post: 02-18-2010, 10:19 AM
  4. Replies: 3
    Last Post: 03-07-2003, 09:06 PM

Tags for this Thread