For example, to display the contents of a variable on the screen, you would use a printf statement like this.
Specify values for enum
printf("temp = %d\n", temp);
However, if you need to display the value of the variable temp in multiple places,
If you find it tedious to type this sentence every time, you can replace it with a `#define` pseudo-instruction.
Source code
#include <stdio.h>
#define PRINT_TEMP printf("temp = %d\n", temp)
int main(void)
{
int temp = 100;
PRINT_TEMP;
return 0;
}
The results of this program's execution are as follows:
Execution results
temp = 100
The trick to this program lies in the definition of the `#define` pseudo-instruction.
The functionality of the #define preprocessor directive is simply substitution, and it can substitute anything, not just numbers.
Here, PRINT_TEMP is directly replaced in the printf statement that displays the variable.
Essentially, it's exactly the same as having a printf statement in that part programmatically.
This is a very powerful feature that can forcefully consolidate any program.
However, overuse of these techniques can result in programs becoming full of shortcuts.
You must use it with great care, as it can become unreadable to anyone but the original programmer.
A simple function called a macro
As we saw in the previous section, the #define pseudo-instruction is very powerful, but...
In fact, the #define pseudo-instruction has even more powerful capabilities.
It's possible to create simple functions using the #define pseudo-instruction.
In `#define` pseudo-instructions, specifying characters in parentheses after the name...
You can replace parts with the same alphabet in subsequent replacements.
For example, the following #define pseudo-instruction displays the specified integer variable.
Source code
#define PRINTM(X) printf("%d\n", X)
The following program demonstrates an example of using the #define pseudo-instruction.
The results of this program's execution are as follows:
Execution results
100 50
In this `#define` pseudo-instruction, X is specified within the parentheses following the name.
"With this, the X in subsequent replacement sentences will be..."
Symbols within parentheses are replaced when using a #define pseudo-instruction.
"In the previous example, it became a scenario where X was replaced with a1 or a2."
As far as I can see from this feature and its usage, it's like a function.
In fact, this feature is used instead of a simple function and is called a macro.
キーワード
【macro】
Using `#define` preprocessor directives for easy expression of formulas and similar constructs.
Macros work exactly like regular functions, but their underlying mechanisms are quite different.
In the case of functions, their actual existence is in one place, and they are called and used when needed.
However, in a macro, the program itself in the location where it's being used is replaced.
It's slightly faster because it doesn't require any setup or overhead.
However, if you create a macro that's too large, it will replace every place where macros are used.
For that reason, the program size can become extremely large.
Therefore, macros are generally used for predefined formulas, among other things.
For example, the following macro calculates the area of a trapezoid.
Source code
#include <stdio.h>
#define GET_TRAPEZOID_AREA(A, B, H) (A + B) * H / 2
int main(void)
{
int up, down, h, s;
printf("上底、下底、height:");
scanf("%d,%d,%d", &up, &down, &h);
s = GET_TRAPEZOID_AREA(up, down, h);
printf("面積:%d\n", s);
return 0;
}
The output of this program is as follows:
Execution results
上底、下底、height:5,10,8 面積:60
This way, you won't have to enter the same formulas repeatedly.
Fear of side effects.
Macros defined with #define are convenient and easy to use, but...
You may encounter unexpected phenomena if you use it incorrectly.
For example, in the program to calculate the area of the trapezoid last time,
Let's consider a scenario where, for some reason, you always have to add +3 to the height.
Here's an example of how I've modified it.
Source code
#include <stdio.h>
#define GET_TRAPEZOID_AREA(A, B, H) (A + B) * H / 2
int main(void)
{
int up, down, h, s;
printf("上底、下底、height:");
scanf("%d,%d,%d", &up, &down, &h);
s = GET_TRAPEZOID_AREA(up, down, h + 3);
printf("面積:%d\n", s);
return 0;
}
The output of this program is as follows:
Execution results
上底、下底、height:5,10,5 面積:76
"The height is increased by +3, so the result should be the same as last time."
For some reason, the answer is showing as 76.
This is because the #define pseudo-instruction is merely a substitution command.
Replacing GET_TRAPEZOID_AREA(up, down, h + 3) with (A + B) * H / 2,
It becomes an expression of (up + down) * h + 3 / 2.
"This will add 3 to the height, which will mess up the calculations."
This is what we call a macro side effect when substitution leads to unexpected calculation results.
There are two ways to solve this.One is to include parentheses when calling .
Source code
GET_TRAPEZOID_AREA(up, down, (h + 3));
"If you put on a facade, a +3 will be added to the height, allowing for correct calculation."
Another approach is to bracket the macro for the person in charge.
Enclose every substitution used in the macro in parentheses, and also enclose the entire macro itself.
"This way, all the numbers are enclosed in parentheses, so it's safe."
However, it's a hassle to be careful when using it, and you might forget.
Therefore, it is generally recommended to avoid excessive use of macros. #define pseudo-instructions should only be used for declaring constants.
It's generally better to use functions whenever possible for calculations like formulas.
About This Site
Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.