1

I have written the following program to answer Kernighan and Ritchies ch1 problem 12.

The issue is that I have never really understood how to properly use functions and would like to know why the one I wrote into this program, getcharc(), does not work?

What are good resources that explain correct function usage. Where? and How?

I know the optimal solution to this problem from Richard Heathfield's site (which uses || or, rather than nested while statements, which I have used), however I would like to know how to make my program work properly:

#include <stdio.h>
int getcharc ();
// Exercise 1-12
// Copy input to output, one word per line
// words deleniated by tab, backspace, \ and space

int main()
{
    int c;

    while ((c = getchar()) != EOF) {
        while ( c == '\t') {
            getcharc(c);
        }
        while ( c == '\b') {
            getcharc(c);
        }
        while ( c == '\\') {
            getcharc(c);
        }
        while ( c == ' ') {
            getcharc(c);
        }
        putchar(c);
    }
}
int getcharc ()
{
    int c;

    c = getchar();
    printf("\n");
    return 0;
}

The original program (and I know it has bugs), without the function was:

#include <stdio.h>

// Exercise 1-12
// Copy input to output, one word per line
// words deleniated by tab, backspace, \ and space

int main()
{
    int c;

    while ((c = getchar()) != EOF) {
        while ( c == '\t') {
            c = getchar();
            printf("\n");
        }
        while ( c == '\b') {
            c = getchar();
            printf("\n");
        }
        while ( c == '\\') {
            c = getchar();
            printf("\n");
        }
        while ( c == ' ') {
            c = getchar();
            printf("\n");
        }
        putchar(c);
    }
}

So all I am trying to do with the function is to stop

c = getchar();
printf("\n");

being repeated every time.

10
  • 1
    Can you give us more information... What is K&R 1-12? What do you want the function to do? What do you observe it doing currently?
    – simonc
    Commented Oct 11, 2013 at 16:24
  • Kernighan and Ritchie = K&R 1-12 is exercise 1-12 in that book. It is the premier book for C programming. In this instance all I want the function to do is replace two lines of code repeated in the program: c = getchar(); printf("\n");. That's it, nothing else.
    – Deesbek
    Commented Oct 11, 2013 at 16:27
  • 2
    I know that much. My memory isn't up to remembering every example in the book however. I was hoping you'd describe the purpose of that exercise.
    – simonc
    Commented Oct 11, 2013 at 16:28
  • copy input to output, replace tab, backspace and multiple spaces with single space
    – Deesbek
    Commented Oct 11, 2013 at 16:29
  • 1
    Your code shouldn't compile at all since you said getcharc() does not take any arguments yet you pass int c into it (eg, getcharc(c)). It's like an DVD player with no input ports and you trying to shove a plug into it. Commented Oct 11, 2013 at 16:31

3 Answers 3

1

What, exactly, is this getcharc() function supposed to do? What it does, is read a character from input, print a newline, and return zero. The character just read from input is discarded, because you didn't do anything with it. When it's called, the return value is ignored as well. In each of the places where it is called, you're calling it in an infinite loop, because there's no provision made for changing the loop control variable.

Perhaps you were intending something like c = getcharc(), but that wouldn't really help because you aren't returning c from the function, anyway. (Well, it would help with the "infinite loop" part, anyway.)

What's the point of this function anyway? If you just use getchar() correctly in its place, it looks like you'd have your solution, barring a few other bugs.

5
  • Thanks for your answer. All I am trying to achieve with the function is to stop having to repeat these two lines 'c = getchar(); printf("\n");' in the original code. Which I have now attached to my original question.
    – Deesbek
    Commented Oct 11, 2013 at 17:39
  • Sorry, so what you are saying is don't use a function and just use getchar() repeatedly in each nested while? Is my understanding of your answer correct?
    – Deesbek
    Commented Oct 11, 2013 at 17:45
  • What is the purpose of the printf("\n")? It looks like you're printing a newline in the place of each repeated space/tab/backspace? Commented Oct 11, 2013 at 17:59
  • And no, I'm not saying not to use the function, I was just not understanding the purpose of it. If you're trying to move the loop into the function, move it entirely into the function, that's fine too. Commented Oct 11, 2013 at 18:01
  • printf("\n") is meant to print a new line instead of a space, tab, etc. So if the input is a number of words on one line with spaces, tabs etc separating the words, the program would then output, the words separately on individual lines.
    – Deesbek
    Commented Oct 11, 2013 at 18:11
0

One of the possible solution is, change prototype for your function to int getcharc (int c, int flag).
Now your code after some modification;

