How to use the C preprocessor
The preprocessor is a tool that helps us a lot when programming in C. It is part of the C standard, just like the language, compiler, and standard library.
It parses our program and ensures that the compiler has everything it needs before proceeding with the process.
Actually, what does it do?
For example, it will find all the header files that you have attached#include
Instructions.
It also looks at every constant you define using#define
And use its actual value instead.
This is just the beginning. I mentioned these two operations because they are the most common operations. The preprocessor can do more.
Did you notice#include
with#define
Have a#
In the beginning? This is common to all preprocessor directives. If a line starts with#
, By the preprocessor.
Conditional
One thing we can do is to change the way the program is compiled based on the value of the expression using conditions.
For example, we can checkDEBUG
The constant is 0:
#include <stdio.h>
const int DEBUG = 0;
int main(void) {
#if DEBUG == 0
printf(“I am NOT debugging\n”);
#else
printf(“I am debugging\n”);
#endif
}
Symbolic constant
We can define aSymbolic constant:
#define VALUE 1
#define PI 3.14
#define NAME "Flavio"
When we use NAME or PI or VALUE in a program, the preprocessor will replace its name with a value before executing the program.
Symbolic constants are very useful because we can name values at compile time without creating variables.
Macro
with#define
We can also define aMacro. The difference between a macro and a symbolic constant is that a macro can accept parameters and usually contains code, while a symbolic constant is a value:
#define POWER(x) ((x) * (x))
Please pay attention to the parentheses around the parameters. This is a good habit to avoid problems when replacing macros during precompilation.
Then, we can use it in our code like this:
printf("%u\n", POWER(4)); //16
The biggest difference between functions is that macros do not specify their parameter types or return values, which may be convenient in some cases.
If defined
We can check whether symbolic constants or macros are defined using#ifdef
:
#include <stdio.h>
#define VALUE 1
int main(void) {
#ifdef VALUE
printf("Value is defined\n");
#else
printf("Value is not defined\n");
#endif
}
we still have#ifndef
Check if the opposite is true (macro is not defined).
We can also use#if defined
with#if !defined
Do the same task.
Usually some code blocks are packed into such blocks:
#if 0
#endif
Temporarily prevent it from running, or use DEBUG symbolic constants:
#define DEBUG 0
#if DEBUG
//code only sent to the compiler
//if DEBUG is not 0
#endif
Predefined symbolic constants that can be used
The preprocessor also defines many symbolic constants that can be used. They are identified by two underscores before and after the name, including:
__LINE__
Convert to the current line in the source code file__FILE__
Convert to file name__DATE__
Converted to compilation date, inMmm gg aaaa
format__TIME__
Converted to compile time, inhh:mm:ss
format
Download mine for freeC manual
More clang tutorials:
- Introduction to C programming language
- C variables and types
- C constant
- C operator
- C conditional
- How to use loops in C
- Introduction to C arrays
- How to determine the length of an array in C
- Introduction to C strings
- How to find the length of a string in C
- C pointer introduction
- Iterate over the array in C
- Boolean in C
- Introduction to C functions
- How to use NULL in C
- Basic I/O concepts in C language
- Double quotes and single quotes in C
- How to return a string from a C function
- How to solve the implicit declaration library function warning in C language
- How to check character value in C
- How to use `printf()` to print percent characters in C language
- C conversion specifiers and modifiers
- How to access command line parameters in C
- Range of variables in C
- Can functions be nested in C?
- Static variables in C
- C global variables
- Typedef keyword in C language
- C enumeration type
- C structure
- C header file
- C preprocessor