From the course: Secure Coding in C (2019)
Formatting preprocessor directives - C Tutorial
From the course: Secure Coding in C (2019)
Formatting preprocessor directives
- [Instructor] Pre-processor directives can conditionally compile your code, but when implemented improperly, they can cause unpredictable behavior. For example, here you see a defined, constant gray scale. Now it's used at line 13 to conditionally compile the code. If it's defined, line 14 will set two arguments, for the set pixel data function. Otherwise, line 16 sets the arguments. Not only is this code less readable than a more compliant choice, the results could be unpredictable, specifically if set pixel data is a macro, and not a directly defined function. Now this code is just a sample, and you can see that the function isn't a macro, but for other code, why take the risk? In this safe solution the function itself is set within the decision tree, so if the gray scale constant is defined, the full function statement is executed at line 13 or 15, depending on the definition. And here in the Code::Blocks editor, the statement that's compiled is highlighted for you. Now, beyond being safe, this presentation is far more readable than the earlier example. In fact, I would use the readability argument to prioritize this type of coding. It will always compile and run properly, whether the function specified is written as a macro or directly.
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
-
-
-
-
-