Define 16 bit integer in C
Asked Answered
C

4

28

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?

Carma answered 21/3, 2012 at 21:37 Comment(3)
Maybe I just don't know C, but I think int16_t is probably guaranteed to be 16 bits.Keratoid
There is no guarantee. The people who wrote the header are free to make non-portable assumptions that may not apply on another compiler. Use the typedef rather than short.Whig
I believe there is a closely related question Exact-width integer types in C (stdint.h) The assertion there is, if int16_t is defined, it is correct, irrespective of how. So, for example, on an 8bit microcontroller like an AVR (on an Arduino), stdint.h might contain typedef int int16_t; But, it does not have to be defined if it can't be done correctly. There are another set of related types which escape at this moment. Anyway the compiler will throw an error if it isn't there, so it should be testable.Misdoubt
S
44

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.

Shaunshauna answered 21/3, 2012 at 21:40 Comment(1)
I meant thatlooking in the header they just wote this lines, with no conditions at all for checking it. So how exactly will it be defined as short on my machine, and not on all machines?Carma
R
11

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.

Reglet answered 21/3, 2012 at 21:48 Comment(0)
T
4

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.

Tildie answered 21/3, 2012 at 21:41 Comment(0)
M
3

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.

Medan answered 21/3, 2012 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.