Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is int in C Always 32-bit?

Tags:

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 this is true.

Do you know any modern platforms with int of a different size? Ignore dinosaur platforms with 8-bit or 16-bit architectures.

NOTE: I already know how to declare a 32-bit integer from the other question. This one is more like a survey to find out which platforms (CPU/OS/Compiler) supporting integers with other sizes.

like image 844
ZZ Coder Avatar asked Aug 05 '09 04:08

ZZ Coder


People also ask

Are ints 32 or 64-bit?

int is 32 bits in size. long , ptr , and off_t are all 64 bits (8 bytes) in size.

Is 16 bit int in C?

The C standard provides guidelines specifying the minimum allowable size for each basic data type. The minimum range for both short and int is –32,767 to 32,767, corresponding to a 16-bit unit, and the minimum range for long is –2,147,483,647 to 2,147,483,647, corresponding to a 32-bit unit.

Can int store 32-bit?

Unsigned int data type in C++ is used to store 32-bit integers.


2 Answers

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 :-)

like image 133
David Claridge Avatar answered Sep 30 '22 00:09

David Claridge


"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.
like image 28
paxdiablo Avatar answered Sep 29 '22 22:09

paxdiablo