An Effective Method for writing macros

Source: Internet
Author: User

[E liangshi Yiyou network ?] There may be some problems when writing macros. For example, the general goal is to write a macro containing a separate function call statement. This means that the caller needs to provide the final semicolon, while the macro does not. Therefore, the macro cannot be a compound statement enclosed by an arc. If this happens, a syntax error occurs during the call, just as if an else clause is added to the IF branch of the IF/else statement.


Therefore, I personally think that the traditional ending scheme is used like this:
# Define macro (arg1, arg2) do {\
/* Declarations */\
Stmt1 ;\
Stmt2 ;\
/*...*/\
} While (0)/* no ending ;*/
When the caller adds a semicolon, the macro is extended to a separate statement under any circumstances. The optimized compiler removes "invalid" tests or branches with a condition of 0, although lint may warn.

If the statements in the macro body are simple statements without declaration or loop, another technique is to write a separate expression separated by one or more comma operators. This technique can also return a value.

Want to know moreProgramming Language tutorial setYou can log on to e liangshi Yiyou.

 

An Effective Method for writing macros

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.