#include <stdio.h>
int getcharc (int c, int flag);
// Exercise 1-12
// Copy input to output, one word per line
// words deleniated by tab, backspace, \ and space

int main()
{
    int c;
    int flag = 0;  //to keep track of repeated newline chars.

    while ((c = getchar()) != '\n') {
        flag = getcharc(c, flag);   // call getcharc() for each char in the input string. Testing for newline and printing of chars be done in the getcharc() function
    }
    return 0;     
}

int getcharc (int c, int flag)
{
        if( (c == ' ' || c == '\t' || c == '\b' || c== '\\')  && flag == 0)
        {
            printf("\n");
            flag = 1;
        }
        else
        {
            if(c != ' ' && c != '\t' && c != '\b' && c!= '\\')
                {
                     putchar(c);
                     flag = 0;
                }
        }
        return flag;
}

EDIT:

but I wanted to keep the nested while statements rather than using || or

Your nested while loop is executing only once for each character as grtchar() reads one character at one time. No need of nested loops here! You can check it by replacing while to if and your code will give the same output for a given string. See the output here.

know the optimal solution to this problem from Richard Heathfield's site (which uses || or, rather than nested while statements, which I have used), however I would like to know how to make my program work properly:

You make your program work to some extent (with your bugs) by adding an if condition and a break statement as;

#include <stdio.h>
int getcharc (int c);

int main()
{
    int c;

    while ((c = getchar()) != '\n') {
        while ( c == '\t') {
            c = getcharc(c);
            if(c != '\t')
                break;
            }

        ....
        ....


        while ( c == ' ') {
            c = getcharc(c);
            if(c != ' ')
                break;
            }
        putchar(c);
    }
    return 0;
}
int getcharc (int c)
{
    c = getchar();
    printf("\n");
    return c;
}
5
  • thank you, but I wanted to keep the nested while statements rather than using || or.
    – Deesbek
    Commented Oct 11, 2013 at 17:43
  • Using nested loop is not a good idea always. Write program as simple as you can.
    – haccks
    Commented Oct 11, 2013 at 17:46
  • I understand that, and I can see that your answer is more elegant. However all I want is to keep the original code and stop having to repeat c = getchar(); printf("\n"); in every iteration of the while loop.
    – Deesbek
    Commented Oct 11, 2013 at 17:50
  • OK. Give me some time. I am busy with some other questions.
    – haccks
    Commented Oct 11, 2013 at 17:51
  • Will do, no problems. I will keep working through the book anyhow I'll wait for your update. Thanks again.
    – Deesbek
    Commented Oct 11, 2013 at 18:19
0
// compiled by my brain muhahaha

#include <stdio.h>
int getcharc(); // we prototype getcharc without an argument

int main()
{
    int c; // we declare c

    // read character from stdio, if end of file quit, store read character in c
    while ((c = getchar()) != EOF) {  
        // if c is tab \t call function getcharc() until forever since c never changes
        while ( c == '\t') { 
            getcharc(c); // we call function getcharc with an argument
            // however getcharc doesn't take an argument according to the prototype
        }
        // if c is \b call function getcharc() until forever since c never changes
        while ( c == '\b') {
            getcharc(c);
        }
        // if c is \\ call function getcharc() until forever since c never changes
        while ( c == '\\') {
            getcharc(c);
        }
        // if c is ' ' call function getcharc() until forever since c never changes
        while ( c == ' ') {
            getcharc(c);
        }
        // since we never will get here but if we happened to get here by some
        // strange influence of some rare cosmic phenomena print out c
        putchar(c);
    }
}

// getcharc doesn't take an argument
int getcharc ()
{
    int c;  // we declare another c

    c = getchar(); // we read from the keyboard a character
    printf("\n"); // we print a newline
    return 0; // we return 0 which anyway will never be read by anyone
}

maybe you are getting confused with the old K&R

nowadays when you write a function argument you specify it like

int getcharch(int c)
{
  ...
}
3
  • Thanks. I am using K&R the second edition, I know it is dated ;-) on page 24 he says to write the function argument as: int power(int m, int n); I just thought I could write the function argument without any inputs - noob me.
    – Deesbek
    Commented Oct 11, 2013 at 18:20
  • nothing wrong about learning new stuff, we all have been there
    – AndersK
    Commented Oct 11, 2013 at 18:23
  • it is funny that we can only mark as one answer correct, as your comments are what helped me immensely. Thanks again.
    – Deesbek
    Commented Oct 11, 2013 at 23:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.