Lesson 4 of 60 – C Program Structure
7%

C Program Structure

Understanding the structure of a C program is one of the first important steps in learning C programming. A C program is made up of different parts such as header files, functions, statements, comments, and the main() function.

Each part has a specific purpose. Once you understand these parts, it becomes much easier to read, write, and debug C programs.

Note: Not every C program must contain exactly the same parts. The structure depends on what the program needs. The main() function is the normal entry point of a hosted C program.

1. Basic Structure of a C Program

A simple C program can look like this:

#include <stdio.h>

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

    return 0;
}

This small program contains a header file, the main() function, a statement, and a return statement.

2. Main Parts of a C Program

A typical C program may contain the following parts:

  • Comments
  • Preprocessor directives
  • Header files
  • Global declarations
  • Function declarations
  • The main() function
  • Local variables
  • Statements
  • User-defined functions

Some of these parts are optional depending on the requirements of the program.

3. Comments

Comments are used to add explanations or notes to source code. They are not treated as executable program instructions.

Single-line comment:

// This is a comment

Multi-line comment:

/*
   This is a
   multi-line comment
*/

Comments can make programs easier to understand and maintain.

4. Preprocessor Directives

Preprocessor directives begin with the # symbol.

For example:

#include <stdio.h>

The preprocessor processes directives before the compiler translates the main source code.

Common directives include #include and #define.

5. Header Files

Header files contain declarations and other information that can be shared with a C source file.

For example:

#include <stdio.h>

The stdio.h header is commonly used when a program needs standard input and output functions.

6. The main() Function

The main() function is the normal entry point of a hosted C program.

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

    return 0;
}

When the program starts, execution begins in the main() function.

7. Return Type of main()

A common and recommended form of the main function is:

int main()
{
    return 0;
}

The int indicates that main() returns an integer value to the environment that started the program.

8. The return Statement

The return statement ends a function and can provide a value to the caller.

int main()
{
    return 0;
}

Returning 0 from main() commonly indicates successful program termination.

9. Statements

Statements are instructions that perform actions in a C program.

printf("Hello");

int age = 20;

age = age + 1;

Many C statements end with a semicolon.

10. Semicolon in C

The semicolon ; is used to terminate many C statements.

int age = 20;

printf("%d", age);

return 0;

Forgetting a required semicolon can result in a compilation error.

11. Curly Braces

Curly braces { } define a block of code.

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

The statements between the braces belong to the function body.

12. Indentation

Indentation means using spaces or tabs to make the structure of code easier to read.

Example:

int main()
{
    int age = 20;

    if(age >= 18)
    {
        printf("Adult");
    }

    return 0;
}

Indentation does not normally change C program execution, but it greatly improves readability.

13. Variables Inside main()

Variables can be declared inside a function. Such variables are local to that function.

int main()
{
    int age = 20;
    float marks = 85.5;

    printf("%d", age);

    return 0;
}

14. Local Variables

A local variable is declared inside a function or block.

int main()
{
    int number = 10;

    printf("%d", number);

    return 0;
}

The variable number is local to the block in which it is declared.

15. Global Variables

A global variable is declared outside all functions.

#include <stdio.h>

int count = 10;

int main()
{
    printf("%d", count);

    return 0;
}

Its scope can make it accessible to multiple functions in the same source file, subject to the language's scope and linkage rules.

16. Function Declaration

A function declaration tells the compiler about a function before it is used.

#include <stdio.h>

int add(int, int);

int main()
{
    int result;

    result = add(10, 20);

    printf("%d", result);

    return 0;
}

int add(int a, int b)
{
    return a + b;
}

The declaration allows the compiler to know the function's name, parameters, and return type.

17. Function Definition

A function definition contains the actual code that performs the function's task.

int add(int a, int b)
{
    return a + b;
}

Here, the function calculates the sum of two integers and returns the result.

18. Calling a Function

A function is called by writing its name followed by parentheses and any required arguments.

int result;

result = add(10, 20);

The program transfers control to the function and receives its return value.

19. Complete Simple Program

