← Back to C Course | Chapter 1: Introduction & Basics | Lesson 10 of 21

C Character Data Type

The char type stores a single character in one byte, internally represented as a small integer (its ASCII code), and is used both for text and, when needed, small numeric values.

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

c
#include <stdio.h>
int main() {
	char letter = 'B';
	printf("%c, size: %zu byte", letter, sizeof(letter));
	return 0;
}

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

c
#include <stdio.h>
int main() {
	char digit = '7';
	char tab = '\t';
	printf("%c%c%c", digit, tab, digit);
	return 0;
}

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

c
#include <stdio.h>
int main() {
	char letter = 'A';
	char next = letter + 1;
	printf("%c", next);
	return 0;
}

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

c
#include <stdio.h>
int main() {
	signed char s = -100;
	unsigned char u = 200;
	printf("%d %d", s, u);
	return 0;
}

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

c
#include <stdio.h>
int main() {
	char c;
	sscanf("Q", "%c", &c);
	printf("%c", c);
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.