Question

I need to declare an integer in the size of 16 bit, in C.

I know that short and int sizes are machine dependent.

I tried to use "stdint.h", but it seems that they simply do

typedef short int16_t

So my question is:

Am I missing something and short type guarantees 16 bit length?

If no, is there is an alternative that guarantees it?

Était-ce utile?

La solution

That means int16_t is defined as short on your machine, not all machines.

Just use the int16_t where you absolutely need a 16bit integer type; it will be defined as appropriate on all platforms that provide stdint.h (which should be all that support C99, or cstdint for C++).

[Edit] To clarify, the "stdint.h" header file is provided by the C (or C++) compiler, so its contents will likely vary per compiler, version, system, CPU architecture, etc. That is, the authors of the compiler suite know exactly what types have what sizes on which systems. Looking at that file on just one system only tells you about the definitions for a particular version of a particular compiler on a particular OS on a particular architecture (e.g. GCC 4.2 on Darwin x86_64, or Visual Studio on WinNT Alpha, or ICC on Solaris IA32, etc). Some systems, especially embedded ones, might have different type sizes, so a short might not always be 16 bits, and the compiler would know the right size to use for that bit length.

If you look at the file stdint.h on another system the definitions might be different, or they might be the same - but its purpose is to provide the definitions for integer types of guaranteed bit lengths.

Autres conseils

No, short does not guarantee 16-bit length. The only guarantees about the basic integer data types are:

  • sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
  • sizeof(char) == 1 (note: a char could still be more than 8 bits!)
  • SHRT_MIN <= -32767 and SHRT_MAX >= 32767 (implies short is at least 16 bits)
  • INT_MIN <= -32767 and INT_MAX >= 32767 (implies int is at least 16 bits)
  • LONG_MIN <= -2147483647 and LONG_MAX >= 2147483647 (implies long is at least 32 bits)
  • LLONG_MIN <= -9223372036854775807 and LLONG_MAX >= 9223372036854775807 (implies long long is at least 64 bits)

(C99 §5.2.4.2.1 and Annex E)

The fact that short is typedef'ed to int16_t on your machine just means that a short is 16 bits on your machine. It does not mean that the definition will be the same on other people's machines (or even on other compiles on your same machine).

If you include <stdint.h>, it will define int16_t in some way which is guaranteed to be a type that is signed and 16 bits wide. If you need exact-size integers, use those exact-size types.

Each machine may or may not have those definitions available.

The only real safe way to do it is via a configure check or something.

But if int16_t exists (and configure finds it) then it should be a short on machines where a short is indeed 16 bits. If a short is a different size, then that system would define int16_t to something else for you. IE, if int16_t is available you can safely assume it's 2 bytes long.

short is only guaranteed to be at least 16-bit wide.

It can be 16-bit wide in your system but 32-bit in another system.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top