char
- C course
- Beginner
- Lesson 15/116
- 3 min
Learn to store single characters with char.
The char type
The char type stores a single character. Character constants are written in single quotes:
char letter = 'A';
char digit = '7';Each character is actually an integer; 'A' has the value 65 (ASCII table). So you can do arithmetic on char: 'A' + 1 gives 'B'.
char and ASCII
Each character has an integer value in the ASCII table. 'A'=65, 'a'=97, '0'=48. So you can print a char both as a character with %c and as an integer with %d:
char c = 'A';
printf("%c %d", c, c);
// Output: A 65The difference between upper and lower case is fixed: 'a' - 'A' = 32.
Practice Print char and ASCII
Add a printf call to the program below: print c with both %c and %d specifiers. The output should be A 65.
Starter code
#include <stdio.h>
int main(void) {
char c = 'A';
// print char and ASCII value
return 0;
}Sample solution
#include <stdio.h>
int main(void) {
char c = 'A';
printf("%c %d", c, c);
return 0;
}Expected output
A 65
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which quotes are used for char?
- "..."
- [...]
- <...>
- '...' Correct answer
Explanation: char values use single quotes.
2 Declare a char
char c = ____;
char c = 'A';
Explanation: char c = 'A'; assigns a single character.
3 Match char concepts
| Term | Meaning |
|---|---|
'A' |
char constant |
65 |
ASCII value |
%c |
Print character |
char |
1 byte |