C++ char Data Type
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
char letter = 'A';
char next = letter + 1; // shifts to 'B' via ASCII arithmetic
std::cout << next << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Using double quotes for a character, like
char c = "A";, which is a string literal and does not compile; a char literal needs single quotesA. - Expecting
cout << c + 1to print a character, when adding an int promotes the result tointand prints a number. - Assuming plain
charis always signed, when whether it is signed or unsigned depends on the platform.
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: