Lesson 3
Structure of a C 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.
Lesson content
Structure of a C Program Explained with Example
A C program is divided into different sections that help organize code properly and make programs easier to understand and maintain. Every C program must contain a main() function because program execution starts from 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 C program contains one or more sections, each having a specific purpose.
Sections of a C Program
Documentation Section
The documentation section contains comments that describe the program.
This section may include:
- Program name
- Author name
- Purpose of the program
- Date or version information
These comments help programmers understand the code later.
- Comment: Text written in a program for explanation purposes that is ignored by the compiler.
Link Section
The link section contains header files required for the program.
Header files include predefined library functions needed during program execution.
Example
#include<stdio.h>- 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 declare symbolic constants using the #define directive.
Example
#define PI 3.14- Symbolic Constant: A constant value represented by a name using the #define directive.
Global Declaration Section
Global variables are declared in this section.
These variables can be accessed from any function in the program, including outside the main() function.
Example
int count;- Global Variable: A variable declared outside all functions and accessible throughout the program.
main() Function Section
Execution of a C program begins with the main() function.
Every C program must contain only one main() function.
This section starts with { and ends with }.
Structure of main() Function
The main() function has two parts:
Declaration Part
Variables used in the program are declared here.
Executable Part
This section contains the actual program statements and logic.
Subprogram Section
The subprogram section contains user-defined functions that can be called inside the main() function.
A C program may contain zero, one, or multiple user-defined functions.
- 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 Basic C Program Structure
#
Used to represent a preprocessor directive.
include
The include directive tells the compiler to include a header file in the program.
stdio.h
The Standard Input/Output header file. It contains functions like:
- printf()
- scanf()
- Preprocessor Directive: An instruction processed before the actual compilation of a C program.
int
int is a datatype used to define integer values.
- Datatype: A specification that defines the type of data a variable can store.
main()
main() is the function where execution of the program starts. It acts as the entry point of every C program.
- Function: A reusable block of code designed to perform a particular task.
{ }
Curly braces specify a block of code. All statements inside a function are written within { }.
- Block of Code: A group of statements enclosed within curly braces.
return
The return statement indicates that the program execution has reached the end of the function.
Example
return 0;Here, 0 usually indicates successful program execution.
- 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.
Why Understanding the Structure of a C Program Is Important
Better Code Organization
A structured program is easier to read and maintain.
Easier Debugging
Organized sections help programmers find and fix errors quickly.
Improved Reusability
Functions and modular sections make code reusable.
Beginner-Friendly Learning
Understanding program structure is the first step in learning C programming.
FAQ About Structure of a C Program
1. Why is the main() function important in C?
The main() function is important because program execution always starts from it.
2. What is the purpose of stdio.h in C?
stdio.h provides standard input and output functions such as printf() and scanf().
3. What is the use of global variables in C?
Global variables allow data to be shared across multiple functions in a program.