C Preprocessors and Macros
In this tutorial, we will learn about preprocessors and macros in C language with the help of examples.
Preprocessors
In C programming, preprocessor is a macro processor which is automatically used by the compiler to transform the program before its actual compilation.
Commands used in preprocessor are called preprocessor directives.
All preprocessor directives starts with #.
Working of Preprocessors
The source code of the program begins to executes, preprocessor replace #define(macro), #include(files), conditional compilation codes like #ifdef, #ifndef by their Respectative values & source codes in source file.
Advantages of Preprocessors
- Easy to develop : Programs are easy to develop with the help of preprocessors.
- Easy to read : Programs are easy to read and source code become shorter.
- Easy to modify : Programs are easy to modify due to less code.
- Increase Protability : Programs become more portable and more transportable between different machine architecture.
Types of Preprocessor Directives
- Macros
- Header file inclusion
- Conditional compilation
- Other directives
Macros
A macro is a piece of code which has been given a name. Whenever the name is used in the program, the name is replace by the value of the macro.
The #define preprocessor directive is used to define a macro.
Syntax :
#define macro_name value
Example :
#define PI 3.14
Whenever, we use PI in our program it is replace with the value 3.14.
Example : Calculating Area of circle
#include <stdio.h>
int main() {
auto int a = 10;
{
auto int a = 11;
{
auto int a = 12;
printf("%d ", a);
}
printf("%d ", a);
}
printf("%d ", a);
}
Here, We have use macro PI whose value is 3.14. Whenever, we use PI keyword in our program, then it is replaced by the value 3.14.
Note : We cannot use semi-colon(;) at the end of macro definition.
Macro with Argument
We can define macros that work like a function call. These macros are also known as function-like macros.
#include <stdio.h>
#define Area(l,b) (l * b)
int main() {
int length = 15, breadth = 20, rect_area;
rect_area = Area(length,breadth);
printf("Area of the rectangle : %d",rect_area);
return 0;
}
Predefined Macros
Here are some predefined macros in C programming.
Macro | Value |
---|---|
__LINE__ |
Line # of the file being compiled |
__FILE__ |
Name of the file being compiled in string format |
__DATE__ |
Current date is compiled as "mm dd yyyy" in string format |
__TIME__ |
Current Time is compiled as "hh:mm:ss" in string format |
__STDC__ |
Signify that the compiler follow ANSI Standard C using boolean values. |
Example 3: Get current time using __TIME__
#include <stdio.h>
int main() {
printf("Time : %s",__TIME__);
return 0;
}
Output
Time : 12:43:13
File Inclusion
File Inclusion preprocessors are used to include a file in the program.
Header or standard files : These files contain definitions of Functions and Variables. Each header file contains information(or declarations) for a particular group of functions.
Types of file we include in the program
Example :
#include <stdio.h>
Here, stdio is the header file name which contain pre-defined functions like printf(), scanf() etc.
User-defined files : These files are the other parts of the program which can be used to make the program more readable.
Example :
#include "file_name.h"
Here, file_name is the name of the file we want to include.
When the program become vary large, then it is divides into smaller files and include Whenever required.
Conditional Compilation
These preprocessor directives are used to compile or skip of some specific part of the program based on whether some conditions are true or false.
#ifdef and #endif preprocessor commands are used for conditional compilation.
Syntax :
#ifdef expression
// conditional codes
#endif
Example :
#include <stdio.h>
int main() {
#define Algbly "An amazing website"
#ifdef Algbly
printf(Algbly);
#endif
return 0;
}
Other Directives
These preprocessor directives are used very less and any program can be made without using them.
#undef
The #undef preprocessor is used to undefine the constant or macro defined by #define.
Example :
#include <stdio.h>
#define PI 3.14
#undef PI
int main() {
printf("%f",PI);
return 0;
}
Output
error: ‘PI’ undeclared
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Preprocessor in C.
Keep Learning : )
In the next tutorial, you'll learn about C Standard Library
.