1. The function body can directly use a macro to define the left and right values of the operation, no matter whether there is any returned value. See:
Error message:
2. Enclose the function body with parentheses () and use macro definitions to obtain the calculation result of the last statement of the function body. Note that ';' cannot be omitted ';',
As shown in:
The output is as follows:
3. Generally, it is best to use the do {...} while (0) or ({}) format when using multi-statement macro definition.
Because do {...} is used for Branch execution in the if () structure {...} while (0) can be used correctly after calling ';'. Once it is not written, an error will be reported as a normal statement, which is in line with our habits, if only the {} struct is used, ';' cannot be used after the call. If ';' is written according to our habits, an error may occur. for example, if () is followed by an else statement, an error occurs because ';' is inserted on the else as the end of another statement (empty command.
Note: After the do {...} while (0) structure is used, this macro definition cannot be used as the left and right values of the value assignment operation.
The figure is as follows:
The printed result is as follows:
4. method for obtaining the return value defined by a FUNCTION macro
We can see from the above that when we use macro definitions in the form, as long as the result of the last operation on the function body is what we want, we can use this form to correctly obtain the returned value and place it on the right side of the value assignment operation. When we use do {...} in the while (0) macro definition, we cannot obtain the return value through the value assignment operation, but we can add a parameter, do {} while (0) in this example, the value to be put back is assigned to this parameter, so that we can get the desired return value.
The illustration is as follows:
The output is as follows: