Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is int16_t guaranteed to be signed?

Tags:

c

unsigned

signed

Is the int16_t type declared in <stdint.h> guaranteed to be signed, or is it just supposed to be signed? I would assume that it would have to be signed, but surprisingly I can't seem to find any hard evidence (i.e. references to the spec) that explicitly mention this anywhere.

Could someone confirm this either way? I'd really appreciate specific references to the spec, if at all possible.

like image 907
templatetypedef Avatar asked Dec 22 '22 19:12

templatetypedef


2 Answers

n1256 7.18.1p1 says:

When typedef names differing only in the absence or presence of the initial u are defined, they shall denote corresponding signed and unsigned types as described in 6.2.5; an implementation providing one of these corresponding types shall also provide the other.

Since there is an int16_t and a uint16_t, the int16_t is therefore signed.

Or indeed 7.18.1.1p1:

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.

which is more explicit about it.

EDIT: although strictly speaking, int16_t is not guaranteed to be signed in the sense that it's not guaranteed to exist. If you have CHAR_BIT==9, for example, there cannot be a int16_t type.

like image 58
Philip Potter Avatar answered Dec 24 '22 08:12

Philip Potter


Yes, int16_t is guaranteed to be signed two's complement by the ISO spec.

  • http://linux.die.net/man/3/int16_t
  • http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf page 256
like image 22
David German Avatar answered Dec 24 '22 08:12

David German