Cout不打印数字
问题
我没有从简单的 cout 中得到任何输出,而 printf 总是会打印数字:
std::cout << variableuint8; // prints nothing
printf("%u", variableuint8); // prints the number
我以前从未遇到过这种行为,虽然我有解决办法,但我想理解它。
上下文:
是的,指点。因此,它可能比所说的要复杂一些。这是上下文 - 我认为这并不重要,当 cout 和 printf 获取信息时,它已取消引用为简单的无符号字符。这可能比解决问题所需的信息要多,但我希望在相关的情况下能够完成。
typedef unsigned char UInt8;
typedef unsigned short UInt16;
typedef struct{
UInt8 len;
// some other definitions. sizeof(DeviceNotification) <= 8
}DeviceNotification;
UInt8 somerandomdata[8];
DeviceNotification * notification;
notification = (DeviceNotification *) somerandomdata;
std::cout << "Len: (" << notification->len << ")\n";
printf("Len: (%u)\n", notification->len);
随机数据在其他地方初始化。对于此输出,数组中的第一个字节是 0x08:
输出:
Len: ()
Len: (8)
环境
- 开发机器:
- OS X 10.6.8
- 编译器 LLVM 1.7
- Xcode 3.2.6
- 测试机:
- OS X 10.6.8 操作系统
Issue
I'm getting no output from a simple cout, whereas a printf will always print the number:
std::cout << variableuint8; // prints nothing
printf("%u", variableuint8); // prints the number
I've never run into this behavior before, and while I have a work around I would like to understand it.
Context:
Yay, pointers. So it may be a little more involved than as stated. Here's the context - I don't think it should matter, by the time cout and printf get the info it's dereferenced to a simple unsigned char. This is probably more information than needed to resolve the issue, but I wanted to be complete on the off chance that it was relevant.
typedef unsigned char UInt8;
typedef unsigned short UInt16;
typedef struct{
UInt8 len;
// some other definitions. sizeof(DeviceNotification) <= 8
}DeviceNotification;
UInt8 somerandomdata[8];
DeviceNotification * notification;
notification = (DeviceNotification *) somerandomdata;
std::cout << "Len: (" << notification->len << ")\n";
printf("Len: (%u)\n", notification->len);
The random data is initialized elsewhere. For this output, the first byte in the array is 0x08:
Output:
Len: ()
Len: (8)
Environment
- Development machine:
- OS X 10.6.8
- Compiler LLVM 1.7
- Xcode 3.2.6
- Test machine:
- OS X 10.6.8
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它将
char
作为字符打印。这会将“A”打印到标准输出。
尽管在这种情况下,您的代码应该打印
Len: (*)
,而且我确实已验证它会打印Len: (*)
。编辑:由于您的控制台可能使用 ASCII 或 UTF-8 等字符编码,因此与 8(退格键)对应的字符不可见,因此看起来好像没有打印出来。
(在某些情况下,可能会导致前一个字符(
(
) 消失,因为它是退格字符。在 DOS 中可能会显示 ◘)It's printing the
char
as a character.This prints 'A' to the stdout.
Although in that case, your code should print
Len: (*)
, and I did have verified it printsLen: (*)
.Edit: Since in character encodings like ASCII or UTF-8 your console is likely using, the character corresponding to 8 (Backspace) is not visible, it will look like it's not printed.
(In some cases it may cause the previous character (the
(
) to disappear because it is a backspace character. In DOS it may show ◘)您是否尝试将其转换为 int,以避免依赖于实现所使用的 char 类型:
这是对所发生情况的解释:什么是 unsigned char?
为了获得一个想法,您的实现使用
unsigned char
作为字符
。 0x08 的 ASCII 代码是退格控制字符,当然它不是可打印字符,这就是为什么在std::cout
的情况下看不到输出。Have you tried to cast it into int, to avoid depending on the char type that is used by the implementation:
Here is an explanation of what is going on: What is an unsigned char?
To get an idea, your implementation is using
unsigned char
aschar
. The ASCII code for 0x08 is the backspace control character, and of course it is not a printable character, that is why you don't see an output in the case ofstd::cout
.会
std::cout << "Len: (" << (unsigned Short)notification->len << ")\n";
打印正确的值?我猜想转换为整数可能就是我们想要的。Would
std::cout << "Len: (" << (unsigned short)notification->len << ")\n";
print the correct value? I guess casting to an integer is what might be looking for.