C Character Data Type
In this page:
The char Type
The char type stores a single character, occupying exactly one byte of memory, and is used for individual letters, digits, symbols, or small integer values across C programs.
Example: The char Type
#include <stdio.h>
int main() {
char letter = 'B';
printf("%c, size: %zu byte", letter, sizeof(letter));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Character Literals
A character literal is written between single quotes, such as A or 7, and can also represent special characters using escape sequences like '\t' for tab or '\n' for newline.
Example: Character Literals
#include <stdio.h>
int main() {
char digit = '7';
char tab = '\t';
printf("%c%c%c", digit, tab, digit);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Characters as Small Integers
Internally, a char is stored as a small integer representing its ASCII code, which means arithmetic operations work directly on characters, letting code shift from one letter to another by adding or subtracting numbers.
Example: Characters as Small Integers
#include <stdio.h>
int main() {
char letter = 'A';
char next = letter + 1;
printf("%c", next);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
signed char vs unsigned char
Whether char is signed or unsigned by default depends on the compiler, so signed char and unsigned char explicitly choose between a range that includes negative numbers or one that extends further into positive numbers.
Example: signed char vs unsigned char
#include <stdio.h>
int main() {
signed char s = -100;
unsigned char u = 200;
printf("%d %d", s, u);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Printing and Reading Characters
The %c format specifier is used with both printf to display a single character and scanf to read one from input, working consistently across writing and reading character data.
Example: Printing and Reading Characters
#include <stdio.h>
int main() {
char c;
sscanf("Q", "%c", &c);
printf("%c", c);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers