C++

Asked • 03/27/19

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); } elseI can't see what the `do while` is doing. Why not just write this without it? #define FOO(X) f(X); g(X)

1 Expert Answer

By:

Jesse P. answered • 03/28/19

Tutor
5 (74)

Computer Programming And Math Expert

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.