From 2578beaa1d3546b6f1a95aeb234a136fd3cfad60 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Mon, 26 Feb 2024 07:54:15 +0000 Subject: [PATCH] Refresh HTML pages --- 02.html | 92 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 46 insertions(+), 46 deletions(-) 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