Question

I was trying an exercise from K&R (ex 1-17), and I came up with my own solution. The problem is that my program appears to hang, perhaps in an infinite loop. I omitted the NUL ('\0') character insertion as I find C generally automatically attaches it to the end of a string (Doesn't it?).

Can somebody please help me find out what's wrong?

I'm using the GCC compiler with Cygwin on win8(x64), if that helps..

Question - Print all input lines that are longer than 80 characters

#include<stdio.h>

#define MINLEN 80
#define MAXLEN 1000

/* getlin : inputs the string and returns its length */
int getlin(char line[])
{
    int c,index;

    for(index = 0 ; (c != '\n') && ((c = getchar()) != EOF) && (index < MAXLEN) ; index++)
        line[index] = c;

    return (index);                                                                     // Returns length of the input string
}

main()
{
    int len;
    char chArr[MAXLEN];

    while((len = getlin(chArr))>0)
    {
        /* A printf here,(which I had originally inserted for debugging purposes) Miraculously solves the problem!!*/
        if(len>=MINLEN)
            printf("\n%s",chArr);
    }
    return 0;
}
Was it helpful?

Solution

And I omitted the null('\0') character insertion as I find C generally automatically attaches it to the end of a string (Doesn't it?).

No, it doesn't. You're using getchar() to read input characters one at a time. If you put the chars in an array yourself, you'll have to terminate it yourself.

The C functions that return a string will generally terminate it, but that's not what you're doing here.

Your input loop is a little weird. The logical AND operator only executes the right-hand-side if the left-hand-side evaluates to false (it's called "short-circuiting"). Rearranging the order of the tests in the loop should help.

for(index = 0 ; (index < MAXLEN) && ((c = getchar()) != EOF) && (c != '\n'); index++)
    line[index] = c;

This way, c receives a value from getchar() before you perform tests on its contents.

OTHER TIPS

I'm not positive about what's wrong, but you don't provide the input to the program so I'm guessing.

My guess is that in getlin your variable c gets set to '\n' and at that point it never gets another character. It just keeps returning and looping.

You never SET c to anything inside your getlin function before you test it, is the problem.

C does not insert a NUL terminator at the end of strings automatically. Some functions might do so (e.g. snprintf). Consult your documentation. Additionally, take care to initialize all your variables, like c in getlin().

1) C doesn't add a final \0 to your string. You are responsible for using an array of at least 81 chars and puting the final \0 after the last character you write in it.

2) You're testing the value of c before reading it

3) Your program doesn't print anything because printf uses a buffer for I/O which is flushed when you send \n. Modify this statement to print a final \n:

printf("\n%s",chArr);

to become:

printf("%s\n",chArr);

4) To send an EOF to your program you should do a Ctrl+D under unix and I don't know if it's possible for windows. This may be the reason why the program never ends.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top