do {} while(0) what?

A recent check in of mine raised a few eye brows during reviews.  I checked in a few macros which ended with/contained a "do{}while(0)" and people were curious as to why. 

In my experience there are two main uses for it. 

  1. Insert an empty statement with no runtime penalty

  2. Group a set of statements into a single statement followable by a semi-colon

    1. do { Func1(); Func2() } while(0)

Macros are mutating little constructs which jump back and forth depending on the compile time options.  Many macros compile to different expressions based on the options and in some cases they compile to nothing.  This is where #1 really comes in hand.  Imagine you have the following code. 


Compiling MY_MACRO to nothing will cause at the least a compile time warning since you will have essentially if(someCondition);. Instead you can use the do{}while(0) trick.

#if CONST_1
#define MY_MACRO() Func1()
#define MY_MACRO() do{}while(0)

Another us is for stylistic reasons.  Some camps don't believe a ; should be used as a empty statement and use do{}while(0) instead.  I tend to agree.  Mainly because one of my early C professors had the same belief.  The habit stuck around.  

Update: Changed incorrect use of word expression -> single statement

Comments (3)

  1. IMHO do{ Func1(); Func2() }while(0) is a kind of perversion. You can use {} instead.

    #if CONST_1

    #define MY_MACRO() Func1()


    #define MY_MACRO() { }


    And I can’t see any reason why { } is worse than do{}while(0), isn’t it?

  2. Thomas says:

    My experience:

    Some developers use do {} while (0) expressions to use "break" like a goto. (Which is terrible for me)

    For example:



     if (something) break;




  3. Alexander,

    Updated: Removed my incorrect assertion that do{}while(0) is an expression.  Don’t post before you finish your coffee.

    The only real difference between the two is style.  

Skip to main content