Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizeof() is not executed by preprocessor

Tags:

People also ask

Is sizeof a preprocessor?

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.

Can I use sizeof in #if?

Because sizeof() is calculated after the preprocessor is run, so the information is not available for #if .

What is sizeof () in C macro?

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).

Can sizeof be used in macro?

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*/