我在C++,Linux中工作,我遇到了如下问题:
struct testing{
uint8_t a;
uint16_t b;
char c;
int8_t d;
};
testing t;
t.a = 1;
t.b = 6;
t.c = 'c';
t.d = 4;
cout << "Value of t.a >>" << t.a << endl;
cout << "Value of t.b >>" << t.b << endl;
cout << "Value of t.c >>" << t.c << endl;
cout << "Value of t.d >>" << t.d << endl;我的控制台上的输出是:
Value of t.a >>
Value of t.b >>6
Value of t.c >>c
Value of t.d >>对于int8_t和uint8_t类型,似乎缺少t.a和t.d。为甚麽呢?
谢谢。
发布于 2010-11-18 19:31:08
int8_t和uint8_t类型可能定义为char和unsigned char。流<<操作符将把它们输出为字符。由于它们分别设置为1和4,这是控制字符,而不是打印字符,因此在控制台上将看不到任何东西。尝试将它们设置为65和66 ('A‘和'B'),看看会发生什么。
EDIT:要打印出数值而不是字符,您需要将它们转换为适当的类型:
cout << static_cast<unsigned int>(t.a) << endl;发布于 2010-11-18 19:28:00
这是因为在选择operator<<重载时,这些变量被视为“char”类型。
尝试:
cout << "Value of t.a >>" << static_cast<int>(t.a) << endl;发布于 2010-11-18 19:32:52
在this linux手册页中,int8_t和uint8_t实际上被定义为字符:
typedef signed char int8_t
typedef unsigned char uint8_t字符的值1和4是控制字符,您可以找到here。
这就是为什么您看不到任何打印的原因。
https://stackoverflow.com/questions/4214215
复制相似问题