A char type is referred as character data type and is used to store a single character.
This type is 1 byte size (8 bits wide), it's guarantee to store a hole single character on a machine. The character can be lowercase for example 'a' or uppercase for example 'R'.
A character is enclosed in a single quote like 'c' or 'C' or 'A' etc..
Declare and initialize
Here is how to declare and initialize a character :
#include <iostream>
int main() {
char myChar; // declare a single char
myChar = 'R'; // initialiaze the single character
return 0;
}
Print a character
To print the value of a char :
#include <iostream>
int main() {
char myChar; // declare a single char
myChar = 'R'; // initialiaze the single character
// print the value of myChar
std::cout << "The value of myChar = " << myChar << std::endl;
return 0;
}
Modify a character
Once initialized a char, we can change it's value again, for example :
Here we change the value of myChar from 'R' to 'a'.
#include <iostream>
int main() {
char myChar; // declare a single char
myChar = 'R'; // initialiaze the single character
std::cout << "Initial value of myChar = " << myChar << std::endl;
myChar = 'a';
std::cout << "New value of myChar = " << myChar << std::endl;
return 0;
}
Character and integer