C preprocessor: Difference between revisions

Added some basic information on the preprocessor
[unchecked revision][unchecked revision]
(Added some basic information on the preprocessor)
Line 5:
 
=== Rules ===
<source lang="c">
#ifdef SOME_MACRO
#define SOME_OTHER_MACRO SOME_VALUE
#undef SOME_MACRO
#endif
 
#ifndef NDEBUG
/* Older compilers may not have __func__ */
fprintf(stderr, __FILE__ ":" __LINE__ " : in function " __func__ "\n");
#endif
</source>
The preprocessor can be used to define macros with the #define macro. A previously defined macro can be undefined with #undef.
And #ifdef block can be used to conditionally compile a block of code if a certain macro is defined. This can be useful if the code is
platform-dependant, or is used for debugging.
 
When the preprocessor reads a file, it replaces any occurrence of a defined macro with the value it was defined to. Since the preprocessor is
language agnostic, if an error is introduced through its use, the error will not be reported until the file is compiled. This also means
that the preprocessor can be used with languages other than C. The standard way to invoke the preprocessor with GCC is "gcc -E".
{{stub}}
=== Uses ===
Anonymous user