The value of do {} while (0) when macro definition (# define) is in C language,

Source: Internet
Author: User

The value of do {} while (0) when macro definition (# define) is in C language,

Recently I found that do {...} is used everywhere in the code of the new company {...} while (0), google, I found that there was a lot of discussion on Stack Overflow, summarized the discussion, and added my understanding, do {...} the value of while (0) is mainly reflected in:

1. Added code Adaptability

The macro definition below does not use do {...} while (0)

#define FOO(x) foo(x); bar(x);

In this macro definition, there will be no problems with separate calls, for example:

FOO(100)

After macro expansion, it becomes:

foo(x);bar(x);

There is no problem in calling FOO in this way, but FOO (x) cannot be put into the control statement, for example

if (condition)    FOO(x);else     ...;

After macro expansion

if (condition)    foo(x);bar(x);else     ...;

This causesSyntax ErrorThe syntax error is not terrible. It can be found at the compilation stage. what's even more fatal is that it may causeLogical errorThe compiler cannot find this error. When this problem occurs, the programmer will be crazy. For example:

if (condition)    FOO(x);

This code is expanded:

if (condition)    foo(x); bar(x);

In this way, bar (x) is called no matter whether the condition is true or false. Have you ever been suffering from this?

At this time, the value of do {...} while (0) is reflected. modify the definition of FOO.

#define FOO(x) do { foo(x); bar(x); } while (0)

In this way, there is no problem in putting FOO into the control statement.

Some people may say: Isn't it okay to enclose foo (x); bar (x) in braces? For example:

#define FOO(x) { foo(x); bar(x); }

Let's look at the following code:

if (condition)    FOO(x);else     ...;

After expansion:

If (condition) {foo (x); bar (x) ;}; // note the last semicolon, syntax error else ...;

Still syntax errors;

2. Added code scalability

I understand the scalability, mainly because other macros can be referenced in the macro definition, such:

#define FOO(x) do{OTHER_FOO(x)} while(0)
In this way, we don't have to worry about whether OTHER_FOO is, but the statement is still consistent with the statement.

3. Added code flexibility

Flexibility is mainly reflected in the following definition:

#define FOO(x)  do{ \    foo(x);  \    if(condition(x)) \        break; \    bar(x) \    ..... \
} while(0)

 

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.