← 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.
Syntax
cpp
char variable_name = 'character';
char escape_name = '\\n';

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 std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare initial and set it to 'D'
	char initial = 'D';
	// Print to the console with cout
	std::cout << initial << std::endl;
	// Return 0 to signal that the program finished successfully
	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 std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare letter and set it to 'A'
	char letter = 'A';
	// Declare tab and set it to '/t'
	char tab = '\t';
	char newline = '\n';
	// Print to the console with cout
	std::cout << letter << tab << "end" << newline;
	// Return 0 to signal that the program finished successfully
	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 std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare s and set it to -100
	signed char s = -100;
	// Declare u and set it to 200
	unsigned char u = 200;
	// Print to the console with cout
	std::cout << (int)s << " " << (int)u << std::endl;
	// Return 0 to signal that the program finished successfully
	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 std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare grade and set it to 'B'
	char grade = 'B';
	// Print to the console with cout
	std::cout << "Grade: " << grade << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using double quotes for a character, like char c = "A";, which is a string literal and does not compile; a char literal needs single quotes A.
  2. Expecting cout << c + 1 to print a character, when adding an int promotes the result to int and prints a number.
  3. Assuming plain char is always signed, when whether it is signed or unsigned depends on the platform.

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.