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.
The C language preprocessor


1. The C language preprocessor

2. C preprocessor introduction

3. Preprocessor
The C compiler also uses a preprocessor that processes the source program before the compiler gets access to the transformed program. The idea started in ALGOL 68 (a complex language) but became popular with C.

4. Directives
Some preprocessor directives, introduced by the hash character "#" include the following in C. C also has a macro expansion capability that makes providing meaningful error messages more difficult.

5. Common directive
A common directive at the top of most C programs is the following.
#include

The stdio stands for "standard input output" and contains routines, such as printf and scanf to do fundamental input and output. If the directive is not present, the C compiler will not know the meaning of printf or scanf and will issue detected error messages.

6. Conditional compilation
Compiler preprocessor directives can be used to conditionally include or exclude source code in the compilation.
#define test #define debug #undef test #undef debug


7. Conditional compilation
Here is the C code.

Here is the output of the C code.


8. Conditional compilation
Note the following;

9. Pre-defined symbols
Many languages have similar directives for pre-processing, file inclusion, etc.

Many languages include pre-defined symbols and values. C defines the following (and many others).
__unix__ _WIN32 __FILE__ __LINE__

A cross-platform program for, say Windows or Linux, can be achieved by using conditional compilation to include code suitable for each platform.

10. Source file location
The "__FILE__" and "__LINE__" values can be used to provide source file debugging information.
The "__DATE__" and "__TIME__" values can be used to provide information about the version as to static date and time as to when the compile took place.

Here is the C code.

Here is the output of the C code.


11. Macro symbol expansion
A macro expansion replaces text with other text (i.e., a sample string rewriting system).

C does not have constants (named literals) but the same effect can be achieved using macro expansion.

Here is the C code.

Here is the output of the C code.

Note how C does not raise an error if an incorrect type specifier is used in a printf. Rather, it does to output anyway.

12. Macro function expansion
Here is the C code.

Here is the output of the C code.

The second printf does the conversion at compile time.
The third printf does the conversion at run time (unless the compiler is smart).

13. Other languages
Some other popular programming languages provide preprocessor directives. Some popular programming languages do not provide preprocessor directives.

14. Compiler directives

15. C/C++

   #include <>    #include ""    #define    #define =    #undef    #ifdef    #ifndef    #if    #elif    #else    #endif

Note: The C/C++ macro preprocessor is much more powerful than the subset of capabilities listed above.

16. Visual Basic

   #Const =    #If Then    #ElseIf Then    #Else    #End If


17. Delphi Object Pascal

   (*$I "" *)    (*$DEFINE *)    (*$UNDEF *)    (*$IFDEF *)    (*$IFNDEF *)    (*$ELSE *)    (*$ENDIF *)


18. End of page

19. Multiple choice questions for this page