The preprocessor doesn't understand the sizeof operator, so you can't use it in preprocessor directives (some compilers do support it as an extension, but it's not standard). What you need is a static assertion, but they were only added to the language in C11.
Because sizeof() is calculated after the preprocessor is run, so the information is not available for #if .
The sizeof is a unary operator in C/C++ programming language, it is used to get the occupied size of a variable, constant, expression, data type, etc. It returns an unsigned integral type (size_t).
The sizeof operator yields an integer equal to the size of the specified object or type in bytes. (Strictly, sizeof produces an unsigned integer value whose type, size_t , is defined in the header <stddef. h>.) A sizeof cannot be used in a #if directive, because the preprocessor does not parse type names.
#if sizeof(int) != 4
/* do something */
Using sizeof inside #if
doesn't work while inside #define
it works, why?
#define size(x) sizeof(x)/sizeof(x[0]) /*works*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With