![]() |
VOOZH | about |
In C programming, #define is a preprocessor directive that is used to define macros. The macros are the identifiers defined by #define which are replaced by their value before compilation. We can define constants and functions like macros using #define. The generics in C are also implemented using the #define preprocessor directive along with _Generic.
The syntax of #define preprocessor directive in C is:
#define MACRO_NAME value#define MACRO_NAME (expression within brackets)Arguments passed in the macros can be used in the expression.
#define MACRO_NAME(ARG1, ARG2,..) (expression within brackets)
There are a few more ways using which we can define macros. To know more, refer to this article - Macros and its types in C
In the below example, we have defined a macro 'PI' and assigned it a constant value which we can use later in the program to calculate the area of a circle.
Area of Circle of radius 21: 1385
In the below example, we have defined a macro 'PI' and assigned it an expression, and that value of the expression is used in the program using 'PI'.
Area of Circle of radius 7: 147
In the below example, we have defined two macros CIRCLE_AREA and SQUARE_AREA with a parameter and that parameter is used in the expression to calculate the area of circle and square respectively.
Area of Circle of radius 21: 1384 Area of square of side 5: 25