1.第一题
int main()
{
int a = 128;
printf("%u\n", a);
system("pause");
}
输出结果
128
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a = 128;
printf("%u\n", a);
system("pause");
}
输出结果
4294967168
因为有符号字符型其范围为-128~127
127用二进制表示为:0111 1111,
128表示为1000 0000,这里发生溢出,因为第一位为1,为符号位,表示负数,即-128
看到这里是不是一头雾水,网上有很多方法解释一同看完还是不懂。所以我画了一张图来解释
有符号字符型其范围为-128~127
所以127的下一个数字是-128不是128
2.第二题
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned i;
for (i = 0; i >= 0; i--)
{
printf("%u\n", i);
}
system("pause");
}
为什么输出结果是这样呢?
无符号数永远大于0
2.1
#include <stdio.h>
#include <stdlib.h>
unsigned char i = 0;
int main()
{
for (i = 0; i <= 255; i++)
{
puts("Hello World");
}
}
3.第三题
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[1000];
int i;
for (i = 0; i < 1000; i++)
{
a[i] = -1 - i;
}
printf("%d\n", strlen(a));
system("pause");
}
strlen()遇到0就停止,那么他什么时候遇到’\0’呢?
当i = 127的时候 a[i] = -128;那么他的下一个数字是多少呢?是-129吗?
4.第四题
int main()
{
short num = 32767;
short int a = num + 1;
printf("%d\n", a);
system("pause");
}
32767(有符号的短整型能表示的最大值)
结果:-32768
记:有符号的短整型能表示的最大值