Rev*_*ick 1 c character variable-assignment output
#include<stdio.h>
#include<conio.h>
int main()
{
char c, p;
p=getchar();
int n=p+259;
c=n;
putchar(c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我输入'a'字符,有人可以告诉我为什么这个程序的输出是'd'字符?
如果p ='a',则n = 97 + 259 = 356.如果我的n变量是356,如何为c分配值100('d'的ASCII代码)?
char 是一个8位数据类型,你大大超过了它的最大代表:
a -> ascii 97
97 + 259 -> 356
356 & 0xFF -> 100 - overflowed, strip off "high bit" which can't be stored.
100 -> ascii 'd'
Run Code Online (Sandbox Code Playgroud)