How to Declare a 32-bit Integer in C

CPortability

C Problem Overview


What's the best way to declare an integer type which is always 4 byte on any platforms? I don't worry about certain device or old machines which has 16-bit int.

C Solutions


Solution 1 - C

#include <stdint.h>

int32_t my_32bit_int;

Solution 2 - C

C doesn't concern itself very much with exact sizes of integer types, C99 introduces the header stdint.h , which is probably your best bet. Include that and you can use e.g. int32_t. Of course not all platforms might support that.

Solution 3 - C

Corey's answer is correct for "best", in my opinion, but a simple "int" will also work in practice (given that you're ignoring systems with 16-bit int). At this point, so much code depends on int being 32-bit that system vendors aren't going to change it.

(See also why long is 32-bit on lots of 64-bit systems and why we have "long long".)

One of the benefits of using int32_t, though, is that you're not perpetuating this problem!

Solution 4 - C

You could hunt down a copy of Brian Gladman's brg_types.h if you don't have stdint.h.

brg_types.h will discover the sizes of the various integers on your platform and will create typedefs for the common sizes: 8, 16, 32 and 64 bits.

Solution 5 - C

You need to include inttypes.h instead of stdint.h because stdint.h is not available on some platforms such as Solaris, and inttypes.h will include stdint.h for you on systems such as Linux. If you include inttypes.h then your code is more portable between Linux and Solaris.

This link explains what I'm saying: HP link about inttypes.h

And this link has a table showing why you don't want to use long or int if you have an intention of a certain number of bits being present in your data type. IBM link about portable data types

Solution 6 - C

C99 or later

Use <stdint.h>.

If your implementation supports 2's complement 32-bit integers then it must define int32_t.

If not then the next best thing is int_least32_t which is an integer type supported by the implementation that is at least 32 bits, regardless of representation (two's complement, one's complement, etc.).

There is also int_fast32_t which is an integer type at least 32-bits wide, chosen with the intention of allowing the fastest operations for that size requirement.

ANSI C

You can use long, which is guaranteed to be at least 32-bits wide as a result of the minimum range requirements specified by the standard.

If you would rather use the smallest integer type to fit a 32-bit number, then you can use preprocessor statements like the following with the macros defined in <limits.h>:

#define TARGET_MAX 2147483647L

#if   SCHAR_MAX >= TARGET_MAX
  typedef signed char int32;
#elif SHORT_MAX >= TARGET_MAX
  typedef short int32;
#elif INT_MAX   >= TARGET_MAX
  typedef int int32;
#else
  typedef long int32;
#endif

#undef TARGET_MAX

Solution 7 - C

If stdint.h is not available for your system, make your own. I always have a file called "types.h" that have typedefs for all the signed/unsigned 8, 16, and 32 bit values.

Solution 8 - C

also depending on your target platforms you can use autotools for your build system

it will see if stdint.h/inttypes.h exist and if they don't will create appropriate typedefs in a "config.h"

Solution 9 - C

stdint.h is the obvious choice, but it's not necessarily available.

If you're using a portable library, it's possible that it already provides portable fixed-width integers. For example, SDL has Sint32 (S is for “signed”), and GLib has gint32.

Solution 10 - C

You can declare 32 bits with signed or unsigned long.

int32_t variable_name;
uint32_t variable_name;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZZ CoderView Question on Stackoverflow
Solution 1 - CCorey DView Answer on Stackoverflow
Solution 2 - CnosView Answer on Stackoverflow
Solution 3 - CBrooks MosesView Answer on Stackoverflow
Solution 4 - CcfrantzView Answer on Stackoverflow
Solution 5 - CCutlasjView Answer on Stackoverflow
Solution 6 - CVeltasView Answer on Stackoverflow
Solution 7 - CRobert DemlView Answer on Stackoverflow
Solution 8 - CSpudd86View Answer on Stackoverflow
Solution 9 - CBastien LéonardView Answer on Stackoverflow
Solution 10 - CmotoView Answer on Stackoverflow