logo
The C preprocessor
See what the compiler do on a code
Define macros
Prohibit the use of a function
Writing macros replacing functions
Predefined macros
Conditional macros

The C preprocessor

In the whole article, the compiler used is GCC.

See what the compiler do on a code

It is possible to see what the compiler do with the following command.
gcc -E -P code.c

Define macros

A basic exemple:
#define THING here is the thing
In this example all occurrences of 'THING' as defined in the macro will be replaced by 'here is the thing'. It is possible to define a macro on multiple lines by escaping the newline at the end of the line with the '\' character. A good habit is to always define macros name in uppercase. It is also good to always put constants in macros.

Prohibit the use of a function

It is possible to prohibit the use of a function with macros, for example we can write the following macro:
#define sprintf USE_SNPRINTF_NOT_SPRINTF
At compile time if the code contains sprintf: undefined reference to `USE_SNPRINTF_NOT_SPRINTF '.

Writing macros replacing functions

It is possible to write macros that have a similar functionality that a function, here is an example:
#include <stdio.h>
#define MIN(X, Y)  ((X) < (Y) ? (X) : (Y))
 
int main(int argc,char* argv[]) {
    printf("The minimum beetween 3 and 4 is %d\n",MIN(3,4));
    printf("The minimum beetween a and b is %c\n",MIN('b','a'));
    printf("The minimum beetween a+3 and b is %c\n",MIN('b','a'+3));
    return 0;
}
/*
hanoo@hp_laptop % ./a.out
The minimum beetween 3 and 4 is 3
The minimum beetween a and b is a
The minimum beetween a+3 and b is b
*/
Must always enclose by parens when using macros. And should not be put anywhere without reason, when in doubt, it is better not to. Here is an exaggerated example:
#include <stdio.h>
#define MIN(X, Y)  ((X) < (Y) ? (X) : (Y))
#define PRINT_MIN(A, B) printf("The minimum beetween %s and %s is %d\n",#A,#B,MIN(A,B));
#define BEGIN int main(int argc,char* argv[]) {
#define END return 0;}
 
BEGIN
    PRINT_MIN(3,4);
    PRINT_MIN('b','a');
    PRINT_MIN('b','a'+3);
END
/*
./a.out
The minimum beetween 3 et 4 is 3
The minimum beetween 'b' and 'a' is 97
The minimum beetween 'b' and 'a'+3 is 98
*/
While we're at it, why not write:
#include <stdio.h>
#define MIN(X, Y)  ((X) < (Y) ? (X) : (Y))
#define PRINT_MIN(A, B) printf("The minimum beetween %s and %s is %d\n",#A,#B,MIN(A,B));
#define BEGIN int main(int argc,char* argv[]) {
#define END return 0;}
#define MACRO_PROGRAMME BEGIN \
    PRINT_MIN(3,4);	      \
    PRINT_MIN('b','a');	      \
    PRINT_MIN('b','a'+3);     \
    END
 
 
MACRO_PROGRAMME
 
/*
./a.out
The minimum beetween 3 et 4 is 3
The minimum beetween 'b' and 'a' is 97
The minimum beetween 'b' and 'a'+3 is 98
*/
In short it is best avoided. Note however that if you put a '#' in front of a macro parameter, it is converted to a string.

Predefined macros

It is possible to see all the predefined macros by entering the following command:
gcc -dM -E - < /dev/null
There are also macros that retrieve information on compile time:
__FILE__the name of the current file
__LINE__the line number
__DATE__the date of compilation
__TIME__the time of compilation

Conditional macros

It is possible to cancel a definition with the command '#undef name'.
#ifdef nameIncludes code until '#endif' iff name is defined
#if exprIf expr is true (operation on integer constants) until include '#endif'
#ifndef nomIncludes code until '#endif' iff name is not defined
#elseThe else that we know
#elifThe else if that we know
Be The First To Comment.

Add a comment:





You can use these HTML tags: <br><a><i><b>