c – do { … } while (0) — what is it good for?

The Question :

357 people think this question is useful

Possible Duplicate:
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?

I’ve been seeing that expression for over 10 years now. I’ve been trying to think what it’s good for. Since I see it mostly in #defines, I assume it’s good for inner scope variable declaration and for using breaks (instead of gotos.)

Is it good for anything else? Do you use it?

The Question Comments :
  • Have a look at this question.
  • Actually, it is not a duplicate since the linked q/a is not specific to define. It’s easy to compare both answers to state it’s not a duplicate.
  • See “decrement_used_memory” of Redis line 53 [link]github.com/antirez/redis-tools/blob/master/zmalloc.c
  • The duplicate is with the question marked as possible duplicate (first line of the post), not with the question given by Federico A. Ramponi.
  • other variants of this do { ... } while ((void)0, 0) used to silence compiler warnings about “constant condition”.

The Answer 1

522 people think this answer is useful

It’s the only construct in C that you can use to #define a multistatement operation, put a semicolon after, and still use within an if statement. An example might help:

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

if (condition)
else // syntax error here

Even using braces doesn’t help:

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

Using this in an if statement would require that you omit the semicolon, which is counterintuitive:

if (condition)

If you define FOO like this:

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

then the following is syntactically correct:

if (condition)

The Answer 2

119 people think this answer is useful

It is a way to simplify error checking and avoid deep nested if’s. For example:

do {
  // do something
  if (error) {
  // do something else
  if (error) {
  // etc..
} while (0);

The Answer 3

88 people think this answer is useful

It helps to group multiple statements into a single one so that a function-like macro can actually be used as a function. Suppose you have:

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

and you do:

void foobar(int n) {
  if (n)

then this expands to:

void foobar(int n) {
  if (n)

Notice that the second call bar(n) is not part of the if statement anymore.

Wrap both into do { } while(0), and you can also use the macro in an if statement.

The Answer 4

20 people think this answer is useful

It is interesting to note the following situation where the do {} while (0) loop won’t work for you:

If you want a function-like macro that returns a value, then you will need a statement expression: ({stmt; stmt;}) instead of do {} while(0):

#include <stdio.h>

#define log_to_string1(str, fmt, arg...) \
    do { \
        sprintf(str, "%s: " fmt, "myprog", ##arg); \
    } while (0)

#define log_to_string2(str, fmt, arg...) \
    ({ \
        sprintf(str, "%s: " fmt, "myprog", ##arg); \

int main() {
        char buf[1000];
        int n = 0;

        log_to_string1(buf, "%s\n", "No assignment, OK");

        n += log_to_string1(buf + n, "%s\n", "NOT OK: gcc: error: expected expression before 'do'");

        n += log_to_string2(buf + n, "%s\n", "This fixes it");
        n += log_to_string2(buf + n, "%s\n", "Assignment worked!");
        printf("%s", buf);
        return 0;

The Answer 5

-5 people think this answer is useful

Generically, do/while is good for any sort of loop construct where one must execute the loop at least once. It is possible to emulate this sort of looping through either a straight while or even a for loop, but often the result is a little less elegant. I’ll admit that specific applications of this pattern are fairly rare, but they do exist. One which springs to mind is a menu-based console application:

do {
    char c = read_input();

} while (c != 'Q');


Add a Comment