C: Lesson 18

C Preprocessor
Includes other files being compiled
Defines symbolic constants and macros
Processes conditional compilation and conditional execution of preprocessor directives.
Preprocessor directives begin with
#
and only white-space characters and comments may appear before a preprocessor directive on a line.
#include Preprocessor Directive
The
#include
directive causes a copy of a specified file to be included in place of the directive.The two forms of the
#include
directive are#include <filename">
Finds standard library implementation#include “filename“
Finds user defined implementation - starts searches in the same directory and may search in other locations too
#define Preprocessor Directive for Constants & Macros
The
#define
directive creates symbolic constants.- format:
#define identifier replacement-text
- format:
Constants are represented as symbols
- eg:
#define PI 3.14159
- eg:
Macros are operations defined as symbols.
- eg
#define CIRCLE_AREA(x) ( (PI) * (x) * (x) )
- eg
The replacement text for a macro or symbolic constant is normally any text on the line after the identifier in the #define directive.
We can use backlash (
\
) at the end of the line if the macro needs to continue on the next lineReplacements would look something like the following
Before processed:
area = CIRCLE_AREA(4)
After processed:
area = ((3.14159) * (4) * (4))
Conditional Compilation
Controls the execution of preprocessor directives.
Each of the conditional preprocessor directives evaluates a constant integer expression
Cast expressions, sizeof expressions and enumeration constants cannot be evaluated in preprocessor directives.
Common use cases:
To comment out portions of the code we want to have in development purposes only
To make use of debuggers
# Operator
The
#
operator causes a replacement text token to be converted to a string surrounded by quotes.The # operator must be used in a macro with arguments because the
operand of # refers to an argument of the macro.
- eg:
#define HELLO(name) printf("hello " #name "\n")
- eg:
## Operator
The ## operator concatenates two tokens.
- eg.
#define TOKEN_CONCAT(A, B) A ## b
- eg.
Line Numbers
line_number
: Sets the current line number to this value."filename" (optional)
: Sets the current file name that the compiler uses in messages (e.g., for warnings and errors).
Predefined Symbolic Constants
Symbol | Description |
__FILE__ | The current filename as a string literal. |
__LINE__ | The current line number in the source file (as an integer). |
__DATE__ | The current compilation date as a string literal (e.g., "Apr 19 2025"). |
__TIME__ | The current compilation time as a string literal (e.g., "14:32:01"). |
__STDC__ | Defined as 1 when the compiler conforms to the ANSI C standard. |
__STDC_VERSION__ | The version of the C standard supported (e.g., 199901L for C99). |
__STDC_HOSTED__ | Defined as 1 if the implementation is a hosted environment. |
Assertions
The assert macro is defined in the
<assert.h>
headerTests the value of an expression.
Subscribe to my newsletter
Read articles from Arty directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
