Curly braces {} and blocks
What you'll learn: How to use curly braces to group multiple lines of code into a single, organized unit called a block.
The boundaries of code
In C, curly braces {} act like the walls of a room. In plain English, we use punctuation like periods to end a sentence and paragraphs to group ideas. In C, we use curly braces to tell the computer, "Everything inside these two marks belongs together."
Whenever you see a {, a new block of code begins. Whenever you see the matching }, that block ends. These are not just decorative; they define the structure and scope of your program.
Think of a shipping container
Imagine you are moving houses. You have many individual items: a toaster, a kettle, and a lamp. If you try to ship them individually, they might get lost or separated. Instead, you put them all inside one large cardboard box.
In C, a function like main() is like that box. The computer needs to know exactly which instructions belong to that function and which do not. By wrapping your code in braces, you turn five or ten individual instructions into one single "package" that the function can execute.
What a block looks like
In the example below, look at how the braces wrap the instructions. Everything inside the braces is part of the main block.
#include <stdio.h>
int main() {
// The opening brace starts the block
printf("This is inside the block.\n");
printf("This is also inside the same block.\n");
return 0;
// The closing brace ends the block
}
Why indentation matters
You will notice that the code inside the braces is shifted to the right (usually by four spaces). While the C compiler doesn't technically require this "indentation" to run the code, humans do. Indenting code inside a block is a universal rule of politeness in programming. It makes it visually obvious where a "room" begins and ends, helping you spot mistakes if you forget to close a brace.
Key takeaway
Curly braces { } create blocks that group multiple lines of code into a single unit.