Linked Questions
164 questions linked to/from Why use apparently meaningless do-while and if-else statements in macros?
457
votes
5
answers
218k
views
do { ... } while (0) — what is it good for? [duplicate]
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 ...
gilm's user avatar
- 8,110
242
votes
1
answer
155k
views
C multi-line macro: do/while(0) vs scope block [duplicate]
Possible Duplicates:
What’s the use of do while(0) when we define a macro?
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?
do { … } while (0) ...
151
votes
2
answers
66k
views
What's the use of do while(0) when we define a macro? [duplicate]
Possible Duplicates:
Do-While and if-else statements in C/C++ macros
do { ... } while (0) — what is it good for?
I'm reading the linux kernel and I found many macros like this:
#define ...
45
votes
7
answers
25k
views
do while(false) pattern [duplicate]
Possible Duplicate:
Why use apparently meaningless do-while and if-else statements in macros?
Why is the do while(false) necessary in the macros below?
#define LOG(message, ...) \
do { \
Lock<...
26
votes
6
answers
9k
views
What does "do { ... } while (0)" do exactly in kernel code? [duplicate]
Possible Duplicates:
What’s the use of do while(0) when we define a macro?
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?
C multi-line macro: do/...
29
votes
3
answers
26k
views
Why use do { } while (0) in macro definition? [duplicate]
Possible Duplicate:
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?
I met code like below:
#define ev_io_init(ev,cb,fd,events) \
do { \
ev_init ((ev), (cb));...
13
votes
4
answers
11k
views
C: do {...} while(0)? [duplicate]
Possible Duplicates:
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?
do { … } while (0) what is it good for?
I'm working on some C code filled with ...
igul222's user avatar
- 8,657
5
votes
5
answers
3k
views
do { } while(0) vs. if (1) { } in macros [duplicate]
Possible Duplicate:
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?
When one needs to execute multiple statements within preprocessor macro, it's usually ...
7
votes
3
answers
5k
views
function like macro curly braces or do..while [duplicate]
Possible Duplicate:
Do-While and if-else statements in C/C++ macros
gcc (GCC) 4.7.2
c89
Hello,
I have the following function-like macro and just wondering what is the preferred usage when using ...
ant2009's user avatar
- 22.7k
6
votes
3
answers
1k
views
Why `do { ...; exit(...); } while (0)` in C? [duplicate]
As a C newbie I'm having trouble understanding the following code:
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
I gathered that the reason ...
1
vote
3
answers
2k
views
Use cases of do-while-false in C? [duplicate]
Lately, I see more and more code pieces that use the template of:
do {
... some code ...
} while (false);
Why to do this? If I'm not mistaken, the code will run only one time so the loop is seem ...
1
vote
2
answers
1k
views
Embedded C programming style [duplicate]
I have noticed a programming style in Embedded C, used for firmware programming:
#define WRITE_REGISTER(reg, value) \
do { \
write_to_register(reg, value); \
} while (0)
...
2
votes
2
answers
473
views
Why "do ... while (0)" can't be replaced by simple curly brackets? [duplicate]
I recently saw a piece of C code including a macro of the following style:
#define TOTO() \
do { \
do_something(); \
do_another_something(); \
} ...
3
votes
3
answers
987
views
Significance of do{} while(0) [duplicate]
What is the significance of do while loop when the condition inside the while loop is 0 i.e. always false.
do
{
//some code implementation.
}while(0);
I have seen at many places this is being used. ...
Aragon's user avatar
- 1,551
1
vote
2
answers
2k
views
Multiple statements in one macro [duplicate]
Is it correct way to write a multiple statements in one Macro?
#define AB() do { printf("hi"); } while(0)
msc's user avatar
- 35.1k