Added Special Operators in Macros (#22514)
One small fix and the explanation of Stringizing and Token Pasting Operators that can be used effectively in C macros.
This commit is contained in:
committed by
Randell Dawson
parent
b72aca56d9
commit
9cea2d56a5
@ -31,30 +31,66 @@ Function-like uses the same `#define` keyword. The difference is that you use a
|
||||
```C
|
||||
#define hello_world() printf("Hello World!")
|
||||
```
|
||||
|
||||
So calling:
|
||||
```C
|
||||
hello_world()
|
||||
hello_world();
|
||||
```
|
||||
|
||||
You get:
|
||||
```C
|
||||
printf("Hello World!");
|
||||
```
|
||||
|
||||
You can set parameters too:
|
||||
```C
|
||||
#define hello(X) printf("Hello " X "!")
|
||||
```
|
||||
|
||||
Now calling:
|
||||
```C
|
||||
hello("World");
|
||||
```
|
||||
|
||||
You get the equivalent of:
|
||||
```C
|
||||
printf("Hello World!");
|
||||
```
|
||||
|
||||
#### Special Operators in Macros
|
||||
One can use the special operators # (stringize) and ## (concatenate) in macros for achieving unique functionality.
|
||||
|
||||
##### Stringizing Operator (#)
|
||||
A macro's parameter preceded by a `#` is converted and treated as a string token.
|
||||
For example, we can define ERROR and WARN macros that print a LOG message.
|
||||
While the LOG message gets prefixed with either an `ERR` or a `WARN`, respectively.
|
||||
```C
|
||||
#define LOG(level, message) printf(#level ": " #message "\n")
|
||||
#define ERROR(msg) LOG(FAIL, msg)
|
||||
#define WARN(msg) LOG(WARN, msg)
|
||||
```
|
||||
|
||||
Now, one can use it as
|
||||
```C
|
||||
ERROR(Invalid settings); // Output-> FAIL: Invalid settings
|
||||
WARN(Upper threshold); // Output-> WARN: Upper threshold
|
||||
```
|
||||
|
||||
##### Concatenation (or token-pasting) Operator (##)
|
||||
Using concatenation the parameters can be joined together to form one single token. Token-pasting is much more powerful in the sense that the resulting token could be an object defined in the C program.
|
||||
```C
|
||||
#define NUM(x) number_##x
|
||||
|
||||
void foo() {
|
||||
int number_one = 10;
|
||||
int number_two = 15;
|
||||
|
||||
printf("%d + %d = %d\n", NUM(one), NUM(two), NUM(one) + NUM(two));
|
||||
// Output-> 10 + 15 = 25
|
||||
}
|
||||
```
|
||||
|
||||
#### More Information:
|
||||
<!-- Please add any articles you think might be helpful to read before writing the article -->
|
||||
[GCC Online Documentation: Macros](https://gcc.gnu.org/onlinedocs/cpp/Macros.html)
|
||||
|
||||
[GCC Online Documentation: Object-like macros](https://gcc.gnu.org/onlinedocs/cpp/Object-like-Macros.html#Object-like-Macros)
|
||||
|
||||
[GCC Online Documentation: Function-like macros](https://gcc.gnu.org/onlinedocs/cpp/Function-like-Macros.html#Function-like-Macros)
|
||||
- [GCC Online Documentation: Macros](https://gcc.gnu.org/onlinedocs/cpp/Macros.html)
|
||||
- [GCC Online Documentation: Object-like macros](https://gcc.gnu.org/onlinedocs/cpp/Object-like-Macros.html#Object-like-Macros)
|
||||
- [GCC Online Documentation: Function-like macros](https://gcc.gnu.org/onlinedocs/cpp/Function-like-Macros.html#Function-like-Macros)
|
||||
|
Reference in New Issue
Block a user