CSC115 ยท 2074 ยท 10 marks
Discuss structure of a C program with suitable example.
Discuss structure of a C program with suitable example.[10]
Quick answer
Every C program follows a standard, well-defined structure. Understanding this structure is essential because the compiler processes each section in a specific order. A well-structured C program is easier to read, debug, and maintain.
Structure of a C Program
Introduction
Sections of a C Program
A C program is typically divided into the following sections:
1. Documentation Section
- Contains comments that describe the purpose of the program, author name, date, etc.
- Comments are ignored by the compiler.
- Single-line comment:
// comment - Multi-line comment:
/* comment */
/* Program to add two numbers
Author: Ram
Date: 2081 */
2. Preprocessor Directives
- Lines that begin with
#are processed before compilation. #includeis used to include header files (libraries).#defineis used to define constants or macros.- These directives make library functions like
printf()andscanf()available to the program.
#include <stdio.h>
#define PI 3.14
3. Global Declarations
- Variables or functions declared outside all functions are called global.
- They are accessible throughout the entire program.
int count = 0; /* global variable */
4. main() Function
- The
main()function is the entry point of every C program. - Execution always begins from
main(). - It returns an integer value to the operating system.
return 0signals that the program ended successfully.- Curly braces
{ }define the body of the function. - Every statement inside must end with a semicolon (
;).
int main()
{
/* statements */
return 0;
}
5. User-Defined Functions
- Functions written by the programmer to perform specific tasks.
- They are defined outside
main()but can be called frommain()or other functions. - They help in making the program modular and reusable.
int add(int x, int y)
{
return x + y;
}
Complete Example
The following example demonstrates all sections of a C program:
/* Program to add two numbers and display the result */ /* 1. Documentation */
#include <stdio.h> /* 2. Preprocessor directive */
int result; /* 3. Global declaration */
int add(int x, int y); /* Function prototype */
int main() /* 4. main() function */
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
result = add(a, b);
printf("Sum = %d\n", result);
return 0; /* Signals successful end */
}
int add(int x, int y) /* 5. User-defined function */
{
return x + y;
}
Sample Output
Enter two numbers: 5 3
Sum = 8
Explanation of Key Elements
| Element | Purpose |
|---|---|
#include <stdio.h> | Includes standard I/O library for printf() and scanf() |
int main() | Starting point of the program; returns integer to OS |
{ } | Curly braces define the body of a function |
return 0 | Signals the program ended successfully |
; | Every statement must end with a semicolon |
scanf() | Reads input from the user |
printf() | Displays output to the screen |
How a C Program Runs
Once the source code is written following the above structure, it goes through the following stages before producing output:
source.c --> Compiler --> source.obj --> Linker --> source.exe --> Output
- Compilation: The compiler translates source code into object code and catches syntax errors.
- Linking: The linker combines object code with library functions (like
printf,scanf) to produce an executable file. - Loading: The loader places the executable into memory.
- Execution: The CPU runs the program and produces output.
Summary
A well-structured C program contains five main sections: documentation, preprocessor directives, global declarations, the main() function, and user-defined functions. Following this structure makes programs organized, readable, and easy to maintain. The main() function is mandatory in every C program as it serves as the entry point for execution.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.