From the course: Secure Coding in C (2019)
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Avoiding putchar() in a while loop - C Tutorial
From the course: Secure Coding in C (2019)
Avoiding putchar() in a while loop
- [Instructor] Using a functions return value as a condition in a loop is common, but better ways to code offer more predictability. This issue applies to both the getchar and putchar functions. In this code at line nine putchar appears as a condition in the while loop. The function output's a given character, but it returns that character's value, so the assumption is that the condition rings false, when the null character's encountered and returned, which happens at the end of the string, and coincidentally, terminates the loop. A better approach is to specifically test for the null character as shown in this improved version of the code, at line nine. The comparison is made specifically with a null character constant. This is a guarantee that the code runs as intended. Here's a pointer version of the same code. This is often the way I process a string, though it's subject to undefined behavior, the same as the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Formatting preprocessor directives1m 23s
-
(Locked)
Using an assignment as a condition1m 31s
-
(Locked)
Avoiding putchar() in a while loop1m 35s
-
(Locked)
Using the system() call1m 58s
-
(Locked)
Accessing elements beyond the array size2m 2s
-
(Locked)
Converting integers2m 26s
-
(Locked)
Looping with floating point values1m 39s
-
(Locked)
Using return values1m 58s
-
(Locked)
Confirming when EOF has been read2m 31s
-
(Locked)
Challenge: Fix the code1m 12s
-
(Locked)
Solution: Fix the code2m 12s
-
-
-
-
-