Course contentsShow
← C
Lesson 418 of 1,2196. Arrays and StringsFree lesson

Difference between `'a'` and `"a"`

Difference between 'a' and "a"

What you'll learn: How to distinguish between a single character literal and a string literal in C.

When you first start working with text in C, single quotes and double quotes might look interchangeable. However, to the C compiler, 'a' and "a" are entirely different beasts. Mixing them up is one of the most common causes of compiler errors for beginners.

The Single Character: 'a'

Think of a single character literal (using single quotes) as a single marble. It is a primitive data type called a char.

In C, a char is actually just a small integer. When you write 'a', the computer sees the number 97 (its ASCII value). It takes up exactly one byte of memory. You can store it in a simple variable like this: char letter = 'a';

The String: "a"

Think of a string literal (using double quotes) as a box containing a marble. Even if there is only one letter inside, it is still a "container" known as an array of characters.

C strings have a special requirement: they must end with a hidden marker called the null terminator (\0). This tells the computer where the text stops. So, while 'a' is just one byte, "a" actually takes up two bytes of memory: the letter 'a' and the invisible \0 terminator.

The Concrete Difference

Because a string is an array and a character is a simple number, you cannot use them the same way. You can do math with 'a', but you cannot do math with "a".

#include <stdio.h>

int main() {
    // A single character: Just the value 97
    char singleChar = 'a'; 

    // A string: A memory address pointing to 'a' and '\0'
    char myString[] = "a"; 

    printf("Character size: %zu byte\n", sizeof(char));
    printf("String size: %zu bytes (includes null terminator)\n", sizeof("a"));

    return 0;
}

If you try to pass "a" into a function that expects a single char, the compiler will complain because you are trying to hand someone an entire box when they only asked for a single marble.

Key takeaway

Use single quotes (' ') for individual characters and double quotes (" ") for sequences of text, even if that sequence is only one letter long.