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

Defining strings with double quotes

Defining strings with double quotes

What you'll learn: How to use double quotes to automatically create null-terminated character arrays in C.

The shorthand for text

Up to this point, you might have thought of arrays as a collection of numbers. But in C, text is simply an array of individual characters. While you could define a word by typing out every single letter in single quotes—like {'H', 'e', 'l', 'l', 'o'}—that would be incredibly tedious.

To make life easier, C provides a shorthand: the string literal. By surrounding text in double quotes ("Hello"), you tell the compiler to handle the heavy lifting for you.

The hidden "Null Terminator"

There is a secret rule in C: every string must have a way to signal that it is finished. Imagine reading a book where the pages don't end, and there is no back cover. You wouldn't know when to stop reading!

When you use double quotes, C automatically adds a hidden character at the very end called the null terminator, written as \0. This character has a numerical value of zero and acts like a "Stop" sign for the computer.

If you type "Cat", the computer actually stores four items in memory:

  1. 'C'
  2. 'a'
  3. 't'
  4. '\0' (The hidden stop sign)

Declaring a string in code

When you define a string array using double quotes, you don't even have to tell C how long the array is; if you leave the square brackets [] empty, the compiler counts the letters (plus that hidden \0) and sets the size automatically.

#include <stdio.h>

int main() {
    // C counts the 5 letters + 1 null terminator automatically
    char greeting[] = "Hello"; 

    // We can print it using the %s (string) format specifier
    printf("My message is: %s\n", greeting);

    return 0;
}

Single vs. Double Quotes

It is vital to remember the difference:

  • Single quotes ('A') are for a single character.
  • Double quotes ("A") are for strings.

Even if your string is only one letter long, "A" is still an array containing 'A' and '\0'. Using the wrong one is like trying to fit a whole book (a string) into a single picture frame (a character).

Key takeaway

Double quotes are a shortcut for creating character arrays that automatically include a hidden \0 (null terminator) to mark the end of the text.