问题
I have just started learning C and a question has bugged me for a while now. If I write
int i = -1;
unsigned int j = 2;
unsigned int k = -2;
What is the type of integer literal -1
and 2
and -2
, and how does it get converted to get stored in signed int
and unsigned int
?
What is meant by signed integer, is that the property of variable or integer literal too? Like -2
is signed integer and 2
is unsigned integer?
回答1:
First off, -1
is not an integer constant. It's an expression consisting of a unary -
operator applied to the constant 1
.
In C99 and C11, the type of a decimal integer constant is the first of int
, long int
, or long long int
in which its value will fit. Similarly, an octal or hexadecimal literal has type int
, unsigned int
, long int
, unsigned long int
, long long int
, or unsigned long long int
. The details are in N1570 6.4.4.1.
-1
and -2
are constant expressions. The result of the unary -
operator has the same type as the operand (even if that result causes an overflow, as -INT_MIN
does in most implementations).
int i = -1;
The constant 1
and the expression -1
are both of type int
. The value is stored in the int
object i
; no conversion is necessary. (Strictly speaking, it's converted from int
to int
, but that doesn't matter.)
unsigned int j = 2;
2
is of type int
. It's converted from int
to unsigned int
.
unsigned int k = -2;
-2
is of type int
. It's converted from int
to unsigned int
. This time, because -2
is outside the range of unsigned int
, the conversion is non-trivial; the result is UINT_MAX - 1
.
Some terminology:
A constant is what some other languages call a literal. It's a single token that represents a constant value. Examples are 1
and 0xff
.
A constant expression is an expression that's required to be evaluated at compile time. A constant is a constant expression; so is an expression whose operands are constants or constant expressions. Examples are -1
and 2+2
.
回答2:
In C99 and C11
If you want to specifies the type of your integer you can use an integer constant:
You can write integer with decimal, octal or hexa representation:
int decimal = 42; // nothing special
int octal = 052; // 0 in front of the number
int hexa = 0x2a; // 0x
int HEXA = 0X2A; // 0X
Decimal representation:
By default, the type of -1, 0, 1, etc. is int
, long int
or long long int
. The compiler must peak the type that can handle your value:
int a = 1; // 1 is a int
long int b = 1125899906842624; // 1125899906842624 is a long int
That only work for signed
value, if you want unsigned
value you need to add u or U:
unsigned int a = 1u;
unsigned long int b = 1125899906842624u;
If you want long int
or long long int
but not int
, you can use l or L:
long int a = 1125899906842624l;
You can combine u and l:
unsigned long int a = 1125899906842624ul;
Finally, if you want only long long int
, you can use ll or LL:
unsigned long long int a = 1125899906842624ll;
And again you can combine with u.
unsigned long long int a = 1125899906842624ull;
Octal and Hexadecimal representation:
Without suffix, a integer will match with int
, long int
, long long int
, unsigned int
, unsigned long int
and unsigned long long int
.
int a = 0xFFFF;
long int b = -0xFFFFFFFFFFFFFF;
unsigned long long int c = 0xFFFFFFFFFFFFFFFF;
u doesn't differ from decimal representation. l or L and ll or LL add unsigned value type.
This is similar to string literals.
回答3:
What is the type of integer literal -1 and 2 and -2, and how does it gets convert to get stored in signed int and unsigned int?
The C parser/compiler, as previously said by chux, "understands" your literal as a signed integer - always. They are then casted to fit in the variable you assign to, which can be of different type. Doing this, some bits can be lost or they can change their meaning (for example, assigning a negative value to an unsigned int). Some compiler could warn you about a "literal out of range", other compilers could silently accept (and truncate) your literals.
What Do You Mean By Signed Integer, is that the property of variable or integer literal too , like -2 is signed integer and 2 is unsigned integer.?
It is a property of the variable. In reality, it is a "type" - written as a "two words" identifier.
回答4:
I would say it depends on the compiler and the architecture of the machine. Given 8 bits = 1 byte
, the following table summarizes different Integer types with their required sizes for both (signed) int
and unsigned int
on 32 and 64-bit machines:
+------+------+---------+-------+--------+-------------+-----------+
|Type |char |short int|int |long int|long long int|int pointer|
+------+-------+--------+-------+--------+-------------+-----------+
|32-bit|8 bits|16 bits |32 bits|32 bits |64 bits |32 bits |
+------+------+---------+-------+--------+-------------+-----------+
|64-bit|8 bits|16 bits |32 bits|64 bits |64 bits |64 bits |
+------+------+---------+-------+--------+-------------+-----------+
As you may know, the biggest difference between (signed) int
and unsigned int
is that in (signed) int
the Most Significant Bit (MSB) is reserved for the sign of the Integer and hence:
- a (signed)
int
having n bits can have a value between-(2^(n-1))
to(2^(n-1))-1
- an
unsigned int
having n bits can have a value between0
to(2^n)-1
Now, we can calculate the range (possible values) of different (singed) int
types as follows:
+------+---------+----------+----------+----------+-------------+-----------+
|Type |char |short int |int |long int |long long int|int pointer|
+------+---------+----------+----------+----------+-------------+-----------+
|32-bit|-(2^7) to|-(2^15) to|-(2^31) to|-(2^31) to|-(2^63) to |-(2^31) to |
| |+(2^7)-1 |+(2^15)-1 |+(2^31)-1 |+(2^31)-1 |+(2^63)-1 |+(2^31)-1 |
+------+---------+----------+----------+----------+-------------+-----------+
|64-bit|-(2^7) to|-(2^15) to|-(2^31) to|-(2^63) to|-(2^63) to |-(2^63) to |
| |+(2^7)-1 |+(2^15)-1 |+(2^31)-1 |+(2^63)-1 |+(2^63)-1 |+(2^63)-1 |
+------+---------+----------+----------+----------+-------------+-----------+
Furthermore, we can calculate the range (possible values) of different unsigned int
types as follows:
+------+-------+----------+-------+--------+-------------+-----------+
|Type |char |short int|int |long int|long long int|int pointer|
+------+-------+---------+--------+--------+-------------+-----------+
|32-bit|0 to |0 to |0 to |0 to |0 to |0 to |
| |(2^8)-1|(2^16)-1 |(2^32)-1|(2^32)-1|(2^64)-1 |(2^32)-1 |
+------+-------+---------+--------+--------+-------------+-----------+
|64-bit|0 to |0 to |0 to |0 to |0 to |0 to |
| |(2^8)-1|(2^16)-1 |(2^32)-1|(2^64)-1|(2^64)-1 |(2^64)-1 |
+------+-------+---------+--------+--------+-------------+-----------+
Finally, to see how and why we store a long long int
using 8 bytes (64 bits) on a 32-bit machine see this post.
来源:https://stackoverflow.com/questions/41405578/what-are-integer-literal-type-and-how-they-are-stored