Is int in C Always 32-bit?

后端 未结 8 1982
故里飘歌
故里飘歌 2020-12-05 05:17

This is related to following question,

How to Declare a 32-bit Integer in C

Several people mentioned int is always 32-bit on most platforms. I am curious if

相关标签:
8条回答
  • 2020-12-05 05:33

    "is always 32-bit on most platforms" - what's wrong with that snippet? :-)

    The C standard does not mandate the sizes of many of its integral types. It does mandate relative sizes, for example, sizeof(int) >= sizeof(short) and so on. It also mandates minimum ranges but allows for multiple encoding schemes (two's complement, ones' complement, and sign/magnitude).

    If you want a specific sized variable, you need to use one suitable for the platform you're running on, such as the use of #ifdef's, something like:

    #ifdef LONG_IS_32BITS
        typedef long int32;
    #else
        #ifdef INT_IS_32BITS
            typedef int int32;
        #else
            #error No 32-bit data type available
        #endif
    #endif
    

    Alternatively, C99 and above allows for exact width integer types intN_t and uintN_t:


    1. The typedef name intN_t designates a signed integer type with width N, no padding bits, and a two's complement representation. Thus, int8_t denotes a signed integer type with a width of exactly 8 bits.
    2. The typedef name uintN_t designates an unsigned integer type with width N. Thus, uint24_t denotes an unsigned integer type with a width of exactly 24 bits.
    3. These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two's complement representation, it shall define the corresponding typedef names.
    0 讨论(0)
  • 2020-12-05 05:33

    It vastly depends on your compiler. Some compile them as 64-bit on 64-bit machines, some compile them as 32-bit. Embedded systems are their own little special ball of wax.

    Best thing you can do to check:

    printf("%d\n", sizeof(int));
    

    Note that sizeof will print out bytes. Do sizeof(int)*CHAR_BIT to get bits.

    Code to print the number of bits for various types:

    #include <limits.h>
    #include <stdio.h>
    
    int main(void) {
        printf("short is %d bits\n",     CHAR_BIT * sizeof( short )   );
        printf("int is %d bits\n",       CHAR_BIT * sizeof( int  )    );
        printf("long is %d bits\n",      CHAR_BIT * sizeof( long )    );
        printf("long long is %d bits\n", CHAR_BIT * sizeof(long long) );
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-05 05:34

    No. Small embedded systems use 16 bit integers.

    0 讨论(0)
  • 2020-12-05 05:42

    As several people have stated, there are no guarantees that an 'int' will be 32 bits, if you want to use variables of a specific size, particularly when writing code that involves bit manipulations, you should use the 'Standard Integer Types' mandated by the c99 specification.

    int8_t
    uint8_t
    int32_t
    uint32_t
    

    etc...

    they are generally of the form [u]intN_t, where the 'u' specifies that you want an unsigned quantity, and N is the number of bits

    the correct typedefs for these should be available in stdint.h on whichever platform you are compiling for, using these allows you to write nice, portable code :-)

    0 讨论(0)
  • 2020-12-05 05:46

    TI are still selling OMAP boards with the C55x DSPs on them, primarily used for video decoding. I believe the supplied compiler for this has a 16 bit int. It is hardly dinosaur (the Nokia 770 was released in 2005), although you can get 32 bit DSPs.

    Most code you write, you can safely assume it won't ever be run on a DSP. But perhaps not all.

    0 讨论(0)
  • 2020-12-05 05:47

    Well, most ARM-based processors can run Thumb code, which is a 16-bit mode. That includes the yet-only-rumored Android notebooks and the bleeding-edge smartphones.

    Also, some graphing calculators use 8-bit processors, and I'd call those fairly modern as well.

    0 讨论(0)
提交回复
热议问题