tags:

views:

2977

answers:

8

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.

+32  A: 
#include <stdint.h>

int32_t my_32bit_int;
Corey D
Just to note intN_t (and uintN_t) is optional in terms of standard. It is required to be defined if and only if the the system has types that meet the requirement.
KTC
That's what you want though. If the code really requires a 32-bit int, and you try to compile it on a platform that doesn't support them, you *want* the compiler to punt back to the developer. Having it pick some other size and go on would be horrible.
T.E.D.
Note that the header '`<inttypes.h>`' is explicitly documented to include the header '`<stdint.h>`' (this is not usual for C headers), but the '`<inttypes.h>`' header may be available where '`<stdint.h>`' is not and may be a better choice for portability. The '`<stdint.h>`' header is an invention of the standards committee, and was created so that free-standing implementations of C (as opposed to hosted implementations - normal ones) only have to support '`<stdint.h>`' and not necessarily '`<inttypes.h>`' too (it would also mean supporting '`<stdio.h>`', which is otherwise not necessary).
Jonathan Leffler
+8  A: 

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 Ofcourse not all platforms might support that.

nos
+9  A: 

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!

Brooks Moses
There's no need to “ignore systems with 16-bit int”, long is guaranteed to be at least 32-bit wide everywhere.
Bastien Léonard
Right, but using "long" doesn't address the initial request, which is something that's exactly 32 bits. On (at least some flavors of) 64-bit Linux, for example, a long is 64 bits -- and that's something that's likely to come up in actual practice.
Brooks Moses
A: 

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.

Robert
A: 

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"

Spudd86
+2  A: 

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.

cfrantz
A: 

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.

Bastien Léonard