Course contentsShow
← C
Lesson 105 of 1,2192. Variables and TypesFree lesson

The `char` type

The char type

What you'll learn: How the char type allows C to store letters and symbols by treating them as small numbers from the ASCII table.

Letters are just numbers in disguise

In C, the char data type is used to store a single character, such as 'A', 'z', or '$'. However, here is the secret: computers don't actually know what a letter is. They only understand numbers.

Think of a char like a small locker that can hold one integer. To represent text, C uses a standard "translation manual" called the ASCII table. In this system, every character is assigned a specific number. For example, the uppercase letter 'A' is actually the number 65, and a space character is the number 32. Because of this, a char is essentially just a very small integer type.

Using single quotes

When you want to assign a character to a variable, you must wrap it in single quotes (' '). Double quotes are reserved for strings (text sequences), which C treats differently. Using single quotes tells the compiler, "Look up the ASCII number for this symbol and store it."

#include <stdio.h>

int main() {
    // Storing the letter 'B'
    char myGrade = 'B';

    // We can print it as a character (%c) 
    // or see its secret number (%d)
    printf("The character is: %c\n", myGrade);
    printf("Its ASCII value is: %d\n", myGrade);

    return 0;
}

Character arithmetic

Because characters are numbers under the hood, you can actually perform math with them. If you have the character 'A' (65) and add 1 to it, the variable becomes 66, which represents 'B'. This is incredibly useful for tasks like converting lowercase letters to uppercase or cycling through the alphabet.

While a char takes up very little memory (usually just 1 byte), it is the foundation of every word, sentence, and paragraph you will eventually process in your programs.

Key takeaway

A char stores a single symbol using single quotes, but the computer sees it as a numeric code from the ASCII table.