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

  • SEARCH
  • Home
  • 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

Related Questions

I have a system with many parameter sets macroized (macros of the form #define
In many of the C and C++ files I have seen macros like this:
Many examples of macros seem to be about hiding lambdas, e.g. with-open-file in CL.
I have an application that writes many times to a formula/macro-laden workbook. It loops
many Places in the sample code i have seen 2 different way of @synthesize
Many ARIA demonstration websites use code such as: <label for=name id=label-name>Your Name</label> <input id=name
When we look at STL header files, we see many macros used where we
I have seen many developers that add various convenience macros to the Prefix.pch of
C# does not support C/C++ macros. There are many Q&A related to this in
Possible Duplicate: Do-While and if-else statements in C/C++ macros gcc (GCC) 4.7.2 c89 Hello,

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.