C Character Data Types
The char Type
The char
data type is used to store a
single character.
The character must be surrounded by single quotes, like 'A' or 'c', and we use the %c
format specifier to print it:
Alternatively, if you are familiar with ASCII, you can use ASCII values to display certain characters. Note that these values are not surrounded by quotes (''
),
as they are numbers:
Example
char a = 65, b = 66, c = 67;
printf("%c", a);
printf("%c", b);
printf("%c", c);
Try it Yourself »
Tip: A list of all ASCII values can be found in our ASCII Table Reference.
Notes on Characters
If you try to store more than a single character, it will only print the last character:
Note: Don't use the char
type for storing multiple
characters, as it may produce errors.
To store multiple characters (or whole words), use strings (which you will learn more about in a later chapter):
For now, just know that we use strings for storing multiple characters/text, and the char type for single characters.