0

I wrote this code to get familiar with getchar(). I took line

while(ch != '\n' && ch != EOF)
    ch = getchar();

from a book. What is a mystery to me is that in the second while loop using getchar() leads to the program asking me for an input. In the line

while(ch != '\n' && ch != EOF)
    ch = getchar(); 

using getchar() does not lead to the program asking for an input. Is it because there is still information in the buffer?

My input is any line with more than the MAX_SIZE number of characters.

#include <stdio.h>
#include <string.h>

#define MAX_SIZE 10

int main ()
{
    int ch = 0,
        i = 0;

    char    array[MAX_SIZE] = {0};

    while(1)
    {
        i = 0;

        while((ch = getchar()) != EOF && ch != '\n' && i < MAX_SIZE - 1)
        {
            array[i++] = ch;
        }
        array[i] = '\0';

        while(ch != '\n' && ch != EOF)
            ch = getchar();

        printf("%s\n",array);
        for (int i = 0; i < strlen(array) + 1; i++)
            printf("[%d]%d\n",i, array[i]);
    }
}
4
  • 1
    You should somehow mark the lines you are talking about in the code. Line 25 is empty, btw. Otherwise every one needs to count your lines. Also, what input do you provide when you are asked for? Commented Apr 17, 2024 at 7:03
  • 2
    You should test i < MAX_SIZE - 1 before calling getchar(). The answer to your first question depends on the initial value of ch, which you haven't shown. Commented Apr 17, 2024 at 7:11
  • Can you explain in plain English, without referring to the source code, what you expect your program to do? Commented Apr 17, 2024 at 7:46
  • @Peter Kirsch, please improve your question by adding an example of exact input used, output seen and desired output. Commented Apr 17, 2024 at 12:35

2 Answers 2

2

There are two distinct cases to exit the first WHILE Case 1) The program exits the first WHILE because ch == EOF or ch == '\n' SO before entering the second WHILE, ch == EOF or ch == '\n' and therefore the exit condition is immediately fulfilled and we do not enter the loop

Case 2) The program exits the first WHILE because i == MAX_SIZE - 1 SO before entering the second WHILE, ch != EOF and ch != '\n' and enter the loop to empty the buffer

Conclusion, In Case 1 the buffer is empty after the first WHILE In Case 2 the buffer is emptied by the second WHILE

Sign up to request clarification or add additional context in comments.

Comments

-1

Your code skipping the remaing characters in in the line should be run only if the ch is not \n or EOF. Otherwise, you will eat the next line entered.

Here is a correct amended code (I added an exit condition too).

int skiptoLF(void)
{
    int ch;
    do 
    {
        ch = getchar();
    }while(ch != '\n' && ch != EOF);
    return ch;
}

int main ()
{
    int ch;
    char array[MAX_SIZE] = {0};

    while(1)
    {
        size_t i = 0;

        while((ch = getchar()) != EOF && ch != '\n' && i < MAX_SIZE - 1)
        {
            array[i++] = ch;
        }
        array[i] = '\0';

        if(ch != '\n' && ch != EOF) skiptoLF();
        if(!strcmp(array, "end")) break;

        printf("%s\n",array);
        for (int i = 0; i < strlen(array) + 1; i++)
            printf("[%d]%d\n",i, array[i]);
    }
}

1 Comment

In the original code, how many times does the loop while(ch != '\n' && ch != EOF) / ch = getchar(); iterate if ch is a newline character or is EOF? What does your code change in that regard?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.