Wan*_*ool 4 c++ integer fixed-width
给出以下代码.
#include <cstdint>
#include <iostream>
#include <limits>
int main()
{
    int8_t x = 5;
    std::cout << x << '\n';
    int y = 5;
    std::cout << y;
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();
    return 0;
}
我的输出是三叶草和5.如果固定宽度整数是整数,为什么它们输出它们的数字的ASCII字符符号?
编辑:刚发现这种行为只发生在8位固定宽度的整数上?这是编译器行为吗?
Bau*_*gen 12
嗯,它们是整数,因为你可以用它们进行8位整数运算.
但很显然,你的系统上,int8_t实现为typedef到signed char.这是完全合法的,因为signed chars也是整数,但由于operator<<for signed chars打印了它们的字符符号而不是它们的数值,因此会产生意想不到的结果.如果有人试图打印,其他任何东西都会很奇怪signed char.
如果你想看到你的数字值int8_t,只需int在打印前投射到.
| 归档时间: | 
 | 
| 查看次数: | 125 次 | 
| 最近记录: |