我的 C 程序的输出不是预期的
My C program's output is not the expected one
#include<stdio.h>
#include<conio.h>
int main()
{
char c, p;
p=getchar();
int n=p+259;
c=n;
putchar(c);
return 0;
}
如果我输入 'a' 字符,有人能告诉我为什么这个程序的输出是 'd' 字符吗?
若p='a',则n=97+259=356。
如果我的 n 变量是 356,怎么可能给 c 赋值 100('d' 的 ASCII 码)?
char
可以取 0 到 255 之间的值,因为它是 8 位。
97 + 259 = 356 和 356 模 256 为 100。
char
是8位数据类型,你大大超过了它的最大表示:
a -> ascii 97
97 + 259 -> 356
356 & 0xFF -> 100 - overflowed, strip off "high bit" which can't be stored.
100 -> ascii 'd'
#include<stdio.h>
#include<conio.h>
int main()
{
char c, p;
p=getchar();
int n=p+259;
c=n;
putchar(c);
return 0;
}
如果我输入 'a' 字符,有人能告诉我为什么这个程序的输出是 'd' 字符吗?
若p='a',则n=97+259=356。 如果我的 n 变量是 356,怎么可能给 c 赋值 100('d' 的 ASCII 码)?
char
可以取 0 到 255 之间的值,因为它是 8 位。
97 + 259 = 356 和 356 模 256 为 100。
char
是8位数据类型,你大大超过了它的最大表示:
a -> ascii 97
97 + 259 -> 356
356 & 0xFF -> 100 - overflowed, strip off "high bit" which can't be stored.
100 -> ascii 'd'