how to define an 8 bit integer

Discussion in 'C Programming' started by DanielJohnson, Oct 25, 2008.

  1. Yes, #if/#error/#endif is better when you can use preprocessor constants
    for your check. Like in this case, since sizeof(char) can be dropped.

    If you want to check e.g. sizeof or enum values at compile time, you
    need to force a constraint violation after preprocessing. Or two
    violations, since some compilers are sloppy about checking them. I
    prefer to macroize that to make the actual assertion readable:

    #define static_assert(name, c) \
    typedef struct { int Assert_##name: 2-4*(c); } Assert_##name[2-4*(c)]

    /* TODO: Port this to saner assumptions */
    static_assert(supported_atomic, sizeof(sig_atomic_t) * CHAR_BIT >= 16);
     
    Hallvard B Furuseth, Oct 28, 2008
    #21
    1. Advertisements

  2. Of course, it would have helped if I didn't invert the test by trying to
    make the typedef a one-liner.

    BTW, the 2 and 4 is just because I have no idea if compilers can be
    relied on to handle 'int foo:1;' well when int bitfields are signed.
     
    Hallvard B Furuseth, Oct 28, 2008
    #22
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.