CH 14
1. Introduction
The preprocessor executes before the C compiler.
It performs actions like:
o Including other files in the source file.
o Defining symbolic constants and macros.
o Conditional compilation of program code.
o Conditional execution of preprocessor directives.
2. #include Directive
Used to include external files into the current source file.
Two forms:
o #include <filename>: Searches for the file in standard library directories.
o #include "filename": Searches in the current directory.
3. #define Directive
Symbolic Constants: Replaces all instances of a constant with its value, e.g., #define
PI 3.14159.
Macros:
o Can include arguments for operations.
o Example:
c
Copy code
#define CIRCLE_AREA(x) ((PI) * (x) * (x))
Common Pitfall: Avoid side effects like modifying variables in macro arguments, as
they can lead to errors due to multiple evaluations.
4. Conditional Compilation
Directives:
o #if, #elif, #else, #endif are used to include/exclude code sections based on
conditions.
o #ifdefand #ifndef are shorthand for checking whether a name is defined.
Useful for including debugging code selectively.
5. #error and #pragma Directives
#error: Terminates compilation with a custom error message.
#pragma: Allows implementation-defined actions, often used for specific compiler
features.
6. # and ## Operators
#: Converts a macro argument into a string.
o Example:
c
Copy code
#define HELLO(x) puts("Hello, " #x)
##: Concatenates two tokens.
o Example:
c
Copy code
#define TOKENCONCAT(x, y) x##y
7. Line Numbers
#line directive can renumber subsequent lines and set a new filename for error reporting.
Syntax:
c
Copy code
#line 100 "file1.c"
8. Predefined Symbolic Constants
Includes constants like:
o __LINE__: Current line number.
o __FILE__: Current filename.
o __DATE__ and __TIME__: Compilation date and time.
o __STDC__: Indicates Standard C support.
9. Assertions
Use the assert macro (from <assert.h>) to test conditions during development.
Example:
c
Copy code
assert(x <= 10);
Can be disabled by defining NDEBUG for production.
10. Secure C Programming
Highlighted best practices include:
o Avoiding unsafe macros that cause multiple evaluations of arguments.
o Prefer functions over macros for better safety and debugging