Chars are automatically formatted like %c. To print integer as char (if you really want to), you can convert it:
int x = 42;
std::cout << (char) x;
Reading works similarly (it behaves similar to cout
, not so much to scanf
). No formatting required:
char c;
std::cin >> c;
Here is an echo example:
char c;
while(std::cin >> std::noskipws >> c) {
std::cout << c;
}
One caveat with cin
is that it is stateful. If you've already used cin
in your code, you may need to reset the error-state bits with std::cin.clear()