Blocks and Indentation

A block can contain as many statements as you need. Everything between { and } belongs to the block, and when the condition is true, all of it runs, one statement after another:

int temperature = 32;

if (temperature > 30) {
    printf("It's very hot\n");
    printf("Drink plenty of water\n");
}
printf("Have a nice day\n");

This prints:

It's very hot
Drink plenty of water
Have a nice day

The block ends with the closing curly brace }. The line that prints Have a nice day comes after it, isn't part of the block, and therefore always runs, whether the condition is true or not.

The lines of the block are indented: they start with 4 spaces. The compiler doesn't care about indentation. Only the curly braces decide what belongs to the block, and the program would work even if you wrote it without any indentation at all. Indentation is for people: it shows at a glance which lines belong to the block, which is why programmers always use it. You don't have to count spaces. When you press Enter after a {, the editor indents the next line for you, and the Tab key inserts 4 spaces too.

Since the braces decide what's in the block, not the indentation, indentation can even mislead you:

int temperature = 20;

if (temperature > 30) {
    printf("It's very hot\n");
}
    printf("Drink plenty of water\n");

This prints:

Drink plenty of water

The last line of code is indented, but it comes after the }, so it isn't part of the block and runs even at 20 degrees.

The same happens if you leave out the curly braces. C allows that, but then only the single statement right after the condition belongs to the if:

int temperature = 20;

if (temperature > 30)
    printf("It's very hot\n");
    printf("Drink plenty of water\n");

This prints:

Drink plenty of water

The second printf doesn't belong to the condition, however it's indented. Code like this usually starts out right, with a single statement and no braces, and goes wrong later, when someone adds a second line to the block and forgets to add the braces. That's why this course always writes the curly braces, even around a single statement.

If you forget the closing brace }, the compiler reports error: expected '}' at the last line of the program, because only there does it find out that a brace is missing. Look for it higher up, though: after the last line of the block you didn't close. The note below the error, to match this '{', may point to the { after int main(void): the compiler pairs the braces in order, so the } that was meant for main closed your block instead.

Instructions

The program tells you to connect the charger even though the battery is 80% full. The line that prints Connect the charger is indented, but it comes after the }. Move the } below it so that the line belongs to the block of the if statement. Then the program prints only Battery: 80%.

Change battery to 15 and run the program. Now the condition is true, and the program prints all three lines:

Low battery
Connect the charger
Battery: 15%

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

2/12