Home > Back-end >  C programming language - exercise 1-6, problem for help
C programming language - exercise 1-6, problem for help

Time:11-04

The experiment is very simple, that is, verify the getchar ()!=EOF value is how much

The following is the validation program code
#include

Int main (void)
{
int ch;
While (ch=getchar ()!=(EOF)
Printf (" % d ", ch);

Printf (" % d ", ch);
return 0;
}

The input and the corresponding experimental phenomena as follows
Input/output
11 \ n 111
1.2. CTRL + z 0
3. 1 and CTRL + z 11 (cycle continue)


Cannot understand is the third time the input, the third time I understand the output should be 10 and end loop program, but why the result is 11, try again to enter, find CTRL + z must be followed at the beginning of the new row and before the line break before closing cycle procedure, do not understand why,

CodePudding user response:

 while (ch=getchar ()!=(EOF) 

Change

 while ((ch=getchar ())!=(EOF) 

Because!=priority than=high, therefore the judgment results assigned to ch, rather than getchar () returns a value assigned to ch

CodePudding user response:

Individual should think and system environment, and the compiler or IDE, not each combination operation result is so, my Linux + GCC has stopped would be prompted to enter CTRL + z, not 0,

CodePudding user response:

No problem, I deliberately do not add parentheses, not to read the characters, but in order to test the EOF, the final output is only 1 s and 0 s
  • Related