Unit 1 · Lesson 118 minAcademic review pending

The shape every C program has

Before syntax, learn the fixed skeleton a C program always follows, and why the compiler needs it in that order.

Choose explanation

After this lesson

You should be able to

  • Name the four parts of a C program: preprocessor directives, the main function, declarations, and executable statements.
  • Explain why declarations must appear before the statements that use them.
  • Predict what the compiler reports when a required part is missing.
01

A program is a recipe with a fixed header

Every C program you write this semester has the same outer shape. At the top come preprocessor directives — lines starting with # that pull in code written by someone else. Then comes main, the function where execution begins. Inside main, you first declare the variables you will need, then write the executable statements that do the work, and finally return a value to the operating system.

This is not decoration. The C compiler reads your file once, from top to bottom. It cannot use a name it has not seen yet, and it does not go back to look. The shape exists because of how the compiler reads, not because of style.

The four parts, in order
#include <stdio.h>

int main(void)
{
    int marks;

    marks = 87;
    printf("Marks = %d\n", marks);

    return 0;
}
02

What each line is actually doing

#include <stdio.h> runs before compilation proper. The preprocessor replaces that one line with the entire contents of stdio.h, which is what tells the compiler that a function named printf exists and what arguments it takes. Without it, the compiler meets printf as a complete stranger.

int main(void) declares the starting point. int says main hands back a whole number when it finishes; void says it takes no input. The braces { and } mark where the function body begins and ends.

return 0; reports success to whatever started the program. Zero means finished normally. Any other value conventionally signals a problem.

03

Reading the error instead of guessing

Beginners lose hours guessing at errors. Read the first one only. If you delete the #include line above, the compiler reports something close to implicit declaration of function 'printf' — it is telling you exactly which name it has not been introduced to. Fix the first error, recompile, and read again; one real mistake often produces ten complaints.

Compile with warnings switched on from the very first program. gcc -Wall -Wextra tells you about mistakes that are legal C but almost never intended, and it costs nothing to type.

Try it yourself

Write the smallest complete C program that prints your name on one line, and explain what each of the four parts contributes.

Need a hint?

You need no variables at all for this. A declaration section is allowed to be empty.

Check the worked solution

The #include introduces printf. int main(void) is where execution starts. There are no declarations because no value is stored. The single printf is the only executable statement, and return 0 reports success. The \n matters — without it the shell prompt appears on the same line as your output.

#include <stdio.h>

int main(void)
{
    printf("Kavya\n");
    return 0;
}

Quick check

Why must #include <stdio.h> appear before main rather than after it?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

C Language Elements · General Form of a C Program · Executable Statements

Maps to course outcomes CO2, CO3.