Thread: K&R symbolic constants, beginner example

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    112

    K&R symbolic constants, beginner example

    Hey all..

    I've been toying with this code for the past several days. I am working through all the exercises (or with a goal of completing) K&R ANSI C. Upon compiling this code and running, after typing in "apple red orange" I would like each character count for each word printed out on a new line. Currently the program is broken, and i've had it working where the first two words were outputting the correct char count, but not the digits weren't properly outputting.
    Code:
    /* Execise 1-13 */
    
    #include <stdio.h>
    
    #define	IN	1
    #define OUT	0
    
    int main(void)
    {
    	int c, state;
    	long nc = 0;
    	c = 0;
    	
    	int y = 0;
    	
    	state = OUT;
    	while ((c = getchar()) != EOF)
    	{
    		++nc;
    		if (c == ' ')
    		{
    			if(state == IN)
    			{
    				printf("\n");
    				state = OUT;
    			}
    		} 
    
    		else if (state == OUT)
    		{
    			state = IN;
    			printf("%ld", nc);
    		}
    
    		else
    		{
    			printf("%ld", nc);
    			++nc;
    		}
    	}
    }

  2. #2
    Registered User
    Join Date
    Dec 2018
    Posts
    38
    If I understand what you are trying to do, wouldn't it be more like this:
    Code:
    #include <stdio.h>
    #include <ctype.h>
     
    int main()
    {
        enum {OUT, IN};
     
        long nc = 0;
        int c = 0;
        int state = OUT;
     
        while ((c = getchar()) != EOF)
        {
            if (isspace(c))
            {
                if(state == IN)
                {
                    printf("%ld\n", nc);
                    state = OUT;
                    nc = 0;
                }
            } 
            else
            {
                state = IN;
                ++nc;
            }
        }
    }
    Or even more simply:
    Code:
    #include <stdio.h>
    #include <ctype.h>
     
    int main()
    {
        long nc = 0;
        int c = 0;
     
        while ((c = getchar()) != EOF)
        {
            if (isspace(c))
            {
                if (nc > 0)
                {
                    printf("%ld\n", nc);
                    nc = 0;
                }
            } 
            else
                ++nc;
        }
    }

  3. #3
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    103
    The K&R program run correct, almost . . . no termination condition. The gentlemen K&R don't like this.


    Your code: For what "y"? In line 12: int c = 0; And "return(0);" is missing.

    Code:
    //Zeilen, Wörter und Zeichen zählen, K&R S. 20 (34)
    
    
    #include <stdio.h>
    
    
    #define IN 1
    #define OUT 0
    
    
    int main(void)
    {
        int c, state;
        int new_line, new_word, char_number;
        
        state = OUT;
        new_line = new_word = char_number = 0;
        do 
        {
            c = getchar();
            ++char_number;
            if (c == '\n')
                { ++new_line; }
            if (c == ' ' || c == '\n' || c == '\t')
                { state = OUT; }
            else if (state == OUT)
            {
                state = IN;
                ++new_word;
            }
        }while (c != '0');
        printf("\n%3d %3d %3d\n\n", new_line, new_word - 1, char_number);
        
        return(0);
    }
    "new_word - 1" because otherwise the program count also the "0".

    K&amp;R symbolic constants, beginner example-kplusr-worte-jpg

  4. #4
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    103
    Small correction:

    Code:
    do 
        {
            c = getchar();
            char_number++;
            if (c == '\n')
                { new_line++; }
            if (c == ' ' || c == '\n' || c == '\t')
                { state = OUT; }
            else if (state == OUT)
            {
                state = IN;
                new_word++;
            }
        }while (c != '0');
        //"new_word - 1" sonst wird die "0" mitgezählt
        //And so only the characters are counted without "\n".
        printf("\n%3d %3d %3d\n\n", new_line, new_word - 1, (char_number - (new_line + 1)));
    K&amp;R symbolic constants, beginner example-kplusr-worte2-jpg

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    713
    Quote Originally Posted by Kernelpanic View Post
    The K&R program run correct, almost . . . no termination condition. The gentlemen K&R don't like this.
    I'm not sure what you're on about. The termination condition is end-of-file. That is, when end-of-file is reached, the program terminates.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,733
    > Small correction:
    Your code will lock up at EOF.
    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.

  7. #7
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    103
    Original code:

    Code:
    #include <stdio.h>
    
    
    #define IN 1
    #define OUT 0
    
    
    int main(void)
    {
        int c, state;
        int new_line, new_word, char_number;
        
        state = OUT;
        new_line = new_word = char_number = 0;
        while ((c = getchar()) != EOF) 
        {        
            char_number++;
            if (c == '\n')
                { new_line++; }
            if (c == ' ' || c == '\n' || c == '\t')
                { state = OUT; }
            else if (state == OUT)
            {
                state = IN;
                new_word++;
            }
        }    
        printf("\n%3d %3d %3d\n\n", new_line, new_word - 1, (char_number - (new_line + 1)));
        
        return(0);
    }
    K&amp;R symbolic constants, beginner example-kplusrnoend-jpg

    Where is the end-of-file? Termination with "String-C" only.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    713
    Kernelpanic: In the Windows world, you can type end-of-file in a terminal with Ctrl-Z (and maybe press Enter too). In the Unix world (Linux, OS X, Cygwin, and others), type Ctrl-D instead.

    What do you mean by "String-C"? Do you mean "Ctrl-C"? That forcefully terminates the program.

  9. #9
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    103
    That's the same file in the Linux-World (Console Linux openSuSE 15 Leap):

    K&amp;R symbolic constants, beginner example-kplusr-unterlinux-jpg

  10. #10
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    103
    And this is my version:

    K&amp;R symbolic constants, beginner example-kplusrmyversion-jpg

    Also under Linux it works correct.
    Last edited by Kernelpanic; 12-31-2018 at 04:11 PM.

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    713
    How about this:

    K&amp;R symbolic constants, beginner example-176919-png

    I typed Ctrl-D after typing the "tree" line, and it works as designed (I don't know if the results are correct, just that the program exits cleanly). Go ahead and give it a try. Type Ctrl-Z and then Enter on Windows or Ctrl-D everywhere else. That tells the program "end of file" when the input is taken from a terminal.

  12. #12
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    103
    @Christop - No, No, No, the program from K&R is not correct.

    A program has to terminated with a simple commando. Not with "String-D" or "String-C" or what ever. That's no correct!

    And this original program from the Gurus is without a correct termination. That's a fact!

  13. #13
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    713
    Quote Originally Posted by Kernelpanic View Post
    A program has to terminated with a simple commando. Not with "String-D" or "String-C" or what ever. That's no correct!
    "End of file" is about the simplest "command" there is. Besides, who says a program "has to" terminate with a simple command? Tell that to the authors of vi and emacs! (To quit vi without saving changes to a file you have to type ":q!" plus Enter.)

    Keep in mind that Dennis Ritchie (the "R" in K&R) is a co-author of both the C language and Unix. He's not just some "guru". You would have to make a very strong case for why a program written by one of the creators of the language itself is "not correct".

  14. #14
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    103
    Quote Originally Posted by christop View Post
    Keep in mind that Dennis Ritchie (the "R" in K&R) is a co-author of both the C language and Unix. He's not just some "guru". You would have to make a very strong case for why a program written by one of the creators of the language itself is "not correct".
    No, he is a human, not a new The Lord. He is only a human and no one is without mistakes.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,496
    Just out of curiosity did you check if the book has an errata for that particular section/exercise? Also do you know that there is an "answer" book for all the exercises available.

    What edition of the book are you reading, if not second edition then put in a glass case since it only covers pre-standard C.?

    And no matter the version do you realize that that book is quite ancient (1988, 1978) and really doesn't cover modern C?

    What is your experience level? If you're a beginner then you really should consider finding a more modern book that has good reviews. IMO, that book, because of it's age should really only be used as a "reference" to the language.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Symbolic Constants? O_o
    By SCRIPT_KITTEH in forum C Programming
    Replies: 6
    Last Post: 07-19-2013, 11:10 PM
  2. Help with symbolic constants
    By rpmischris in forum C Programming
    Replies: 20
    Last Post: 09-17-2012, 08:15 PM
  3. is there a weird rule about symbolic constants?
    By i_can_do_this in forum C Programming
    Replies: 5
    Last Post: 07-10-2006, 07:14 AM
  4. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 08:29 PM
  5. Symbolic Constants??
    By ACAC in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2001, 06:09 PM

Tags for this Thread