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

The ASCII encoding scheme

The ASCII encoding scheme

What you'll learn: How computers use the ASCII table to translate numeric values into the letters and symbols we see on the screen.

Computers only speak in numbers

At their core, computers are just massive collections of switches that are either "on" or "off." They don't actually know what the letter 'A' or a dollar sign '$' is. To solve this, computer scientists created a secret codebook called ASCII (American Standard Code for Information Interchange).

Think of ASCII like a restaurant menu where every dish has a number. If you tell the waiter you want a "Number 65," they know exactly which meal you mean. In C, when you store a character, the computer isn't storing a tiny picture of a letter; it is storing the specific number assigned to that letter in the ASCII codebook.

The ASCII Table

The standard ASCII table assigns a value from 0 to 127 to specific characters. For example:

  • The number 65 represents the uppercase 'A'.
  • The number 97 represents the lowercase 'a'.
  • The number 48 represents the digit '0'.

This is why the char type in C is so special. Even though we treat it like a letter, C sees it as a small integer. Because of this, you can actually perform math on letters! If you take the character 'A' (65) and add 1 to it, you get 66, which is the ASCII code for 'B'.

Seeing ASCII in action

In C, we use single quotes to tell the compiler to look up the ASCII value for a character.

#include <stdio.h>

int main() {
    // We can store a letter directly
    char myLetter = 'A'; 
    
    // We can also store the ASCII number directly
    char myNumber = 65; 

    // Both will print 'A' because 65 is the code for 'A'
    printf("Character 1: %c\n", myLetter);
    printf("Character 2: %c\n", myNumber);

    // We can even see the underlying number!
    printf("The ASCII code for A is: %d\n", myLetter);

    return 0;
}

When you use %c in a print statement, you are telling C to "look up this number in the ASCII table and show me the symbol." When you use %d, you are saying "just show me the raw number."

Key takeaway

Characters in C are just numbers in disguise, mapped to specific symbols using the ASCII encoding standard.