C, K & R Exercise 1-6, Stuck, Confused

I am noob, teaching myself C programming using the C programming language, second edition (from K & R). In chapter 1, Section 1.5.1 Copying files, the authors very briefly refer to the priority of work when comparing values, emphasizing the importance of using parentheses, in this case, to ensure that the assignment is performed by the variable 'c' before evaluating. They claim that:

    c = getchar() != EOF

equivalently

    c = (getchar() != EOF)

Which "has an undesirable effect of setting cto 0 or 1, depending on whether the call to getchar met the end of the file"

The authors then submit Exercise 1-6 . Make sure the expression getchar () != EOFis 0 or 1

Based on the author’s previous statement, this seemed almost trivial, so I created this code:

#include <stdio.h>

main()
{
    int c;

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

Unfortunately, when I run the program, it simply displays all the characters that I entered in the command window, and not the expected line 1 or 0 if EOF is encountered.

, , , , . , c , , - getchar(), - ? c , putchar() 0 1, , , , . ? ? , ? Visual Studio 2017 Community Edition Windows 10 x64. Tiny C Compiler, TCC.

+4
2

, , . ( ) echo funcion, , ( Enter). ASCII 0 1, .

putchar(c) printf("%d", c), 1 s. , , c , .

'0' '1' ASCII 48 49 , , . 0, . putchar(48), ( , ).

putchar('0');
        ^ ^

,

c = getchar() != EOF;

c = (getchar() != EOF);

. != () = ( ), .

, - . , :

while ( (c = getchar()) != EOF )
+4

c = (getchar() != EOF) , . 1 , EOF. . - , . . , ascii 1. , .

EOF, . , , , ascii 1. ( , ). ascii-control. ( ).

, c=0 c=1 0 1.

int c= 68;
putchar(c);

, , , . , ascii 68, , 68.

((c = getchar()) != EOF).

, . . (-), ascii- 1 ( )

+2

Source: https://habr.com/ru/post/1689838/


All Articles