C Preprocessor check before compilation

In C, the #define token is widely used as a macro and maybe others. When we write a program with complex #define and such, maybe incorrectly used on top of another, how can we see if they are really correct?

In GCC (and probably others like c++), we can use -E flag to show what the code will look like after the preprocessing part, but not yet compiled.

For example, we write a short test code here:

#include 

/* Looking for the minimum */
#define MIN(a, b) (a < b ? a : b)
int main(int argc, char *argv[])
{
    int a = 5;
    int b = 3;
    printf("%d\n", MIN(a, b));
    return 0;
}

With gcc -E now we see:

......
# 943 "/usr/include/stdio.h" 3 4

# 2 "arg.c" 2

int main(int argc, char *argv[])
{
    int a = 5;
    int b = 3;
    printf("%d\n", (a < b ? a : b));
    return 0;
}

Which is quite useful to see the validity of macro correctness.

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.