← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 12 of 15

C++ char Data Type

The char type stores a single character in one byte, internally represented as a small integer (its ASCII code), used both for text and, when needed, small numeric values.

The char Type

The char type stores a single character, occupying exactly one byte of memory, and is used for individual letters, digits, symbols, or small integer values across C++ programs.

Example: The char Type

cpp
#include <iostream>

int main() {
	char initial = 'D';
	std::cout << initial << std::endl;
	return 0;
}

Character Literals

A character literal is written between single quotes, such as A or 7, and can also represent special characters using escape sequences like '\t' for tab or '\n' for newline.

Example: Character Literals

cpp
#include <iostream>

int main() {
	char letter = 'A';
	char tab = '\t';
	char newline = '\n';
	std::cout << letter << tab << "end" << newline;
	return 0;
}

Characters as Small Integers

Internally, a char is stored as a small integer representing its ASCII code, which means arithmetic operations work directly on characters, letting code shift from one letter to another by adding or subtracting numbers.

Example: Characters as Small Integers

cpp
#include <iostream>

int main() {
	char letter = 'A';
	char next = letter + 1; // shifts to 'B' via ASCII arithmetic
	std::cout << next << std::endl;
	return 0;
}

signed char vs unsigned char

Whether char is signed or unsigned by default depends on the compiler, so signed char and unsigned char explicitly choose between a range that includes negative numbers or one that extends further into positive numbers.

Example: signed char vs unsigned char

cpp
#include <iostream>

int main() {
	signed char s = -100;
	unsigned char u = 200;
	std::cout << (int)s << " " << (int)u << std::endl;
	return 0;
}

char with cin and cout

Reading and writing a single char with cin and cout works directly, without any special format specifier, since C++'s stream operators automatically know how to handle each type.

Example: char with cin and cout

cpp
#include <iostream>

int main() {
	char grade = 'B';
	std::cout << "Grade: " << grade << std::endl;
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.