Site Tools


c-do-while-0

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
c-do-while-0 [August 22, 2026 at 16:03] – Ivan Janevskic-do-while-0 [August 26, 2026 at 11:41] (current) – external edit 127.0.0.1
Line 1: Line 1:
 # C do-while-0 # C do-while-0
  
-**[do { ... } while (0)](https://stackoverflow.com/questions/2381300/what-does-do-while-0-do-exactly-in-kernel-code)** is a C idiom that executes a block exactly once while appearing syntactically like a loop. Compilers optimize it away completely, producing no extra machine code. It's commonly used in [[c-function-macro|function macros]] to safely wrap multiple statements and enforce semicolon syntax.+**C do-while-0** is an idiom using `do { ... } while(0)` to create a single-iteration loop that allows `break` to exit cleanly. It's used in macros to ensure multiple statements execute as a unit, or to allow early exit without goto or nested conditionals.
  
-Use `do-while(0)` in macros to enable safe statement grouping and prevent control flow issues.+Use do-while-0 in macros to wrap multiple statements safely, preventing issues when macros are used in conditionals or with semicolons.
  
 ## Example ## Example
  
-This example shows do-while-0 wrapping multiple statements in a function macro.+This example shows do-while-0 fixing common macro pitfalls.
  
 ```c ```c
-// compile: gcc -o dowhile dowhile.c +// compile: gcc -o do_while do_while.c 
-// run: ./dowhile +// run: ./do_while 
-// description: do-while-0 enables safe multi-statement function macros+// description: do-while-0 macro pattern
  
 #include <stdio.h> #include <stdio.h>
 +#include <stdlib.h>
  
-#define LOG_ERROR(fmt, ...) do { \ +#define LOG_SAFE(msg) \ 
-    fprintf(stderr, "[ERROR] " fmt "\n", ##__VA_ARGS__); \ +    do { \ 
-    perror("System error"); \ +        printf("LOG: %s\n", msg); \ 
-} while (0)+        fflush(stdout); \ 
 +    } while(0) 
 + 
 +#define VALIDATE(ptr) \ 
 +    do { \ 
 +        if ((ptr) == NULL) { \ 
 +            fprintf(stderr, "Validation failed\n"); \ 
 +            break; \ 
 +        } \ 
 +        printf("Validation passed\n"); \ 
 +    } while(0)
  
 int main() { int main() {
-    if (1) { +    if (1) 
-        LOG_ERROR("Failed to open file"); +        LOG_SAFE("Message");
-    }+
          
 +    int *arr = malloc(100);
 +    VALIDATE(arr);
 +    
 +    free(arr);
     return 0;     return 0;
 } }
 ``` ```
 +
 +## Common patterns
 +
 +**Why do-while-0 is needed**:
 +- Wraps multiple statements into single unit
 +- Allows `break` for early exit without goto
 +- Handles trailing semicolon safely
 +
 +**Semicolon handling**:
 +- User writes `MACRO;` with semicolon
 +- Macro ends with `while(0)`, not `while(0);`
 +- Extra semicolon is benign
 +
 +**Compiler optimization**:
 +- Compiler removes the loop (condition always false)
 +- Zero runtime overhead
 +- Same as inline { } block
 +
 +**When to use**:
 +- Multi-statement macros in conditionals
 +- Cleanup/error handling in macros
 +- Backward-compatible C code
  
c-do-while-0.1787414631.md.gz · Last modified: by Ivan Janevski