Program structure, main() & comments
7 min lesson
- A C program is made of functions, and every program must have exactly one main function where execution begins.
- The structure of a simple program is: include headers, define main, write statements inside braces, and return a value.
- Headers like <stdio.h> give you access to library functions; you include them at the top of the file.
- Comments are notes for humans that the compiler ignores; they explain what the code does and why.
- C supports two comment styles: // for single-line comments and /* ... */ for multi-line comments.
- Good comments explain the purpose, not just repeat the code; they help you and others understand the program later.
- The main function returns an int to the operating system; return 0; means success, and other values can signal errors.
- Statements inside main are executed in order, from top to bottom, unless you use control flow like if or loops (covered later).
- Whitespace (spaces, tabs, newlines) is mostly ignored by the compiler, but consistent indentation makes code readable.
- Preprocessor directives like #include start with # and are handled before compilation.
- This lesson uses no code because the focus is on structure and comments, which you will practice in every future program.
- Every C program needs exactly one main function; execution starts there.
- Use // for short comments and /* */ for longer explanations.
- Include headers at the top and end main with return 0;.
- Write comments that explain why, not just what; future you will thank you.
Breaking it down
Preprocessor directives
#include <stdio.h>Lines starting with # are handled by the preprocessor before compilation. #include brings in library declarations so the compiler knows about functions like printf.
main function
int main(void) { ... }This is the entry point. The operating system calls main when you run the program. Everything inside the braces runs in order.
Single-line comment
// This is a commentEverything after // on that line is ignored by the compiler. Use it for short notes about the next line or a quick explanation.
Multi-line comment
/* This comment
spans two lines */Everything between /* and */ is ignored. Use it for longer explanations or to temporarily disable a block of code.
Return statement
return 0;Ends main and sends a value to the operating system. 0 means success; any other value can indicate an error, which is useful in scripts.
A helpful analogy
A C program is like a recipe book. The headers are the list of ingredients you need, main is the recipe you are cooking, and comments are the notes in the margin that explain why you stir slowly or preheat the oven. The compiler is the chef who follows the recipe exactly, ignoring your margin notes but using the ingredients.
Nesting rules
- 1.Every opening brace { must have a matching closing brace } in the same function.
- 2.Preprocessor directives (#include) go at the top of the file, before any function definitions.
- 3.Comments can appear anywhere, but never nest /* */ comments inside each other; the first */ ends the comment.
Common mistakes
Putting statements outside any function
Avoid
printf("Hello");
int main(void) { return 0; }Use instead
int main(void) { printf("Hello"); return 0; }C requires all executable statements to be inside a function. Code outside main (or another function) causes a compile error.
Nesting block comments
Avoid
/* outer /* inner */ still outer */Use instead
// outer
// innerThe first */ closes the comment, leaving 'still outer */' as code, which causes a syntax error. Use // for nesting or avoid nested block comments.
Writing comments that just repeat the code
Avoid
// increment i by 1
i++;Use instead
// Move to the next student in the list
i++;Comments should explain why, not what. The code already says i++ ; a comment should give context the code cannot.
Quick tips
- •Start every program with a comment saying what it does; it helps you focus.
- •Use // for most comments; reserve /* */ for temporarily disabling code blocks.
- •Keep comments up to date: if you change the code, change the comment.
- •Indent code inside main by 4 spaces to make the structure visually clear.
- •If you get a strange error, check for a missing closing brace } or a stray comment symbol.