Introduction to Macros in C
In C programming language, when the C program is compiled then the program is sent to the compiler which converts the C program into machine language and then the compilation is done and executes the C program. The C preprocessor is also known as a macro preprocessor. The macro in C can be defined as a set of program statements is given a name and this name is used whenever these set of code has to be used. These macros always start with symbol “#” and the statements staring with this symbol are invoked by the compiler. There are two types of macros: object- like macro when data objects are used and function-like macro when function calls are used.
Examples of Macros in C
A macro in C programming language is a block or set of statements of program code that can be replaced by macro #define directive. As discussed above there are two types of macros. They are:
1. Object-Like Macro
This macro can be a pointer variable or identifier or variable defined using #define and it is replaced by its value. This is mainly used when numeric constants are required.
Example:
#define MAX 100
In this example, we can use “MAX” as a macro name with value 100 which will be replaced with “MAX”.
2. Function-Like Macro
This macro is similar to a function call in C programming language. In this, a function name is defined with arguments passed to the function.
Example:
#define ADD (p, q) (p) + (q)
In the above example, we can function named “ADD” is defined to perform the addition of two numbers “p” and “q”.
Before using macro in C programming we should note the following points:
- In C language, when we use “define” for defining a macro with “#” prefixed is used for defining constants for the entire program, so when we define a constant with some macro name then in the entire program the defined constant is matched and is replaced with the value that is assigned at the starting of declaring the macro with “#define” statement. Example:
#include<stdio.h>
#define PI 3.14
intmain()
{
printf("The value of Pi is %f", PI);
return 0;
}
Output:
Similarly “#include” is used to include the header files in the C program. For example, we saw in the above code “#include<stdio.h>” in this “stdio.h” is a header file we use this header file because to read and print the values we use “printf” and “scanf” are within this “stdio.h” header file.
- You should note that when the argument passed to the function-like macro where it supports any datatype. Let us consider an example below:
Example:
#include <stdio.h>
#define INCREMENT(n) ++n
intmain()
{
char *p = "Educba";
int a = 20;
printf(" This use char datatype %s ", INCREMENT(p));
printf("\n This uses int datatype %d", INCREMENT(a));
return 0;
}
Output:
- The macro can also be written in multiple lines starting the statements with “\” and no need to end with “\”. Let us see an example of this:
Example:
#include <stdio.h>
#define MACRO(n, limit) while (n < limit) \
{ \
printf("Educba "); \
n++; \
}
intmain()
{
int n = 0;
MACRO(n, 5);
return 0;
}
Output:
- In C, the macro can use conditional statements like if-else directives also while defining that can be used for conditional compilation. Let us consider an example:
Example:
intmain()
{
#if VERBOSE >= 2
printf("Message");
#endif
}
No Output:
- If the macro is used for a function-like macro which has arguments passed to it are not evaluated before macro expansion. Let us see the example:
Example:
#include <stdio.h>
#define DIV(a, b) a/b
intmain()
{
printf("%d", DIV(5+3, 3+2));
return 0;
}
Output:
Here in the above macro is expanded as 2 +3 /3 + 5 = 2 + 1 + 5 =8 which is confusing so you can resolve by below modified macro.
#include <stdio.h>
//here, instead of writing a*a we write (a)*(b)
#define DIV(a, b) (a) / (b)
intmain()
{
printf("%d", DIV(5+3, 3+2));
return 0;
}
Output:
The macro is expended as (2 + 3) * (3 + 5), as 5*8 = 1
- The macro also supports to pass the tokens to the macro to concatenate tokens using the special operator “##” which is known as the token pasting operator. Let us see an example:
Example:
#include <stdio.h>
#define concate(p, q) p##q
intmain()
{
printf("%d ", concate(56, 78));
}
Output:
The above code defines the function-like macro where the token is passed to concatenate two values of “p” and “q”.
- The macro also can be used to pass the token that can be converted to a string by using the “#” special symbol before the token. Let us see an example:
Example:
#include <stdio.h>
#define get_string(n) #n
intmain()
{
printf("%s", get_string(Educba));
}
Output:
In the above code, we are defining function-like macro where an argument “n” is passed with “#n” which will allow you to retrieve string literal.
Conclusion
In C programming language, the macro is not less than similar to functions, but macro has built-in macro also. In C, the macro is used to define any constant value or any variable with its value in the entire program that will be replaced by this macro name, where macro contains the set of code that will be called when the macro name is used in the program. The macro uses the “#define” directive to define the macro in C programming language. This is very useful to save time and have less code writing.
Recommended Articles
This is a guide to Macros in C. Here we also discuss the introduction and working of macros in c along with different examples and its code implementation. You may also have a look at the following articles to learn more –
3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses