Strange gcc behavior with unsigned ints

#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 , output is also 5 . But when I change type ii to uint8_t , I don't get 5 , but 53 , which seems to be an ASCII value of 5 . Is this expected?

+7
c ++ c ++ 11 g ++ c ++ 14
source share
1 answer

uint8_t allowed (but not required) for typedef for char (if not specified) or unsigned char . And the input of this data is performed as characters, not numbers. Therefore, this is a valid but not required behavior.

+11
source share

All Articles