C Characters
The char type stores a single character, such as a letter, a digit symbol, or a punctuation mark. Behind the scenes C actually stores characters as small integer codes, which leads to some surprising and useful behaviour.
Storing a Character
A char value is written between single quotes, like 'A' or '?'. Be careful: single quotes are for one character, while double quotes are for strings of text.
Printing a character
#include <stdio.h>
int main() {
char letter = 'H';
printf("The letter is %c\n", letter);
return 0;
}Characters Are Numbers
Every character is stored as a number using the ASCII table. For example the letter 'A' is stored as 65 and 'a' is stored as 97. If you print a char with %d instead of %c, you will see that number.
Seeing the ASCII value
#include <stdio.h>
int main() {
char c = 'A';
printf("Character: %c\n", c);
printf("ASCII value: %d\n", c);
return 0;
}Doing Math with Characters
Because characters are numbers, you can do arithmetic on them. Adding 1 to 'A' gives you 'B'. This trick is handy for moving through the alphabet.
Adding to a character
#include <stdio.h>
int main() {
char first = 'A';
char next = first + 1;
printf("%c comes before %c\n", first, next);
return 0;
}Some ASCII values