C Character Data Type
In this page:
char variable_name = 'character';
char escape_name = '\\n';
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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare letter and set it to 'B'
char letter = 'B';
// Print formatted text to the screen
printf("%c, size: %zu byte", letter, sizeof(letter));
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare digit and set it to '7'
char digit = '7';
// Declare tab and set it to '/t'
char tab = '\t';
// Print formatted text to the screen
printf("%c%c%c", digit, tab, digit);
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare letter and set it to 'A'
char letter = 'A';
// Declare next and set it to letter + 1
char next = letter + 1;
// Print formatted text to the screen
printf("%c", next);
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare s and set it to -100
signed char s = -100;
// Declare u and set it to 200
unsigned char u = 200;
// Print formatted text to the screen
printf("%d %d", s, u);
// Return 0 to signal that the program finished successfully
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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
char c;
sscanf("Q", "%c", &c);
// Print formatted text to the screen
printf("%c", c);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Writing
char c = "A";with double quotes, when a character needs single quotes likeA. - Printing a
charwith%dand being surprised by a number, when%dshows its integer code and%cshows the character. - Assuming plain
charis always signed, when that is implementation-defined.
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