C language learned by suffering
C language learned by suffering
Realization of simple functions
Advanced features of #define pseudo instruction
The #define pseudo-instruction introduced in Section 1 is a pseudo-instruction that declares a constant.
In fact, it has more advanced features than that.
Constants by #define pseudo-instructions are realized by mere substitution, but
This can be used to have special processing performed.
For example, to display the contents of a variable on the screen, use the printf statement as follows
However, if you need to display the value of the variable temp here and there, you can use the
If it is tedious to type this statement each time, it can be replaced by the #define pseudo instruction.
The result of executing this program will be as follows
The trick to this program is in the content of the #define pseudo instruction definition.
The function of the #define pseudo instruction is simply to replace, and it can replace anything, even if it is not a number.
Here, PRINT_TEMP is replaced directly by a printf statement that prints the variable.
In other words, from a programmatic standpoint, it is exactly the same as writing a printf statement in that section.
This is a very powerful feature that can forcefully bring any program together.
However, if you abuse the above techniques, your program will be full of abbreviations and
It must be used with great care, as it will only be readable by the person who wrote the program in the first place.
In fact, it has more advanced features than that.
Constants by #define pseudo-instructions are realized by mere substitution, but
This can be used to have special processing performed.
For example, to display the contents of a variable on the screen, use the printf statement as follows
Specify a numerical value for enum
printf("temp = %d\n", temp);
However, if you need to display the value of the variable temp here and there, you can use the
If it is tedious to type this statement each time, it can be replaced by the #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 result of executing this program will be as follows
Execution Result
temp = 100
The trick to this program is in the content of the #define pseudo instruction definition.
The function of the #define pseudo instruction is simply to replace, and it can replace anything, even if it is not a number.
Here, PRINT_TEMP is replaced directly by a printf statement that prints the variable.
In other words, from a programmatic standpoint, it is exactly the same as writing a printf statement in that section.
This is a very powerful feature that can forcefully bring any program together.
However, if you abuse the above techniques, your program will be full of abbreviations and
It must be used with great care, as it will only be readable by the person who wrote the program in the first place.
Simple function called macro
As we saw in the previous section, the #define pseudo instruction is very powerful, but
In fact, the #define pseudo instruction has an even more powerful feature.
It is possible to create simple functions with #define pseudo instructions.
In the #define pseudo-instruction, you can specify a letter in () after the name
The same alphabetical part can be replaced by the content to be replaced thereafter.
For example, the following #define pseudo instruction displays a variable of the specified int type on the screen.
The following program is an example of using the #define pseudo-instruction described earlier.
The result of executing this program will be as follows
In the #define pseudo instruction here, X is specified in the () after the name.
This allows X in subsequent replacement statements to be
The symbol in () will be replaced by the symbol in () when the #define pseudo instruction is used.
In the previous example, X has been replaced by a1 or a2.
In fact, this function is used instead of a simple function and is called a macro.
The usage of macros is exactly the same as for ordinary functions, but the mechanism is very different.
In the case of a function, its reality is in one place and it is called and used when needed.
With macros, however, the program itself replaces the program in the location where it is being used.
This is slightly faster because there is no need to make calls, etc.
However, since all the places where macros are used are replaced, if you make too huge a macro, it may be difficult to get it to work.
This can result in an extremely large program size.
For this reason, macros are generally used for routine formulas and the like.
For example, the following macro finds the area of a trapezoid.
The result of executing this program is as follows
In this way, there is no need to repeatedly type in a formula that has become a cliché.
In fact, the #define pseudo instruction has an even more powerful feature.
It is possible to create simple functions with #define pseudo instructions.
In the #define pseudo-instruction, you can specify a letter in () after the name
The same alphabetical part can be replaced by the content to be replaced thereafter.
For example, the following #define pseudo instruction displays a variable of the specified int type on the screen.
source code
#define PRINTM(X) printf("%d\n", X)
The following program is an example of using the #define pseudo-instruction described earlier.
Source code
#include <stdio.h>
#define PRINTM(X) printf("%d\n", X)
int main(void)
{
int a1 = 100, a2 = 50;
PRINTM(a1);
PRINTM(a2);
return 0;
}
The result of executing this program will be as follows
Execution Result
100
50
50
In the #define pseudo instruction here, X is specified in the () after the name.
This allows X in subsequent replacement statements to be
The symbol in () will be replaced by the symbol in () when the #define pseudo instruction is used.
In the previous example, X has been replaced by a1 or a2.
As far as this function and its usage is concerned, this is just like a function.
In fact, this function is used instead of a simple function and is called a macro.
Keywords.
[Macro
#defineA simple expression of an expression, etc., by substitution with a pseudo-instruction.
The usage of macros is exactly the same as for ordinary functions, but the mechanism is very different.
In the case of a function, its reality is in one place and it is called and used when needed.
With macros, however, the program itself replaces the program in the location where it is being used.
This is slightly faster because there is no need to make calls, etc.
However, since all the places where macros are used are replaced, if you make too huge a macro, it may be difficult to get it to work.
This can result in an extremely large program size.
For this reason, macros are generally used for routine formulas and the like.
For example, the following macro finds 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("Top bottom, bottom bottom, height:");
scanf("%d,%d,%d", &up, &down, &h);
s = GET_TRAPEZOID_AREA(up, down, h);
printf("Area:%d\n", s);
return 0;
}
The result of executing this program is as follows
Execution Result
Upper bottom, lower bottom, height: 5,10,8
Area: 60
Area: 60
In this way, there is no need to repeatedly type in a formula that has become a cliché.
Fear of side effects
Macros with #define pseudo-instructions are easy and convenient, but
If used incorrectly, you may encounter unexpected phenomena.
For example, in the previous program for finding the area of a trapezoid
Consider the case where, for some reason, the height must always be +3.
The following program is an example of such a change.
The result of executing this program is as follows
The height is +3, so the result should be the same as before.
For some reason, the answer is displayed as 76.
This is because the #define pseudo-instruction is just a replacement instruction.
Replacing GET_TRAPEZOID_AREA(up,down,h + 3) with (A + B) * H / 2
The expression would be (up + down) * h + 3 / 2.
This would cause the calculation to be incorrect, since 3 is no longer added to the height.
This unexpected result of substitution is called a side effect of the macro.
There are two ways to solve this: one is to add parentheses to the call.
and if you add parentheses, 3 will be added to the height first, so it can be calculated normally.
The other method is to put parentheses on the macro.
Put parentheses around all replacements used in the macro, and also around the macro as a whole.
If you do it this way, all the numbers will be in parentheses and you should be fine.
However, it is tedious to use with care, and you may inadvertently forget to do so.
For this reason, macros should not be used too frequently.
The #define pseudo instruction should only be used to declare constants and should not be used for
It is better to use functions for mathematical expressions and other calculations whenever possible.
If used incorrectly, you may encounter unexpected phenomena.
For example, in the previous program for finding the area of a trapezoid
Consider the case where, for some reason, the height must always be +3.
The following program is an example of such a change.
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("Top bottom, bottom bottom, height:");
scanf("%d,%d,%d", &up, &down, &h);
s = GET_TRAPEZOID_AREA(up, down, h + 3);
printf("Area:%d\n", s);
return 0;
}
The result of executing this program is as follows
Execution Result
Upper bottom, lower bottom, height: 5,10,5
Area: 76
Area: 76
The height is +3, so the result should be the same as before.
For some reason, the answer is displayed as 76.
This is because the #define pseudo-instruction is just a replacement instruction.
Replacing GET_TRAPEZOID_AREA(up,down,h + 3) with (A + B) * H / 2
The expression would be (up + down) * h + 3 / 2.
This would cause the calculation to be incorrect, since 3 is no longer added to the height.
This unexpected result of substitution is called a side effect of the macro.
There are two ways to solve this: one is to add parentheses to the call.
Source code
GET_TRAPEZOID_AREA(up, down, (h + 3));
and if you add parentheses, 3 will be added to the height first, so it can be calculated normally.
The other method is to put parentheses on the macro.
Put parentheses around all replacements used in the macro, and also around the macro as a whole.
Source code
#define GET_TRAPEZOID_AREA(A, B, H) (((A) + (B)) * (H) / 2)
If you do it this way, all the numbers will be in parentheses and you should be fine.
However, it is tedious to use with care, and you may inadvertently forget to do so.
For this reason, macros should not be used too frequently.
The #define pseudo instruction should only be used to declare constants and should not be used for
It is better to use functions for mathematical expressions and other calculations whenever possible.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.