C Preprocessor and Macros
C Preprocessor and Macros
Take me there
C Flow Control
C Preprocessor and Macros
In this tutorial, you will be introduced to c preprocessors, and you will learn to use #include,
C Functions
#define and conditional compilation with the help of examples.
C Programming Arrays
C Programming Pointers
C Programming Strings
C Programming Files
Additional Topics
C Enumeration
C Preprocessors
C Standard Library
C Programming Examples The C preprocessor is a macro preprocessor (allows you to define macros) that transforms
your program before it is compiled. These transformations can be the inclusion of header file,
macro expansions etc.
A DV E RT I S E M E N T S
#include <stdio.h>
Here, stdio.h is a header file. The #include preprocessor directive replaces the above line
with the contents of stdio.h header file.
That's the reason why you need to use #include <stdio.h> before you can use functions like
scanf() and printf() .
You can also create your own header file containing function declaration and include it in
your program using this preprocessor directive.
#include "my_header.h"
Here's an example.
#include <stdio.h>
#define PI 3.1415
int main()
{
float radius, area;
printf("Enter the radius: ");
scanf("%f", &radius);
printf("Area=%.2f",area);
return 0;
}
#include <stdio.h>
#define PI 3.1415
#define circleArea(r) (PI*r*r)
int main() {
float radius, area;
return 0;
}
Visit this page to learn more about macros and #define preprocessor.
Conditional Compilation
In C programming, you can instruct preprocessor whether to include a block of code or not.
To do so, conditional directives can be used.
The if statement is tested during the execution time to check whether a block of code
should be executed or not whereas, the conditionals are used to include (or skip) a block of
code in your program before execution.
Uses of Conditional
to exclude certain code from the program but to keep it as reference for future purpose
To use conditional, #ifdef , #if , #defined , #else and #elseif directives are used.
#ifdef Directive
#ifdef MACRO
// conditional codes
#endif
Here, the conditional codes are included in the program only if MACRO is defined.
#if expression
// conditional codes
#endif
The conditional codes are included in the program only if the expression is evaluated to a
non-zero value.
#if expression
conditional codes if expression is non-zero
#else
conditional if expression is 0
#endif
You can also add nested conditional to your #if...#else using #elif
#if expression
// conditional codes if expression is non-zero
#elif expression1
// conditional codes if expression is non-zero
#elif expression2
// conditional codes if expression is non-zero
#else
// conditional if all expressions are 0
#endif
#defined
The special operator #defined is used to test whether a certain macro is defined or not. It's
o en used with #if directive.
Predefined Macros
Here are some predefined macros in C programming.
Macro Value
The following program outputs the current time using __TIME__ macro.
#include <stdio.h>
int main()
{
printf("Current time: %s",__TIME__);
}
Output
Recommended Readings
Line control
Pragmas
Preprocessor Output
Other directives
Related Tutorials
Swi Tutorials
C# Tutorials
DSA Tutorials