Course contentsShow
← C
Lesson 36 of 1,2191. FoundationsFree lesson

Whitespace and indentation

Whitespace and indentation

What you'll learn: How to use empty space to make your code readable for humans without changing how the computer runs it.

The invisible organizer

In C, "whitespace" refers to spaces, tabs, and newlines (hitting Enter). To the C compiler—the tool that turns your text into a program—most whitespace is invisible. You could technically write your entire program on one never-ending horizontal line, and the computer wouldn't care.

However, humans are not computers. Imagine reading a book where there are no spaces between words, no paragraphs, and no margins. You could eventually decode it, but it would be exhausting. Whitespace acts like the "punctuation" of your code's layout, helping your eyes quickly find where a thought begins and ends.

Indentation: Showing hierarchy

The most important use of whitespace is indentation. In C, we use curly braces { } to group lines of code together. To make it clear which lines belong inside those braces, we "indent" them by moving them a few spaces to the right.

Think of a formal outline or a table of contents. If "Chapter 1" is the main heading, the sub-sections are tucked underneath it, shifted to the right. Indentation does the same thing for your logic.

#include <stdio.h>

int main() {
    // These lines are indented 4 spaces
    // It shows they "belong" inside the main function
    printf("Hello, world!");
    printf("Proper spacing makes this easy to read.");

    return 0;
}

Consistency is key

Whether you use two spaces, four spaces, or the Tab key doesn't matter to C. What matters is consistency. If you start a block of code with four spaces, stay with four spaces. If you jump around, the "shape" of your code becomes jagged and confusing, making it much harder to spot errors like a missing brace.

Use blank lines to separate different "paragraphs" of logic. Just as you wouldn't write a whole essay as one giant wall of text, you shouldn't crowd your code. Give your statements room to breathe.

Key takeaway

Whitespace is for humans; use indentation and blank lines to reveal the structure of your code at a glance.