Macro function writing

Source: Internet
Author: User

Macro functions are fast to execute, but occupy a large length of Code and are not easy to debug.

 

Maxcompute macro functions can be written

#define f(a,b)   ((a)+(b))

All the parameters in the macro function should be enclosed in parentheses to ensure that the operations of A and B are completed first. the outermost side of the macro function should be added with parentheses to ensure the overall operation of the macro function.

 

For complex macro functions that require calling other functions, enclose them with braces to avoid statements such as if and only execute the first sentence.

#define F(a,b)  {  \          f(a);          g(b);          }  

The backlash is used to meet the requirements of macro functions defined in a row.

However, another problem occurs here. For some statements, such as if (x) f (a, B) else... only one statement in IF is required. If the preceding format is used

if (x) {    f(a);      f(b);}; else...

An extra semicolon does not conform to the syntax specification. It is obviously two statements that violate the syntax. Therefore, you need to set a statement that can be followed by a semicolon outside the braces. The common one is do {} while (0 );

The final form is:

#define F(a,b)  do{  \          f(a);          g(b);          }while(0)

The expanded form is

if ( x )      do { f(a);    f(b);    } while(0);else
    ...

 

Refer:

Summary of macro c Definition

Http://dxf206.blog.163.com/blog/static/4227861200952511813462/

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.