C preprocessor

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#includeInstructions.

It also looks at every constant you define using#defineAnd 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#includewith#defineHave 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 checkDEBUGThe 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#defineWe 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#ifndefCheck if the opposite is true (macro is not defined).

We can also use#if definedwith#if !definedDo 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 aaaaformat
  • __TIME__Converted to compile time, inhh:mm:ssformat

Download mine for freeC manual


More clang tutorials: