From the course: Secure Coding in C

Formatting preprocessor directives - C Tutorial

From the course: Secure Coding in C

Start my 1-month free trial

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.

Contents