There are several preprocessor statements that can help you determine which source code needs to be compiled and which needs to be excluded from being compiled. That is, a condition can be applied and the desired statements will be compiled only if the specified condition is true. These directives can be nested for more precise branching. There are numerous preprocessor statements, such as #if, #ifdef, #ifndef, #else, #elif, and #endif, that can be used to collect statements into blocks that we want to be compiled when the specified condition is true.
Some of the advantages of using macros are as follows:
- The execution speed of the program increases as the value or code of the macro is substituted by the name of the macro. So, the time involved in invoking or calling the functions by the compiler is saved.
- Macros reduce the length of the program.
The main disadvantage of using a macro is that the size of the program increases prior to the compilation of the program, as all the macros are substituted by their code. In this chapter, we will learn how to apply conditional compilation using preprocessor directives.
We will also learn how to implement validation in the program by making use of assertions. Assertions are a sort of validation check for different critical statements of the program. If those assertions or expressions don't validate or return false, then an error is displayed and the program is aborted. The main difference between this and usual error handling is that assertions can be disabled at runtime.
Besides the normal asserts, there are also asserts that are referred to as static or compile-time asserts, which are used to catch errors at the time of compilation. Such asserts can be used to do compile-time validations.
In addition to this, we will learn how to use stringize and token-pasting operators using the example of a pizza parlor.
In this chapter, we will learn how to make the following recipes:
- Performing conditional compilation with directives
- Applying assertions for validation
- Using assertions to ensure a pointer is not pointing to NULL
- Catching errors early with compile-time assertions
- Applying stringize and token-pasting operators
Let's start with the first recipe.