I define below data type:
typedef int MyInt;
Then I hope to define a new data type based on size of MyInt, something like below code, but I can’t find a solution for it.
#if sizeof(MyInt) == 2
typedef long MyLong;
#else
typedef short MyLong;
#endif
Could anybody help?
It’s not the preprocessor’s job to evaluate
sizeof, that’s done by the compiler which is a later stage in the process. Evaluatingsizeofneeds deep C knowledge that the preprocessor simply doesn’t have.You could (in theory) consider the preprocessor as a separate step, that does text only transforms, in effect converting “foo.c” to “foo-preprocessed.c”. The latter file won’t have any
#includeor#ifs left, they’re all evaluated and replaced by the preprocessor. The actual compiler never sees them.You should consider using
<stdint.h>and the known-precision types (uint16_tand friends).