Question

#include <iostream>

int main() {
    for(int i=0;i<18;i++)
       std::cout << (char)i << '\n';
}

OUTPUT is:
enter image description here

But where are other characters?
enter image description here

Was it helpful?

Solution

The characters you are expecting to see are not ASCII. In ASCII, the codes below 32 signify what is called control characters, which were originally intended to control functions on teletype printers. Many of them don't apply to modern terminals, so your terminal just picked some characters (or got them from some other encoding), such as the faces and the card suits, to use for those codes. Some of the ASCII control characters are still applicable though.

  • 7 is a called the bell character ('\a'), you may have heard a beep.
  • 8 is a backspace ('\b').

    std::cout << "abc" << (char)8 "def"; // where's the c?

  • 9 is a horizontal tab ('\t'), so that's invisible, but you'll probably notice it if you print visible characters before and after it.

    std::cout << "before" << (char)9 << "after";

  • 10 is a line feed, a.k.a. newline ('\n')

  • 13 is a carriage return ('\r').

    std::cout << "hello" << (char)13 << "world"; // where's the hello?

Your results may vary depending upon which terminal you use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top