-6

Why the program listed below is not terminating. I am using gcc compiler in linux.

#include <stdio.h>

int main(void)
{
    int c;

    printf("Enter characters: ");

    while((c = getchar()) != EOF)
        putchar(c);

    return 0;
}
2
  • 1
    what is your input? Commented Apr 7, 2020 at 15:01
  • 2
    Do you ever input EOF? Commented Apr 7, 2020 at 15:01

1 Answer 1

3

This program will terminate just fine when the conditions are met. If you are on Linux press CTRL-D which is the EOF input. This will satisfy the condition and end the while loop.

while((c = getchar()) != EOF)
    putchar(c);

This function will run forever, keeping your program from terminating until it gets a EOF input. Check how to send EOF on your native system. Once you input that the program will terminate.

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

2 Comments

If you are on windows press Ctrl-D which is the EOF input.... No, Ctrl-D works on Unix systems, such as linux or Mac/OS. on Windows you would type Ctrl-Z Enter
Great point, will correct!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.