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 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.
#include header files (as modules)
#define to define preprocessor
#error for custom error messages (related to the compile)
#pragma for non-standard or compiler-specific directives.
#if, etc.
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;
Defined symbols can be used to toggle test mode and non-test mode on and off.
Such symbols can be defined in a file and included using the include directive so that an entire project can be made and the test mode changed in just one place.
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.
C#, VB.NET, .NET languages in general
VBA, Visual Basic
Delphi, Turbo Pascal
Common Lisp
Ada, Perl, JavaScript, Ruby, PL/SQL
Some popular programming languages do not provide preprocessor directives.