#include <stdio.h>

int main()
{
    int a = 10;
    int b = 20;
    int sum;

    sum = a + b;

    printf("Sum = %d", sum);

    return 0;
}

This program contains a header file, the main function, variable declarations, an expression, an output statement, and a return statement.

20. Program Execution Flow

A simple C program can be understood through this general flow:

Source Code
     ↓
Preprocessor
     ↓
Compiler
     ↓
Object Code
     ↓
Linker
     ↓
Executable Program
     ↓
Execution

The exact build process can vary depending on the compiler and environment.

21. Preprocessor Stage

During preprocessing, directives such as #include and #define are handled.

#include <stdio.h>
#define PI 3.14

The preprocessor produces source code that is then passed to later compilation stages.

22. Compilation Stage

The compiler translates C source code into a lower-level form suitable for later stages of the build process.

If the source code contains syntax or other compile-time errors, the compiler can report them.

23. Linking Stage

The linker combines compiled code with required libraries and other object files to create the final executable or program image.

For example, a program using standard library functions may need the appropriate runtime and library components during the linking stage.

24. Execution Stage

After the program has been successfully built, the resulting executable can be run.

Build
  ↓
Executable
  ↓
Run Program
  ↓
Output

For example, a program containing printf() may display text on the terminal.

25. C Program with Input

A C program can accept input from the user.

#include <stdio.h>

int main()
{
    int age;

    printf("Enter your age: ");

    scanf("%d", &age);

    printf("Your age is %d", age);

    return 0;
}

Here, scanf() receives formatted input and stores it in the variable.

26. C Program with a Condition

#include <stdio.h>

int main()
{
    int age;

    printf("Enter age: ");
    scanf("%d", &age);

    if(age >= 18)
    {
        printf("Eligible");
    }
    else
    {
        printf("Not Eligible");
    }

    return 0;
}

This example demonstrates how variables, input, conditions, output, and the main function can work together.

27. Common Structure Mistakes

Beginners commonly make mistakes such as:

  • Forgetting a semicolon
  • Missing a closing brace
  • Using incorrect header names
  • Misspelling function names
  • Using an undeclared variable
  • Incorrectly placing parentheses
  • Incorrect capitalization
printf("Hello")

The above statement is missing a semicolon.

28. Recommended Basic Structure

For beginner programs, the following structure is a useful starting point:

#include <stdio.h>

int main()
{
    // Variable declarations

    // Program statements

    // Output

    return 0;
}

As you learn more C, you will add functions, structures, header files, and other components when needed.

29. Example with Multiple Functions

#include <stdio.h>

int add(int a, int b);
void message();

int main()
{
    int result;

    message();

    result = add(10, 20);

    printf("Sum = %d", result);

    return 0;
}

void message()
{
    printf("Welcome to C\n");
}

int add(int a, int b)
{
    return a + b;
}

This example demonstrates a program containing a header, function declarations, main(), function calls, and user-defined functions.

30. Summary of C Program Structure

A C program is organized into different components that work together to perform a task.

Comments
   ↓
Preprocessor Directives
   ↓
Header Files
   ↓
Declarations
   ↓
main()
   ↓
Variables and Statements
   ↓
User-Defined Functions

The exact structure can vary from program to program. Small programs may contain only a few components, while larger applications can contain multiple source files, header files, structures, and many functions.

📌 Key Points

  • A C program can contain comments, directives, declarations, functions, and statements.
  • The main() function is the normal entry point of a hosted C program.
  • #include is commonly used to include header files.
  • stdio.h provides declarations for standard input and output functions.
  • Many C statements end with a semicolon.
  • Curly braces define blocks of code.
  • Variables can be local or global depending on where they are declared.
  • Functions help divide programs into smaller reusable units.
  • A function can have a declaration, definition, and function call.
  • A C program normally goes through preprocessing, compilation, and linking before execution.
  • Proper indentation makes code easier to read.
  • The structure of a C program depends on its requirements.

🧠 Quick Quiz

Question: Which function is the normal entry point of a hosted C program?