Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 55577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:23:57+00:00 2026-05-10T17:23:57+00:00

In many C/C++ macros I’m seeing the code of the macro wrapped in what

  • 0

In many C/C++ macros I’m seeing the code of the macro wrapped in what seems like a meaningless do while loop. Here are examples.

#define FOO(X) do { f(X); g(X); } while (0) #define FOO(X) if (1) { f(X); g(X); } else 

I can’t see what the do while is doing. Why not just write this without it?

#define FOO(X) f(X); g(X) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:23:58+00:00Added an answer on May 10, 2026 at 5:23 pm

    The do ... while and if ... else are there to make it so that a semicolon after your macro always means the same thing. Let’s say you had something like your second macro.

    #define BAR(X) f(x); g(x) 

    Now if you were to use BAR(X); in an if ... else statement, where the bodies of the if statement were not wrapped in curly brackets, you’d get a bad surprise.

    if (corge)   BAR(corge); else   gralt(); 

    The above code would expand into

    if (corge)   f(corge); g(corge); else   gralt(); 

    which is syntactically incorrect, as the else is no longer associated with the if. It doesn’t help to wrap things in curly braces within the macro, because a semicolon after the braces is syntactically incorrect.

    if (corge)   {f(corge); g(corge);}; else   gralt(); 

    There are two ways of fixing the problem. The first is to use a comma to sequence statements within the macro without robbing it of its ability to act like an expression.

    #define BAR(X) f(X), g(X) 

    The above version of bar BAR expands the above code into what follows, which is syntactically correct.

    if (corge)   f(corge), g(corge); else   gralt(); 

    This doesn’t work if instead of f(X) you have a more complicated body of code that needs to go in its own block, say for example to declare local variables. In the most general case the solution is to use something like do ... while to cause the macro to be a single statement that takes a semicolon without confusion.

    #define BAR(X) do { \   int i = f(X); \   if (i > 4) g(i); \ } while (0) 

    You don’t have to use do ... while, you could cook up something with if ... else as well, although when if ... else expands inside of an if ... else it leads to a ‘dangling else‘, which could make an existing dangling else problem even harder to find, as in the following code.

    if (corge)   if (1) { f(corge); g(corge); } else; else   gralt(); 

    The point is to use up the semicolon in contexts where a dangling semicolon is erroneous. Of course, it could (and probably should) be argued at this point that it would be better to declare BAR as an actual function, not a macro.

    In summary, the do ... while is there to work around the shortcomings of the C preprocessor. When those C style guides tell you to lay off the C preprocessor, this is the kind of thing they’re worried about.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer .... but how do we find the equivalent class in… May 11, 2026 at 2:16 pm
  • added an answer Generally speaking, if you need precise control, your best bet… May 11, 2026 at 2:16 pm
  • added an answer You need to also install cygrunsrv so you can set… May 11, 2026 at 2:16 pm

Related Questions

In many C/C++ macros I'm seeing the code of the macro wrapped in what
I've been programming in C,C++,C# and a few other languages for many years, mainly
See: Understanding Pointers In many C flavoured languages, and some older languages like Fortran,
I've noticed in many places in Java (C# included), that many getter methods are
In a recent question on stubbing, many answers suggested C# interfaces or delegates for
What logging library or approach would you recommend for this case: We want to
Most mature C++ projects seem to have an own reflection and attribute system ,
I just finished setting up an out-of-place build system for our existing C++ code
I am developing a TCP/IP client that has to deal with a proprietary binary

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.