Characters as Integers
ASCII values.
Characters Are Numbers
In C, a char is just a small integer. Every character has a numeric value defined by the ASCII table.
The letter 'A' is stored as the number 65, 'B' is 66, and so on.
#include <stdio.h>
int main(void) {
char c = 'A';
printf("The character %c has value %d\n", c, c);
return 0;
}Printing the ASCII Value
Use the %d format specifier to print a char as its integer value, and %c to print it as a character.
The same variable can be viewed both ways.
#include <stdio.h>
int main(void) {
char letter = 'z';
printf("As char: %c\n", letter);
printf("As int: %d\n", letter);
return 0;
}All lessons in this course
- Characters as Integers
- The ctype Library
- Reading Characters
- Building Char Utilities