Getting variable size in C preprocessor

i have an problem, i am writing a header file, which depending on the Sizes of the c standard datatypes, defines different Structs and Unions. I would need something like this:

#if sizeof(int) == 4
    typedef struct {
        int i;
    } test;
#else
    typedef struct {
        long i;
    } test;
#endif

sadly this doesnt work.

Does somebody know, how i can do this?

Thanks for your help.

Upvotes: 0

Views: 246

Answers (2)

0___________
0___________

Reputation: 67476

You cant do it this way as preprocessor does not know anything about the C language, but for this purpose you have fixed size integer types which are standard and portable

typedef struct {
    int32_t i;
} test;

Upvotes: 2

datenwolf
datenwolf

Reputation: 162164

Strictly speaking, you can't. What you can do however is take "well known" preprocessor definitions as hints to decide which architecture, compiler and OS you're targeting and decide from that.

Upvotes: 0

Related Questions