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 (a standard that gives each character a number), 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\n", a);
printf("%c\n", b);
printf("%c", c);
Try it Yourself »
Tip: A list of all ASCII values can be found in our ASCII Table Reference.
Characters Are Stored as Numbers
Inside the computer, every character is stored as a number. For example, 'A' is stored as 65.
If you print a char with %d instead of %c, you see the number:
Example
char myLetter = 'A';
printf("%c\n", myLetter); // A
printf("%d", myLetter); // 65
Try it Yourself »
Choose a character below to see which number it is stored as:
Code
Output
In memory
Explanation
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.