问题
I always thought that in C, int
stands for signed int
; but I have heard that this behavior is platform specific and in some platforms, int
is unsigned
by default. Is it true? What says the standard, and has it evolved over time?
回答1:
You are quite right. As per C11
(the latest c standard), chapter §6.7.2
int
,signed
, orsigned int
is categorized as same type (type specifiers, to be exact). So, int
is the same as signed int
.
Also, re-iterating the same, from chapter §6.2.5/P4
There are five standard signed integer types, designated as
signed char
,short int
,int
,long int
, andlong long int
. (These and other types may be designated in several additional ways, as described in 6.7.2.) [....]
So, for any conforming environment, int
stands for signed int
and vice versa.
回答2:
int
, signed
, and signed int
are all the same type.
The exact form of int
is implementation specific; the range must be at least -32767 to +32767. There is no upper limit on the range. Note also that the complement scheme can differ too: 2's complement is common these days although 1's complement and signed magnitude are also allowed.
回答3:
According to this Wikipedia article, int
is a signed integral data type which is at least 16 bits in size.
来源:https://stackoverflow.com/questions/44624857/is-int-always-signed