← 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.
Syntax
c
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

c
// 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;
}

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 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;
}

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 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;
}

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 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;
}

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 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing char c = "A"; with double quotes, when a character needs single quotes like A.
  2. Printing a char with %d and being surprised by a number, when %d shows its integer code and %c shows the character.
  3. Assuming plain char is always signed, when that is implementation-defined.

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.