Chapter 02 of 44

C Program Structure

Before writing bigger C programs, it's important to understand the basic structure of a C program.

Don't worry—it's actually quite simple. A C program is mainly made up of different sections, and each section has a specific purpose.

Let's start with a simple program:

#include <stdio.h>

int main()
{
    printf("Hello, World!");

    return 0;
}

Now let's understand each part.

1. Header File

#include <stdio.h>

stdio.h stands for Standard Input Output.

It provides functions such as printf() and scanf() that are commonly used for displaying output and taking input.

The #include line is a preprocessor directive, not a statement (that is why it has no semicolon). It tells the preprocessor to copy the contents of this header file into the program before compilation. stdio.h contains the declarations of functions like printf(), so the compiler knows how to call them.


2. main() Function

int main()
{
    // program code
}

The main() function is the starting point of a C program.

When we run the program, execution begins from main().

Think of it like the entry point of your program.


3. Statements

Inside main(), we write the instructions that the computer needs to execute.

printf("Hello, World!");

This is a statement that displays text on the screen.

Most C statements end with a semicolon (;).


4. Curly Braces { }

int main()
{
    printf("Hello");
}

Curly braces define the beginning and end of a block of code.

Everything between { and } belongs to the main() function.


5. return 0

return 0;

This returns a value from the main() function and generally indicates that the program finished successfully.


Basic Structure at a Glance

You can remember the basic structure like this:

Header Files
     ↓
main() Function
     ↓
Statements
     ↓
return 0

A simple C program therefore looks like:

#include <stdio.h>

int main()
{
    // Statements

    return 0;
}

One Important Thing

Not every C program will look exactly the same. Larger programs can contain multiple functions, variables, comments, header files, structures, and other components.

But for a beginner, understanding this basic structure is enough to start writing C programs confidently.