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.
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.
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:
The __FILE__ is necessary for complex projects consisting of many files.
A convention such as always ending the printd string with a newline is needed (where printd is used).
Each printd should print one (and preferably only one) line, though there are ways around this.
A post-processing step is needed to extract the marks from the output and generate a suitable output.
Some suitable output, such as web-based HTML with JavaScript actions for click on the line is needed.
The editor can, for example, color-code such debugging statements so that they are easily seen.
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.
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.