【发布时间】:2016-11-16 19:32:35
【问题描述】:
此代码将打印:
s = 1, i = 65537, f = 65537.000000, c = 1
我需要帮助来理解为什么它打印 c=1。
代码:
#include <stdio.h> // Standard input-output library
#include <stdlib.h> // Standard general utilities library
int main(void) {
int i = 65537;
unsigned short s = (unsigned short)i;
float f = (float)i;
char c = (char)i;
printf("s = %u, i = %d, f = %f, c = %d\n", s,i,f,c);
system("PAUSE");
return (0);
}
【问题讨论】:
-
@StoryTeller 如果您想查看数值,请不要。
-
c=1 出于同样的原因 s=1
-
(在您的机器上)字符是 8 位。 Int 是 32 位。不同之处在于 65537 完全适合 int 但不适合 char。你得到 1 因为 65537 % (2^8) = 1。
-
char的大小(以字节为单位)是多少?int的大小(以字节为单位)是多少?这些类型的最大值是多少?当一个数字溢出时会发生什么? -
@BadZen:如果
char已签名,则它是实现定义的,只有当我们知道实现是什么以及它如何处理它时,它才是“定义明确的”。链接的问题是关于unsigned char!