diff --git a/02.html b/02.html index c7d5322f..0d45c9b1 100644 --- a/02.html +++ b/02.html @@ -819,6 +819,52 @@

getchar()

+ +

🔧 Task: write code that will read characters from a terminal and prints them out.

+

It should work like this:

+
$ cat /etc/passwd | ./a.out > passwd
+$ diff passwd /etc/passwd
+$ echo $?
+0
+
+ +
if ((c = getchar()) == EOF)
+	return (0);
+

instead of:

+
c = getchar();
+if (c == EOF)
+	return (0);
+

However, do not abuse it as you may create a hard to read code. Note the +parentheses around the assignment. The = operator has a lower priority than +the == operator. If the parens are not used, the following would happen:

+

if (c = getchar() == EOF) would be evaluated as:

+

if (c = (getchar() == EOF)), meaning that c would be either 0 or 1 based +on whether we read a character or the terminal input is closed.

+

We will learn more about operator priority later in the semester.

+

🔑 getchar.c

The sizeof operator