Nano Technology

Structure of C Program


1. What is the Structure of a C Program?

The structure of a C program refers to the standard layout or organization of different sections that make up a C program. Every C program follows a logical sequence of sections that helps the compiler understand how the program should be executed.

Understanding the structure of a C program is essential for beginners because it explains where to write statements, how functions are organized, and how the program execution begins.

Structure of C Program

2. Basic Components of a C Program

A typical C program consists of the following main components:

  • Documentation Section
  • Link Section
  • Definition Section
  • Global Declaration Section
  • main() Function
  • User-Defined Functions

Each of these sections has a specific purpose and contributes to the correct execution of the program.

3. Documentation Section

The documentation section contains comments that describe the program. It provides information such as the program name, author, date, and purpose.

This section is meant for human understanding and is ignored by the compiler.

// Program to add two numbers
// Author: IPLTS
// Date: 2025

4. Link Section (#include)

The link section tells the compiler to include header files required by the program. Header files contain predefined functions and macros.

#include <stdio.h>

For example, stdio.h provides functions like printf() and scanf().

5. Definition Section (#define)

The definition section is used to define symbolic constants using the #define directive. These constants do not change during program execution.

#define PI 3.14
#define MAX 100

Using symbolic constants improves program readability and makes modifications easier.

6. Global Declaration Section

The global declaration section contains global variables and function declarations that are accessible to all functions in the program.

int count;
float total;

Global variables are declared outside all functions and retain their values throughout program execution.

7. main() Function

The main() function is the entry point of a C program. Program execution always starts from the main() function.

int main()
{
    // statements
    return 0;
}

It contains executable statements and may call other user-defined functions. The return 0; statement indicates successful program termination.

8. User-Defined Functions

User-defined functions are functions written by the programmer to perform specific tasks. They help in modular programming and reduce code repetition.

void display()
{
    printf("Welcome to C Programming");
}

These functions are usually defined outside the main() function and called when needed.

9. Sample C Program with Explanation

// Program to print Hello World

#include <stdio.h>        // Link section

#define MSG "Hello World" // Definition section

int main()               // main function
{
    printf("%s", MSG);   // statement
    return 0;
}

Explanation:

  • Comments describe the program.
  • #include <stdio.h> allows use of printf().
  • #define MSG defines a symbolic constant.
  • main() starts execution.
  • printf() prints output to the screen.

10. Key Points

  • Every C program follows a well-defined structure.
  • main() is mandatory in every C program.
  • Header files provide built-in functions.
  • Functions improve program modularity.
  • Understanding structure helps write error-free programs.
Note: A clear understanding of the structure of a C program is essential before learning data types, operators, loops, and functions.