char and ASCII in C++: a char is a one-byte integer
A C++ char is one byte holding a number. The real bytes of "cat" are 63 61 74 00, and subtracting 32 from the first one prints Cat.
What is a char in C++, and where does ASCII come in? A char is not a letter. It is one byte holding a number, and the letter you see is only how that number gets drawn when it is printed. Here are the real bytes of a short string.
This is the written version of Understanding C++ #7. Everything below comes from one program, compiled with g++ -std=c++20 -Wall -O2 on x86-64 Linux.
How big is a char?
sizeof(char) is 1, and that is not a measurement: the standard defines it as 1. sizeof counts in units of char, so a char is the unit everything else is measured in. On this machine, as on almost every machine today, that unit is an 8-bit byte.

What does char s[] = "cat" put in memory?
The string literal "cat" has four characters, not three: the compiler adds a zero byte at the end. So char s[] = "cat" is an array of four chars, and sizeof s prints 4.
The program prints each byte in hex with %02X:
| byte | hex | decimal | prints as |
|---|---|---|---|
s[0] |
63 |
99 | c |
s[1] |
61 |
97 | a |
s[2] |
74 |
116 | t |
s[3] |
00 |
0 | (nothing: it ends the string) |
![The bytes of char s[] = "cat": 63, 61, 74 and the 00 that ends the string](https://vthvqgqmgbnywvxzqlws.supabase.co/storage/v1/object/public/blog-media/shorts/cpp-007-char-ascii/dump.jpg)
The 00 is how C-style strings end. Functions like printf("%s") and strlen walk forward byte by byte until they reach a zero byte, so the string carries no separate length.
Where are the bytes?
The loop also prints the address of each byte with %p. In this run they were 0x7ffd8995db24 to 0x7ffd8995db27: each one higher than the last. An array's elements are contiguous, and a char is one byte wide, so consecutive chars sit at consecutive addresses. The addresses themselves change from run to run (the stack is placed at a random address each time); the step of one does not.

Where does the letter come from?
Printing s[0] with %d gives 99. The char is promoted to an int and printed as a number, because that is all it is. When the same byte goes to the terminal through %s, the terminal looks it up in a character encoding and draws the glyph for 99, which in ASCII is a lowercase c.
ASCII is the mapping almost every system uses for these first 128 values: 65 to 90 are A to Z, 97 to 122 are a to z, 48 to 57 are the digits. The C++ standard does not require ASCII, but it is what you will meet in practice, including on this machine.
Why does subtracting 32 make a capital?
In ASCII every lowercase letter sits exactly 32 (hex 20) above its capital: a is 97 and A is 65. The program checks it with 'a' - 'A', which prints 32. Character literals are integers too, so this is ordinary subtraction.

So s[0] -= 32 turns the byte 0x63 (99) into 0x43 (67), which is C, and printf("%s", s) prints Cat. Nothing about the letter was changed directly: one byte got a smaller number.
![After s[0] -= 32 the first byte is 0x43 and the string prints Cat, with the program chars.cpp below](https://vthvqgqmgbnywvxzqlws.supabase.co/storage/v1/object/public/blog-media/shorts/cpp-007-char-ascii/cat.jpg)
In real code, use std::toupper from <cctype> instead: it does not depend on ASCII and leaves characters that are not lowercase letters alone. The subtraction is here to show that a char is arithmetic on a byte.
Is plain char signed?
That depends on the platform. char, signed char and unsigned char are three distinct types, and whether plain char behaves like the signed or the unsigned one is implementation-defined. On x86-64 Linux with g++ it is signed, on ARM Linux it is usually unsigned. For values 0 to 127, like every byte in this program, it makes no difference. For raw binary data, use unsigned char or std::byte.
The program and its output
#include <cstdio>
int main() {
char s[] = "cat";
std::printf("%zu\n", sizeof(char));
std::printf("%zu\n", sizeof s);
for (char& b : s)
std::printf("%p %02X\n", (void*)&b, b);
std::printf("%d %d\n", s[0], 'a' - 'A');
s[0] -= 32;
std::printf("%s\n", s);
}
1
4
0x7ffd8995db24 63
0x7ffd8995db25 61
0x7ffd8995db26 74
0x7ffd8995db27 00
99 32
Cat
The four addresses are from the run shown; yours will differ, but they will still count up by one. The same output, apart from the addresses, comes from -O0.
Try it
In ASCII, what is 'z' - 'a'? Work it out from the table above (or from the fact that z is the last of 26 letters), then add a line to the program that prints it.