Table of Contents
The char data type stores a single character value. Characters in C++ are enclosed in single quotes and are internally represented using their corresponding integer character codes.
This example demonstrates how a character is stored in a char variable and displayed using the standard output stream.
Source Code #
#include <iostream>
int main()
{
char grade = 'A';
std::cout << "Grade: " << grade << '\n';
return 0;
}
Output #
Grade: A
Explanation #
char grade = 'A';declares a character variable and initializes it with the character'A'.- Character literals are enclosed in single quotes (
' '), distinguishing them from string literals, which use double quotes (" "). - A
charvariable stores only one character at a time. - Internally, the character
'A'is stored as its corresponding character code. The output stream automatically displays it as the character rather than its numeric value. std::cout << "Grade: " << grade << '\n';prints the text followed by the character stored in the variable.return 0;terminates the program successfully.