Thread: C Primer Plus Exercise ch7.02

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    4

    C Primer Plus Exercise ch7.02

    Hi, I am learning/practising C using the book of <<C Primer Plus>>. Got stuck at the exercise 702:

    Write a program that reads input until encountering
    #. Have the program print eachinput character and its ASCII decimal code. Print eight character-code pairs per line.Suggestion: Use a character count and the modulus operator (%) to print a newlinecharacter for every eight cycles of the loop.

    And this is my code:

    Code:
    #include <stdio.h>
    #define SPACE ' '
    #define NL '\n'
    
    
    int main(void)
    {
        char ch;
        int m;
    
        m = 0;
    
        printf("\nEnter your input and end with a "#":\n");
        while ((ch = getchar()) != '#')
        {
            if (ch == NL)
                continue;
    
            if (ch == SPACE)
                continue;
    
            printf("%c:%d ", ch, ch);
                m++;
    
            if (m % 8 == 0)
                printf("\n");
        }
        printf("\n\n");
    
        return 0;
    }
    Compiled and ran. But whenever I hit the Return key during the input test even before I gave the # character to end the input, I got output shown on the screen. Baffled about it, anyone can help me to explain and give some hints? Many thanks.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You're code is very tiny.

    Your defines are not very useful. It communicates more in the code to see the actual '\n' and ' '. A more useful define would be:
    Code:
    #define ENDCHAR '#'
    since the idea that it's an terminating character is arbitrary.

    I'm unsure as to the source of your bafflement. The printf is inside your input loop, so it's not going to wait until you enter #. If you want that kind of behaviour you'd have to save up all the characters in an array and print them after the loop.

    Note that the getchar waits until a newline is entered before processing any input because it is "line buffered".

    Also note that getchar returns an int, not a char. This is so that it can use a special value that isn't equal to any possible char value to indicate an end-of-file (or error) condition. That special value is called EOF and is usually equal to -1.

    Code:
    #include <stdio.h>
    
    #define ENDCHAR '#'
    #define MAXSIZE 1000
    
    int main(void) {
        char a[MAXSIZE];
        int ch;
        int m = 0;
        int i;
    
        printf("\nEnter your input and end with a '#':\n");
        while ((ch = getchar()) != ENDCHAR) {
            if (ch == '\n' || ch == ' ')
                continue;
            if (m >= MAXSIZE)
                break;  // don't write past the end of the array!
            a[m++] = ch;
        }
    
        for (i = 0; i < m; i++) {
            printf("%c:%3d ", a[i], a[i]);
            if (i % 8 == 7)
                printf("\n");
        }
        printf("\n\n");
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    4
    Many thanks for the reply. Your explanation is very helpful. I've thought about using an array but tried to follow the examples in that particular chapter. I am new to C but use Matlab before. Is there any way for C to first define an empty array similar to Matlat like a[] and then add values later depending on the input size? Or we have to define a max size first to allocate the space for the array?

    Quote Originally Posted by algorism View Post
    You're code is very tiny.

    Your defines are not very useful. It communicates more in the code to see the actual '\n' and ' '. A more useful define would be:
    Code:
    #define ENDCHAR '#'
    since the idea that it's an terminating character is arbitrary.

    I'm unsure as to the source of your bafflement. The printf is inside your input loop, so it's not going to wait until you enter #. If you want that kind of behaviour you'd have to save up all the characters in an array and print them after the loop.

    Note that the getchar waits until a newline is entered before processing any input because it is "line buffered".

    Also note that getchar returns an int, not a char. This is so that it can use a special value that isn't equal to any possible char value to indicate an end-of-file (or error) condition. That special value is called EOF and is usually equal to -1.

    Code:
    #include <stdio.h>
    
    #define ENDCHAR '#'
    #define MAXSIZE 1000
    
    int main(void) {
        char a[MAXSIZE];
        int ch;
        int m = 0;
        int i;
    
        printf("\nEnter your input and end with a '#':\n");
        while ((ch = getchar()) != ENDCHAR) {
            if (ch == '\n' || ch == ' ')
                continue;
            if (m >= MAXSIZE)
                break;  // don't write past the end of the array!
            a[m++] = ch;
        }
    
        for (i = 0; i < m; i++) {
            printf("%c:%3d ", a[i], a[i]);
            if (i % 8 == 7)
                printf("\n");
        }
        printf("\n\n");
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by llnnttss View Post
    Many thanks for the reply. Your explanation is very helpful. I've thought about using an array but tried to follow the examples in that particular chapter. I am new to C but use Matlab before. Is there any way for C to first define an empty array similar to Matlat like a[] and then add values later depending on the input size? Or we have to define a max size first to allocate the space for the array?
    It's certainly possible (Matlab is probably written in C!) but it's considered somewhat "advanced" so you might want to save that for a little later. It involves the realloc function to expand the size of a dynamically-allocated array as needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Primer exercise 16.47
    By frank67 in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2015, 05:36 AM
  2. c++ primer exercise 16.12
    By frank67 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2015, 03:32 PM
  3. c++ primer exercise 15.31
    By frank67 in forum C++ Programming
    Replies: 10
    Last Post: 09-23-2015, 09:38 AM
  4. c++ primer exercise 15.26
    By frank67 in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2015, 09:34 AM
  5. C++ Primer 4th Edition, Problem with Exercise
    By Kaidao in forum C++ Programming
    Replies: 4
    Last Post: 07-15-2006, 11:13 AM

Tags for this Thread