About the definition of a macro function

Source: Internet
Author: User

I used to think that the definition of macro is very simple, there is no long macro function, the most commonly used is the definition of Max (), when writing a macro function today, relative to Max (), A little bit longer, and then stepped on a little hole.

At the beginning of the definition of the macro function, for the convenience of writing, a function is written in a few lines, but not connected with the connection symbol, similar to the following

1 #define CHECK (ret) ({2                  int  ret; 3                  ... 4 })

GCC always suggests that RET is not in range. Thought for a long time did not understand how the matter.

Then I found a problem.

My vim defines the macro function when #define那一行是显示的蓝色, but my int ret, and below are all plain white, and then I looked at my previous code and found that my macro definition of Vim was shown in blue.

What I'm thinking right now is my definition of the INT ret below is not included in the macro, this macro, defined as CHECK (ret) = = ({; The empty is outside the main function, this of course will error, and then I used the connection symbol, the code is as follows

1 #define CHECK (ret) ({  2                 int  ret;                     3 })

Ok!!!

The code in VIM is all blue, so it recognizes the entire macro.

Think carefully, the definition of macro is how to end, the end of the macro is no semicolon, then infer that the macro is through the carriage return to end, that is, the whole line is a macro, so when the macro is very long, it is necessary to use the connector to connect.

This inference is also proved by the above error.

This hole is not big, but it also explains from the side, write the procedure, is a careful work. This small problem, don't do it again!

Also, there is no space behind the connector, and GCC warns:

Warning:backslash and newline separated by space

The explanations from the IBM Web site are:

Solution:the backslash (\) character is used as the continuation character to continue #define statements and strings to The next line. GCC expects the backslash character to being the very last character on the line. This warning indicates, there is a space after the backslash. Delete the space and any other characters this come after the backslash.

About the definition of a macro function

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.