c - What does this not work for getchar() method? -


this own experiment understand goes under hood, program mean compiler?

main() {   int c;   printf("%d\n",c);   printf("%d ", getchar());     while ((c == getchar()) != eof){     putchar(c);   } } 

when c must equal getchar() (c == getchar()), not proceed through while loop? confused of own code, of c must mean!

also, in code:

main() { int c; c = getchar() while ((c = getchar()) != eof) putchar(c); } 

if modify int c int c = getchar(), why cannot somply write this:

while (c != eof)(     putchar(c);     c = getchar();     } 

the compiler should know previous statement c = getchar(), why have write statement again? sorry, if confused.

while ((c==getchar()) != eof) {   ... } 

is while loop. evaluates condition each iteration of loop , terminates if condition false.

in case, condition is:

(c==getchar()) != eof) 

which nonsensical expression, let's examine anyway:

first, program evaluate:

    getchar() 

this grabs keystroke standard input. value of expression value of key.

then:

 c==getchar() 

this takes result of getchar() , compares whatever in c. in first program, c uninitialized, value indeterminate. if c had defined value, c==getchar() evaluate either true or false. since c had no defined value, c==getchar() has no defined value.

now program evaluates:

(c==getchar()) 

which still true or false, except in case undefined.

the program next considers:

(c==getchar()) != eof 

that is, compares true-false value eof; makes no particular sense, , in case still have undefined behavior of uninitialized c.

in sum, if c initialized, expression fetch key standard input , compare either true or false eof. said, nonsensical expression.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -