What is the char type?

The char data type stores a single character - any individual letter, digit, punctuation mark, or space.

Despite storing characters, char is an integral type - its value is stored internally as a whole number. Characters are mapped to numeric values through ASCII (American Standard Code for Information Interchange), which assigns each character a unique number from 0 to 127.

For example:

  • 'A' = 65
  • 'a' = 97
  • '0' = 48
  • ' ' (space) = 32

In C++, character literals must be enclosed in single quotes: 'g', '1', ' '.

Initializing chars

The recommended way to initialize char variables is using character literals:

char grade{'A'};  // preferred - stores code point 65

You can also initialize with integers, though this is discouraged:

char grade{65};  // stores integer 65 ('A') - not preferred
Warning
Don't confuse character digits with integer values:
- char ch{5}; stores integer 5 directly
- char ch{'5'}; stores code point 53 (the character '5')

Printing and inputting chars

When outputting a char, std::cout displays the corresponding character:

#include <iostream>

int main()
{
    char response{'Y'};
    std::cout << response << '\n';  // displays 'Y'

    return 0;
}

For input, std::cin extracts only the first character:

#include <iostream>

int main()
{
    std::cout << "Enter a character: ";
    char ch{};
    std::cin >> ch;
    std::cout << "You entered: " << ch << '\n';

    return 0;
}

If the user types "xyz", only 'x' is extracted - "yz" remains in the input buffer.

Extracting whitespace characters

By default, std::cin >> skips whitespace. To capture spaces, use std::cin.get():

char ch{};
std::cin.get(ch);  // extracts any character, including whitespace

Escape sequences

C++ defines special character sequences called escape sequences that begin with a backslash:

Sequence Meaning
\n Newline
\t Horizontal tab
\' Single quote
\" Double quote
\\ Backslash

Example:

#include <iostream>

int main()
{
    std::cout << "She said, \"Hello!\"\n";
    std::cout << "Path: C:\\Users\\Name\n";
    std::cout << "Column1\tColumn2\n";

    return 0;
}

Output:

She said, "Hello!"
Path: C:\Users\Name
Column1	Column2
Warning
Escape sequences use backslashes (\), not forward slashes (/). Using '/n' instead of '\n' won't work as expected.

Single vs double quotes

  • Single quotes ('a'): Represent a single char literal
  • Double quotes ("Hello"): Represent a C-style string (multiple characters)
Best Practice
Use single quotes for individual characters ('t', '\n') and double quotes for text strings ("Hello").

Char size and range

The char type is always exactly 1 byte. By default, char may be signed (-128 to 127) or unsigned (0 to 255) depending on the compiler. For ASCII characters (0-127), the sign doesn't matter.

Other character types

C++ also provides:

  • wchar_t: Wide character (avoid - implementation-defined size)
  • char8_t: 8-bit Unicode (UTF-8)
  • char16_t: 16-bit Unicode (UTF-16)
  • char32_t: 32-bit Unicode (UTF-32)

For most programs, stick to char and ASCII characters.