Macros and its Types in C
Hello Everyone, In this article, we will discuss about Preprocessor in C Programming. Macros are a powerful tool that allows you to define code that can be reused throughout your program. They help make your code more efficient and easier to read. We will cover what macros are, how to create them, their types, and provide examples to help you understand their use. Whether you are new to C or have some experience, this guide will help you to understand basics of Preprocessor in C effectively with examples. Let’s get started.
Macros in C Programming are preprocessor directives that allow you to define constants, perform simple computations, or generate code at compile time. Macros are defined using the #define
directive and are replaced with their values or expressions before the actual compilation of the code. This means that macros are evaluated at the pre-processing stage, before the actual compilation of the program.
Here’s a table that shows some of the predefined macros in C:
Macro | Definition |
---|---|
DATE | The date of compilation in the format “Mmm dd yyyy” |
TIME | The time of compilation in the format “hh:mm:ss” |
FILE | The name of the current source file |
LINE | The current line number in the source file |
func | The name of the current function (C99) |
In addition to the predefined macros, you can define your own macros using the #define
directive. Macros can take arguments and can be used to define constants, perform simple computations, or generate code.
For example, here’s a simple macro that defines the value of PI:
#define PI 3.14159265359
And here’s a macro that calculates the area of a circle:
#define CIRCLE_AREA(r) (PI * (r) * (r))
You can use this macro in your code like this:
float radius = 2.0;
float area = CIRCLE_AREA(radius);
After preprocessing, the code will look like this:
float radius = 2.0;
float area = (3.14159265359 * (radius) * (radius));
Type of Macros
The two types of macros in C are Object-like macros and Function-like macros.
1. Object-like macros: Object-like macros are the simplest type of macro, which are used to define a constant value or expression. They are defined using the #define
directive followed by the macro name and the value or expression it represents. When the macro is used in the code, it is replaced with the defined value or expression. For example:
#define PI 3.14159
2. Function-like macros: Function-like macros are similar to functions in that they take arguments and perform operations based on those arguments. They are defined using the #define
directive followed by the macro name, the parameter list in parentheses, and the code that performs the operation. When the macro is used in the code, it is replaced with the code that performs the operation, with the arguments substituted for the parameters. For example:
#define CIRCLE_AREA(r) (PI * (r) * (r))
In this example, the CIRCLE_AREA macro takes an argument r, and computes the area of a circle with that radius using the previously defined PI macro.