如何找出一个变量是否签署或unsingned [英] how to find out if a variable is signed or unsingned
问题描述
在C程序中,如果一个整数整数签署
,那么最高位为 1
,否则 0
。
In C program, if an integer is signed integer
, then the highest bit is 1
, otherwise 0
.
让我们字符
和 unsigned char型
,对范围内的符号字符
是 -128
到 127
和 unsigned char型
是 0
到 255
,但实际上它们的十六进制是<$ C $的C> 0×00 <范围/ code>到 0xFF的
。
我的问题是,现在如果一个字符
和 unsigned char型
存储在使用8位二进制数,请问该电脑内存自己知道是否是签署
或无符号
?
Lets take char
and unsigned char
, the range for a signed char
is -128
to 127
and unsigned char
is 0
to 255
, but in fact their hexadecimal are in range of 0x00
to 0xff
.
My question is now if a char
and unsigned char
are stored in memory using 8 bits binary number, how does the computer itself know whether it is signed
or unsigned
?
char a = 0xff; printf("%d", a); //its result is -1.
unsigned char a = 0xff; printf("%d", a); //its result is 255.
在上面的例子中,请问的printf
知道的0xFF的
值带有符号?是仅依赖于定义 A
?
In the example above, how does printf
know the value of 0xff
is signed or unsigned? Is that only depends on the definition of a
?
推荐答案
有许多相关的问题在这里,例如的此内容之一。
There are many related questions here, for example this one.
您的问题是不完全正确:为签署
的最高位并不总是 1
- 只有当值是负的。事实上,签署
或无符号
是类型归结到同一个位patters,而这些位模式如何除preTED的比较或晋升时的是由各自类型的定义。
Your question is not quite right: for signed
the highest bit is not always 1
-- only if the value is negative. In fact, signed
or unsigned
are "types" attributed to the exact same bit patters, and how these bit patterns are interpreted when compared or promoted is defined by their respective types.
例如:
unsigned char u = 0xFF; // decimal 255
signed char s = 0xFF; // decimal -1
可以看到这两个值如何都是一样的,在这两个最高位被置位,但它们在它们的类型不同。
You can see how both values are the same, in both the highest bit is set, but they differ in their types.
编译器使用一个类型系统知道如何跨preT值,这是程序员的分配有意义的类型值的任务。在上面的例子中,我告诉编译器,第一个 0xFF的
应PTED为无符号
价值间$ P $ (见包含文件 limits.h中)与最大射程:
The compiler uses a type system to know how to interpret values, and it is the task of the programmer to assign meaningful types to values. In the above example, I told the compiler that the first 0xFF
should be interpreted as an unsigned
value (see also the include file limits.h) with the maximum range:
u = 0x00; // decimal 0, CHAR_MIN
u = 0xFF; // decimal 255, UCHAR_MAX
和第二 0xFF的
为签署
通过最大范围值:
and the second 0xFF
as a signed
value with the maximum range:
s = 0x00; // decimal 0, CHAR_MIN
s = 0x7F; // decimal 127, SCHAR_MAX
s = 0x80; // decimal -127, SCHAR_MIN (note how 0x7F + 1 = 0x80, decimal 127 + 1 = -127, called an overflow)
s = 0xFF; // decimal -1
有关在您的示例的printf,在%d个
告诉它期望符号int
值。根据C语言的整数提升规则较小字符
类型是符号扩展(如果它是签署
型)或零扩展(如果它是无符号
类型)。与上面的例子完成
For the printf in your example, the %d
tells it to expect a signed int
value. According to the integer promotion rules of the C language the smaller char
type is either sign-extended (if it's signed
type) or zero-extended (if it's unsigned
type). To finish with the above example:
printf("%d", u); // passes a int 0x000000FF, decimal 128, to the function
printf("%d", s); // passes a int 0xFFFFFFFF, decimal -1, to the function
更多printf的格式化说明符这里,例如 %U
可能在这方面对你有意思。
More printf formatting specifiers are here, for example %u
might be interesting for you in this context.
这篇关于如何找出一个变量是否签署或unsingned的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!