The char Data Type
- What is char?
The char (short for character) data type is used to store a single character, such as a letter, digit, or symbol.
It always takes 1 byte (8 bits) of memory.
Each char value is stored as an ASCII number (American Standard Code for Information Interchange).
🧩 Example 1 – Single Character
#include <stdio.h>
int main() {
char myGrade = ‘A’; // Single character inside single quotes
printf(“%c”, myGrade);
return 0;
}
✅ Output:
A
🔹 2. Characters & ASCII Values
Every character in C is represented internally by an ASCII value (a numeric code).
For example:
‘A’ → 65
‘B’ → 66
‘C’ → 67
You can even store and print them using their ASCII numbers!
🧩 Example 2 – Using ASCII Values
#include <stdio.h>
int main() {
char a = 65; // ASCII for ‘A’
char b = 66; // ASCII for ‘B’
char c = 67; // ASCII for ‘C’;
printf(“%c”, a);
printf(“%c”, b);
printf(“%c”, c);
return 0;
}
✅ Output:
ABC
💡 Tip: You can view all ASCII values in a reference table (0–127 standard characters).
- What Happens If You Store More Than One Character?
If you try this 👇
char myText = ‘Hello’;
printf(“%c”, myText);
👉 It will only print the last character, ‘o’,
because the char data type can store only one character.
⚠️ Important Note
Don’t use char for storing multiple letters or words — that’s where strings come in.
Example 3 – Using a String Instead
#include <stdio.h>
int main() {
char myText[] = “Hello”; // String = multiple characters
printf(“%s”, myText);
return 0;
}
✅ Output:
Hello
🧩 Quick Summary
|
Purpose |
Data Type |
Example Value |
Format Specifier |
|
Single character |
char |
‘A’, ‘9’, ‘#’ |
%c |
|
Text / Multiple characters |
char[] (string) |
“Hello” |
%s |
✅ Key Points to Remember
Always use single quotes (‘ ‘) for single characters.
Always use double quotes (” “) for strings.
char variables use ASCII values internally.
%c prints a single character.
%s prints a string.