Typical usage and error points of FUNCTION macro definition

Source: Internet
Author: User


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:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.