奇怪的gcc行为与无符号整数

时间:2022-05-03 17:24:11
#include <iostream>

#include <cstdint>
#include <cstdio>

using namespace std;

int main()
{
    uint16_t ii; 
    std::cin >> ii;                                                    
    printf("%d\n", ii);
}

When I give input 5 the output is also 5. But when I change the type of ii to uint8_t, I do not get 5 but 53 which seems to be the ASCII value of 5. Is this expected?

当我输入输入5时,输出也是5.但是当我将ii的类型更改为uint8_t时,我没有得到5但是53这似乎是ASCII值为5.这是预期的吗?

1 个解决方案

#1


11  

uint8_t is allowed (but not required) to be a typedef for char (if it happens to be unsigned) or unsigned char. And input of those is done as characters not numbers. So this is valid but not required behaviour.

允许uint8_t(但不是必需的)为char的typedef(如果它恰好是unsigned)或unsigned char。这些输入是作为字符而不是数字完成的。所以这是有效但不是必需的行为。

#1


11  

uint8_t is allowed (but not required) to be a typedef for char (if it happens to be unsigned) or unsigned char. And input of those is done as characters not numbers. So this is valid but not required behaviour.

允许uint8_t(但不是必需的)为char的typedef(如果它恰好是unsigned)或unsigned char。这些输入是作为字符而不是数字完成的。所以这是有效但不是必需的行为。