0

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.

1
  • 1
    #if INT_MAX == 2147483647 ... remember to #include <limits.h> Commented May 13, 2020 at 16:18

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.