Consider the following code:
INTEGER n=5;
PRINT("Value of n is %d",n);
END
The above code is not a valid C code.
But the following code is valid:
#define START int main(){
#define END }
#define INTEGER int
#define PRINT(A,B) printf(A,B)
START
INTEGER n=5;
PRINT("Value of n is %d",n);
END
Before compilation, the macro START, INTEGER, PRINT and END has been replaced by their value, and the code becomes a valid C code. We can check using the following command:
This command will show after expanding all the macros.
Now we will see different types of macros:
1. Object-like macros:
Syntax:
- Macro is always starting with #define
- macro_name is a user-defined name of the macro
- macro_value is the value of the macro. It may be anything, but one line and the macro body ends with that line’s ends. It does not require semicolon (;) at the end. Space is also considered.
If the macro takes more than one line we can do it as follows:
macro_value2 \
macro_value3
#define MAX 200
This macro looks like a data object that’s why this type of macro called as an object-like macro.
//#include <stdio.h>
#define MAX 200
int main()
{
printf("MAX Value is: %d",MAX);
return 0;
}
In Exapmle2.c, MAX is a macro. From the output, we observe that MAX is replaced by its value 200.
2. Function-like macros:
Syntax:
macro_name is a user-defined name of the macro. Pair of parenthesis has to put after the macro_name. No space is allowed between macro_name and parenthesis. We also can pass arguments in this type of macros.
This macro looks like a function call that’s why this type of macro called a function-like macro.
#define add(x,y) x+y
int main()
{
int a;
float b;
a = add(4,5);
b = add(2.5,3.6)
return 0;
}
In Example3.c, we have seen that unlike C’s function, the macro replace only the code with arguments without calculating it. So, we can pass different data type using the same macro.
If we put a space between the macro name and parenthesis, it works the same as an object-like macro. Below C Example illustrates this.
#define add (x,y) x+y
int main()
{
int a;
float b;
a = add(4,5);
b = add(2.5,3.6)
}
In Example4.c, we have seen that the macro add is replaced by (x,y) x+y . Same as an object-like macro.
3. Macro for Token Pasting:
In C language, ## operator is used for token pasting. Using this operator, we can combine two valid tokens into one valid token.
Example:
#define MARGE(x,y) x##y
int main()
{
int num = MARGE(52,34);
return 0;
}
If we try to token pasting that does not generate a valid token, the C compiler gives an error or warning.
#define MARGE(x,y) x##y
int main()
{
int num = MARGE(52,+);
return 0;
}
In Example6.c, we have an error message because, after a combination of two tokens, we get an invalid token ’52+’.
4. Macro for Stringizing:
In C language, # operator is used to converting a macro parameter into a string constant. When a # operator precedes with a macro parameter, the parameter converts to a string literal. Stringizing can be used for object-like and function-like macros.
Example:
In Example7.c we have got a string “Hello World” using STRINGIZING macro.
Conclusion:
This article has learned about all types of macro-like Object-like macros, Function-like macros, Macro for Token Pasting, Macro for Stringizing and Macro for Stringizing in C language. Now we can use a macro in our C program without any doubt.
from Linux Hint https://ift.tt/3oYCd2V
0 Comments