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

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