From the course: Secure Coding in C
Unlock this course with a free trial
Join today to access over 25,600 courses taught by industry experts.
Formatting preprocessor directives - C Tutorial
From the course: Secure Coding in C
Formatting preprocessor directives
- [Instructor] This exercise file shows defined constant GRAYSCALE. Its presence is used to conditionally affect compiling of this chunk of code. When the constant is present, the pixel_gray pointer is used, otherwise, pixel_color's pointer is used. You can compile this code, but it does issue a warning, as variable pc is unused, which is technically correct, but that's not the issue. Yes, one problem is that this code isn't really readable, but a bigger issue lies in the set_pixel_data function. While this function is legitimate, the pre-compiler directions here generate an unpredictable result, if in fact, this function is a macro. Here's a better construction. When the GRAYSCALE constant is defined, the full function statement is executed here or here, depending on the definition. Beyond being safe, this presentation is more readable. In fact, I would use readability as a priority for this type of coding. It will always compile and it will run properly, whether the set_pixel_data…
Contents
-
-
-
-
(Locked)
Formatting preprocessor directives1m 33s
-
(Locked)
Using an assignment as a condition1m 44s
-
(Locked)
Avoiding putchar() in a while loop2m 38s
-
(Locked)
Using the system() call2m 4s
-
(Locked)
Accessing elements beyond the array size2m 38s
-
(Locked)
Converting integers2m 57s
-
(Locked)
Looping with floating point values3m 10s
-
(Locked)
Using return values1m 52s
-
(Locked)
Confirming when EOF has been read3m 34s
-
(Locked)
Challenge: Fix the code1m 20s
-
(Locked)
Solution: Fix the code1m 54s
-
(Locked)
-
-
-
-