Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
C preprocessor defined macros


1. C preprocessor defined macros
This page shows how to use some of the C preprocessor defined variables.

The C preprocessor has many pre-defined macros which are values (acting as variables but not variables) defined at compile time (not run time). Some are shown here.

2. Process
The preprocessor "pre processes" the source code before the compiler sees it.

preprocessor
We want to look at the preprocessor step in more detail.

3. Date and time macros
The __DATE__ and __TIME__ are the compile date and time, not the date and time when the program is run.
   __DATE__    __TIME__

These "v" can be used, for example, as part of the version of the software. They are not really "variables" but are dynamic textual replacements done at compile time by the preprocessor.

4. File and line variables
The __FILE__ and __LINE__ can be useful for debugging purposes (see below).
   __FILE__    __LINE__

Here is the C code.

Here is the output of the C code.


5. Debug output
A better debug output requires some system help in post-processing the output file or another generated file.

Here a beginning program is presented and then a preprocessor macro called printd added for debugging output support.

6. Example program
Here is the starting example program.

Here is the C code.

Here is the output of the C code.


7. Modified program
Here is the modified program example that uses a printd macro for text replacement and added debugging output.

Here is the C code.

Here is the output of the C code.

Note the following:

8. Intermediate file
The -E option to gcc allows one to see the intermediate code.
gcc -E mycode.c

This code has a lot of lines in it. There are 1478 lines for the above program (of 11 lines). Many lines are blank and many are comments with debugging line information in them. Remember, the include has many other includes and all of them are represented. For the above program, here are the last few lines.
# 6 "D:\\W\\PROGRAM1.C\\debug24-03.c" int main() { printf("One\n"); printf(":MARK f=[%s],r=%d\n","D:\\W\\PROGRAM1.C\\debug24-03.c",7); printf("Two\n"); printf(":MARK f=[%s],r=%d\n","D:\\W\\PROGRAM1.C\\debug24-03.c",8); printf("Three\n"); printf(":MARK f=[%s],r=%d\n","D:\\W\\PROGRAM1.C\\debug24-03.c",9); return 0; }

Note: The first line starting with a hash sign "#" is used for debugging information, much as the ":MARK" is used in the example program.

9. Platform
The C preprocessor can be used to determine the compiler platform on which the code is being compiled. Certain compiler platforms may have specific limitations, etc.

Note: One can also define symbols for different versions of software or the platform for which the code being compiled to be run (as opposed to the platform being used to compile the code to run). Here is the C code.

Here is the output of the C code.

This code was compiled on a CYGWIN platform (at the time these notes were created).

10. Production code
You may see many of these features, and more, used in production code.

You may also see these features used in code in future computer science courses that you may take.

11. End of page