Why use apparently meaningless do-while and if-else statements in macros?
问题 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: 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.