SrcForge

Lesson 3

Structure of a Program

A C program is divided into different sections that help organize code properly. Every C program must contain a main() function because program execution starts from there.

C Track

Save progress as you learn.

44 lessons

Lesson content

Structure of a C Program Explained with Example

Every C program follows a specific structure that helps organize the code in a logical and readable manner. Understanding the structure of a C program is one of the first steps toward learning C programming. Although some sections are optional, every C program must contain a main() function, as program execution always begins there.

What Is the Structure of a C Program?

  • C Program Structure: The organized layout of sections and components used to write a C program.
  • main() Function: The starting point of execution in every C program.

A typical C program consists of one or more of the following sections:

  1. Documentation Section
  2. Link Section
  3. Definition Section
  4. Global Declaration Section
  5. main() Function Section
  6. Subprogram (User-Defined Function) Section

Sections of a C Program

Documentation Section

The Documentation Section contains comments that describe the program. These comments provide useful information such as the program name, author name, date of creation, purpose, and version details. Comments are ignored by the compiler and are meant only for programmers.

Example
/* Program Name : Hello World
   Author       : John
   Purpose      : Display a welcome message
*/
Benefits
  • Improves readability
  • Makes maintenance easier
  • Helps other programmers understand the code
  • Comment: Text written in a program for explanation purposes that is ignored by the compiler.

Link Section

The Link Section contains the header files required by the program. Header files provide declarations for built-in library functions. They are included using the #include preprocessor directive.

Example
#include <stdio.h>

Here, #include tells the preprocessor to include a header file, and stdio.h provides standard input and output functions such as printf() and scanf().

  • stdio.h - Standard Input and Output
  • stdlib.h - Memory allocation and utility functions
  • string.h - String handling functions
  • math.h - Mathematical functions
  • ctype.h - Character handling functions
  • Header File: A file containing predefined functions, macros, and declarations used in C programs.
  • stdio.h: The standard input and output header file in C.

Definition Section

The Definition Section is used to define symbolic constants using the #define preprocessor directive. Instead of writing the same value multiple times, programmers define a symbolic name that represents the value.

Example
#define PI 3.14159
#define MAX 100
Advantages
  • Makes programs easier to modify
  • Improves readability
  • Eliminates magic numbers
  • Symbolic Constant: A constant value represented by a name using the #define directive.

Global Declaration Section

The Global Declaration Section contains global variables and function declarations. Global variables are declared outside all functions and can be accessed by every function in the program.

Example
int count = 0;
float salary;
Advantages
  • Variables can be shared among functions
  • Reduces the need to pass variables repeatedly
  • Global Variable: A variable declared outside all functions and accessible throughout the program.

main() Function Section

The main() function is the most important part of every C program. Program execution always starts from the main() function, and every C program must contain exactly one main() function. The function begins with an opening brace { and ends with a closing brace }.

Declaration Part

This section declares the variables used inside the function.

Executable Part

This section contains the statements that perform the required operations.

  • Function: A reusable block of code designed to perform a particular task.

Subprogram (User-Defined Function) Section

This section contains user-defined functions. These functions perform specific tasks and are called from the main() function whenever needed. There may be zero, one, or many user-defined functions in a C program.

Example
void displayMessage()
{
    printf("Welcome to C Programming");
}
  • User-Defined Function: A function created by the programmer to perform a specific task.

Basic Structure of a C Program

#include <stdio.h>

int main()
{
    return 0;
}

Explanation of the Basic C Program Structure

#include

Preprocessor directive used to include header files in the program.

  • Preprocessor Directive: An instruction processed before the actual compilation of a C program.

stdio.h

The Standard Input/Output header file. It contains functions like printf() and scanf().

int

Return type of the main() function. It indicates that the function returns an integer value.

  • Datatype: A specification that defines the type of data a variable can store.

main()

The entry point of every C program. Program execution always begins here.

{ }

Curly braces define the beginning and end of a block of code. All statements inside a function are written within { }.

  • Block of Code: A group of statements enclosed within curly braces.

return 0;

Indicates successful completion of the program and returns control to the operating system.

Example
return 0;
  • Return Statement: A statement used to terminate a function and optionally return a value.

;

A semicolon is used as a statement terminator in C. Every executable statement in C must end with a semicolon.

  • Statement Terminator: A symbol used to indicate the end of a statement in a program.

FAQ About Structure of a C Program

1. Which section of a C program is mandatory?

The main() function is the only mandatory section in every C program. Program execution always starts from the main() function.

2. What is the purpose of the Documentation Section?

The Documentation Section contains comments that describe the program, including its name, author, purpose, and other useful information. These comments help programmers understand and maintain the code.

3. Why do we use #include <stdio.h> in C?

The stdio.h header file provides standard input and output functions such as printf() and scanf(). Without including this header file, these functions cannot be used correctly.

4. What is the purpose of return 0; in the main() function?

The statement return 0; indicates that the program has finished executing successfully and returns control to the operating system.

5. What are global variables in C?

Global variables are variables declared outside all functions. They can be accessed from any function within the program, making them useful for sharing data between functions.

6. Is the Definition Section mandatory in every C program?

No. The Definition Section is optional and is only used when symbolic constants or macros need to be defined using the #define directive.

Prev: FeaturesBack to hub