Date: agosto 25, 2025
Author: Guillermo Garcia
Categories: Embedded C programming Tags: Embedded C programming
In this article we will see how to use preprocessor directives in C language programs.
Table of Contents
A program written in C code needs to traverse different steps before it can be executed. First, the C preprocessor or parser will search for preprocessor directives in the source code and take the appropriate action. Next, the compiler will translate the code produced by the preprocessor into an object-file. Finally, the linker will create an executable.
Preprocessor directives always begin with #. Only white spaces or comments can be written on the same line before a preprocessor directive.
#define can be used to define both symbolic constants and macros.
The general format is:
#define symbolic-name replacement-text
The result of this directive is that the preprocessor will look for all occurrences of the symbolic-name in the source code and replace each occurrence with the replacement-text before the source code is compiled.
#define MAX 100 #define YES 1 if(i < MAX) answer = YES;
In the above example, the word MAX will be replaced with the value 100 and the word YES with the value 1.
The preprocessor directive #undef discards the symbolic constant from there on
#undef MAX
A macro is basically a symbolic constant with arguments, defined in a #define preprocessor directive.
Like for a symbolic constant, all occurrences of the macro name will be replaced by the replacement text. The only difference is the presence of the arguments that will be substituted in the replacement text before replacing the macro name occurrences.
#define MAX(a,b) ((a) > (b) ? (a) : (b))
Wherever MAX(x,y) appears in the file, the values of x and y will replace the letters a and b in the macro. Afterwards, the full replacement text with a and b substituted, will replace MAX(x,y).
For instance the statement:
y= 2 * MAX(i+1, j-1);
will be replaced by:
y = 2 * ((i+1) > (j-1) ? (i+1) : (j-1));
The parenthesis around every a and b in the replacement text are needed to force the correct order of evaluation when the macro arguments happen to be an expression rather than a single value.
With the #include preprocessor directive a copy of the specified file will be included in the source code. The 2 standard formats for the #include directive are:
#include <filename> #include “filename”
In the first case, the filename is enclosed in angle brackets (< >). This instructs the preprocessor to search for the file with name filename in the standard include directory. If the file is not found, a preprocessor error will be issued.
When filename is enclosed in double quotes (“ ”), like in the second format, the preprocessor will look for the file in the directory that contains the source code file. If not found in that directory, the standard include directory will be searched instead. This is typically used to includeprogrammer defined header files:
#include “header.h”
Programmer defined header files contain preprocessor definitions, declarations of structures, function prototypes, enumerations, typedef’s and, if needed, global variables.
Conditional compilation allows to control which preprocessor directives are carried out and what portion of the source code will be compiled. A conditional preprocessor directive can be followed by a constant integer expression only.
The general format of the #ifdef preprocessor directive is:
#ifdef <identifier> C code to be compiled if the identifier has been defined #else C code to be compiled if the identifier has not been defined #endif
An example of using #ifdef to control the execution of preprocessor directives is shown below:
#ifndef PI #define PI 3.14159265358979 #endif
The usage of #if is similar to the one of #ifdef except that #if can be followed by a constant expression:
#define TEST 1
int main(void)
{
…
#if TEST
printf(…);
#endif
return 0;
}

Deja una respuesta