我试图为char
,unsigned char
和signed char
变量分配一个整数,以了解相同的结果。我在64位Mac OS上运行此代码。我对这个回应感到困惑,我正在找人帮助我。我不是在看结果的大小,我只是想了解为什么打印变量的值会导致奇怪的字符。
#include<iostream>
using namespace std;
int main() {
char a = 2;
unsigned char b = 10;
signed char c = 200;
cout<<"Regular char variable value "<<a<<"\t"<<"size of the variable is "<<sizeof(a)<<"\n";
cout<<"Regular char variable value "<<b<<"\t"<<"size of the variable is "<<sizeof(b)<<"\n";
cout<<"Regular char variable value "<<c<<"\t"<<"size of the variable is "<<sizeof(c)<<"\n";
}
此代码的输出如下:
Regular char variable value size of the variable is 1
Regular char variable value
size of the variable is 1
Regular char variable value ? size of the variable is 1
请注意,输出中的第二行是乱码,这会增加输出的复杂性。