2 out of 3 rated this helpful - Rate this topic

C_ASSERT macro

Applies to: desktop apps only

Checks assertions at compile time.

Syntax

void C_ASSERT(
   expr
);

Parameters

expr

An expression that can be determined at compile time.

Return value

If the assertion succeeds, the expression evaluates to typedef char __C_ASSERT__[1];, which the compiler will accept.

If the assertion fails, the expression evalues to typedef char __C_ASSERT__[-1];, which results in a compilation error because negative subscripts are not valid.

Remarks

The C_ASSERT macro is defined as follows.

#define C_ASSERT(e) typedef char __C_ASSERT__[(e)?1:-1]

The following examples demonstrate common types of compile-time assertions.

C_ASSERT (BUFFER_CCH_SIZE <= MAX_PATH);

C_ASSERT (ARRAYSIZE(array1) == ARRAYSIZE(array2));

C_ASSERT (FIELD_OFFSET(STRUCT_DEF, MemberName) == 0x1d4);

C_ASSERT (sizeof(BOOLEAN) == sizeof(UCHAR));

Requirements

Header

Winnt.h

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
This macro won't allow using it more than once; There is a better way
Use the following:

#define C_ASSERT(e) extern char __CASSERT__[e]

This can be used as many times as you want!