0
votes

Im using scanf because we must use it. the problem is the following : (thats just an example of the problem):

    int main() {
        char ch [10]={0};
        scanf("%s",ch);

        printf("%s",ch);
    }

if i run the program and enter for example : word^Z when ^Z is EOF. the program stays in place, stuck in the scanf, althogh i did type word then ctrl+z then Enter. but somehow it stays in the scanf, its the same thing with redirection, like its not a problem with ctr+z or anything.

i hope that i can get some help

thanks in advance, totally apprecaite it :)

1
Which operating system? ^D is EOF on Linux, ^Z is EOF on Windows.Ry-
windows, ^Z is always eof and it works,except with scanf %sNajwan Abu Saleh

1 Answers

2
votes

scanf uses whitespace as a delimiter to store the read data into various fields. From the command line, entering ControlZ, then Enter only puts the EOF character into the input stream and scanf() continues waiting for whitespace. If you hit Enter again, scanf will receive the whitespace character, and everything including the EOF will be stored into the ch array.

Here's a sample run. The first line is the input, and the second line is the output.

Hello^Z
Hello→