Consider this simple C++ program:
#include <cstdint>
#include <iostream>
int main() {
uint8_t var;
std::cin >> var;
std::cout << "var = " << var << '\n';
if (var == 1) {
std::cout << "var is 1\n";
} else {
std::cout << "var is not 1\n";
}
}
Running it shows some surprising behavior. When the input is 1
, the output is
var = 1
var is not 1
which is clearly absurd! After quite a few varied tests, I realized what is happening—it's reading and writing a char
! Still, it's not the behavior I want—I used uint8_t
because I want the int
eger behavior. How can I make uint8_t
behave like the other integer types when doing stream I/O? Alternatively, what one-byte type should I use instead?