C++ char Keyword
The char keyword in C++ is used to declare variables that store a single character. It represents a data type that typically requires 1 byte of memory and is capable of storing 256 different values, including both printable and non-printable characters. Characters are enclosed in single quotes (' '), and the underlying type of a char is an integer, which means it can also store ASCII values.
The char data type is essential for handling text, characters, and working with strings in C++.
Syntax
char variable_name = 'character';
- char
- The keyword used to declare a character variable.
- variable_name
- The name of the variable to store the character.
- character
- A single character or its corresponding ASCII value enclosed in single quotes (
' ').
Examples
Example 1: Declaring and Printing a Character
This example demonstrates how to declare a char variable and print its value.
#include <iostream>
using namespace std;
int main() {
char ch = 'A'; // Declare a char variable
cout << "The character is: " << ch << endl;
return 0;
}
Output:
The character is: A
Explanation:
- The
charvariablechis declared and initialized with the character'A'. - The
coutstatement prints the value ofchto the console.
Example 2: Storing and Printing ASCII Values
This example demonstrates how char variables can store and print ASCII values.
#include <iostream>
using namespace std;
int main() {
char ch = 65; // ASCII value for 'A'
cout << "The character for ASCII 65 is: " << ch << endl;
return 0;
}
Output:
The character for ASCII 65 is: A
Explanation:
- The
charvariablechis initialized with the ASCII value65. - When printed,
chdisplays the character corresponding to ASCII 65, which is'A'.
Example 3: Character Arithmetic
This example shows how arithmetic operations can be performed on char variables.
#include <iostream>
using namespace std;
int main() {
char ch = 'A';
ch = ch + 1; // Increment character
cout << "The next character is: " << ch << endl;
return 0;
}
Output:
The next character is: B
Explanation:
- The
charvariablechis initialized with'A'. - The statement
ch = ch + 1increments the ASCII value of'A'by 1, resulting in'B'. - The updated value of
chis printed, displaying'B'.
Example 4: Working with Escape Sequences
This example demonstrates the use of special characters and escape sequences in char variables.
#include <iostream>
using namespace std;
int main() {
char newline = '\n'; // Newline escape sequence
char tab = '\t'; // Tab escape sequence
cout << "Hello," << newline << tab << "World!" << endl;
return 0;
}
Output:
Hello,
World!
Explanation:
- The
charvariablenewlinestores the newline escape sequence'\n'. - The
tabvariable stores the tab escape sequence'\t'. - The program uses these variables to format the output, introducing a line break and a tab space between “Hello,” and “World!”.
Key Points about char Keyword
- The
charkeyword declares variables that store single characters or their ASCII values. - Characters must be enclosed in single quotes (
' '). - The
chardata type can store both printable and non-printable characters, including escape sequences. - Arithmetic operations on
charvariables manipulate their ASCII values. charis integral to working with strings, formatting, and character-based operations in C++.
