How to Declare and Use Character Variables in C Programming
C Basic Concepts
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.
Syntax of Declaring Character Variable in C
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.
Example 1
Declare a character variable x.
char x;
Example 2
Declare 3 character variables x, y, and z to assign 3 different characters in it.
char x='A', y='5', z='#';
Note: A character can be anything, it can be an alphabet or digit or any other symbol, but it must be single in quantity.
Example 3
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 @
Test Your Knowledge
Attempt the multiple choice quiz to check if the lesson is adequately clear to you.