- Advanced function of clause 1 :# define quasi-instruction
- ..clause 2..: Convenience function of macro
- ..clause 3..: Fear of side effect
Though # define quasi-instruction introduces by paragraph 1 is a quasi-instruction that declares a constant
Actually, an advanced function is had any more.
#Though the constant by the define quasi-instruction has been achieved by a mere replacement
It is also possible to make them do special processing when this is used.
For instance, use the printf sentence to display contents of the variable on the screen as follows.
However, it is necessary to display the value of variable temp here and there.printf("temp = %d\n",temp);
The execution result of this program becomes as follows.#include <stdio.h> #define PRINT_TEMP printf("temp = %d\n",temp) int main(void) { int temp = 100; PRINT_TEMP; return 0; }
The trick of this program is in the content of the definition of # define quasi-instruction.
temp = 100
Though the program becomes cramped when # define quasi-instruction is abused in such a usage
#It is necessary to know as one of the strong functions of the define quasi-instruction.
Though # define quasi-instruction is very strong as seen in the preceding clause
#In the define quasi-instruction, when you specify the character by () after the name
The part of the same alphabet can be replaced by the content that it replaces thereafter.
For instance, the following # define quasi-instruction displays the variable of the specified int type on the screen.
The following program is an example that uses # define quasi-instruction ahead.#define PRINTM(X) printf("%d\n",X)
The execution result of this program becomes as follows.#include <stdio.h> #define PRINTM(X) printf("%d\n",X) int main(void) { int a1 = 100,a2 = 50; PRINTM(a1); PRINTM(a2); return 0; }
In # define quasi-instruction here, X is specified in () after the name.
100
50
This is like the function as long as this function and the usage are seen.
Actually, this function is used for a simple function to change, and called a macro.
The mechanism is greatly different though the usage of the macro is quite the same as a usual function.
[ Macro ]
#Express the expression etc. easily by the replacement by the define quasi-instruction.
However, when you make a too huge macro because all places where the macro is used are replaced
Therefore, the size of the program might become extremely large.
Therefore, the macro generally decides and is used for the cut expression etc.
For instance, the following macro is a macro from which a trapezoid area is requested.
The execution result of this program becomes as follows.#include <stdio.h> #define GET_TRAPEZOID_AREA(A,B,H) (A + B) * H / 2 int main(void) { int up,down,h,s; Printf ("Bottom, lower base, and height on"); scanf("%d,%d,%d",&up,&down,&h); s = GET_TRAPEZOID_AREA(up,down,h); Printf ("Area: %d\n" s); return 0; }
The cut expression need not be decided and be input many times if it does in this manner.
The above bottom, lower base, and height: 5, 10, and 8
Area: 60
#Though the macro by the define quasi-instruction is easy and is convenient
For instance, put it on the program that requests the last trapezoid area.
Think about the case to have always + to do three by thinking about height in some circumstances.
The following program is an example of the change like that.
The execution result of this program becomes as follows.#include <stdio.h> #define GET_TRAPEZOID_AREA(A,B,H) (A + B) * H / 2 int main(void) { int up,down,h,s; Printf ("Bottom, lower base, and height on"); scanf("%d,%d,%d",&up,&down,&h); s = GET_TRAPEZOID_AREA(up,down,h + 3); Printf ("Area: %d\n" s); return 0; }
Though the result is sure to become as well as last time because +3 height is done
The above bottom, lower base, and height: 5 and 10 and 5
Area: 76
Because # define quasi-instruction is only a mere replacement instruction and exists, this :.
GET_TRAPEZOID_AREA(up,down,h + 3) (A + B) * H / When you replace it with two
(up + down) * h + 3/ It becomes an expression called two.
With this, three comes not to join height, and the calculation becomes amusing.
It is called the side effect of the macro to become a numerical result not anticipated in the replacement in this manner.
There are two methods of solving this. One is to apply parentheses at the call.
Because three previously joins height if parentheses are applied, it is possible to calculate normally.GET_TRAPEZOID_AREA(up,down,(h + 3));
Another method is a method of applying parentheses to the macro.
Apply parentheses to all the replacement parts used by the macro, and, in addition, apply it to the entire macro.
Because parentheses have adhered to all numerical values if it does in this manner, it is safe.#define GET_TRAPEZOID_AREA(A,B,H) (((A) + (B)) * (H) / 2)
However, it might be troublesome, and carelessly forget to use it taking care.
Therefore, the macro is assumed that you should not multiuse it so much.
#Use the define quasi-instruction only to declare the constant.
You will use the function as much as possible for the calculation of the expression etc.