Character Variable in C++ Programming
In this lesson, we will discover how to declare and use character variables in C++ programming with this practical guide. Get hands-on with examples and take a quiz to reinforce your understanding.
What is Character Variable
In C++ programming, a character variable can hold a single character enclosed within single quotes. To declare a variable of this type, we use the keyword char, which is pronounced as kar. With a char variable, we can store any character, including letters, numbers, and symbols.
char variable_name;
Here char is used for declaring Character data type and variable_name is the name of variable (you can use any name of your choice for example: a, b, c, alpha, etc.) and ; is used for line terminator (end of line).
Now let's see some examples for more understanding.
Declare a character variable x.
char x;
Declare 3 character variables x, y, and z to assign 3 different characters in it.
char x='A', y='5', z='#';
Declare a character variable x and assign the character '$' and change it value to '@' in the next line.
char x = '$';
x = '@'; //now the new value of x is @
A character can be anything, it can be an alphabet or digit or any other symbol, but it must be single in quantity.
Test Your Knowledge
Attempt the questions in the exercise to check whether you have understood the lesson clearly.