Snow Innocent
Snow Innocent

Reputation: 147

Why is assert defined as (void)0?

Why #define assert(expression) ((void)0), rather than #define assert(expression) is used in release mode?(strictly speaking, when NDEBUG is defined)

I heard that there are some reasons, but I've forgot it.

Upvotes: 6

Views: 2162

Answers (2)

Jts
Jts

Reputation: 3527

The reason why ((void)0) is used in empty macros is make them behave like a function, in the sense that you need to specify the semicolon ; at the end

For example:

#define assert1(expression) (void)0
     assert(1) // compile error, missing ;

#define assert2(expression) 
     assert(1) // works

Upvotes: 4

anukul
anukul

Reputation: 1952

((void)0) defines assert(expression) to do nothing.
The main reason to use it is that #define assert(expression) would allow assert(expression) to compile without a semicolon but it will not compile if the macro is defined as ((void)0)

Upvotes: 5

Related Questions