BIT102 ยท 2078 ยท 5 marks
What is the basic structure of a C program? Explain each part.
What is the basic structure of a C program? Explain each part. [5]
C ProgrammingChapter 1: Fundamentals of C ProgrammingBasic structure of C program
Quick answer
A C program is organized into several distinct sections. Below is the general template: Contains comments about the program such as program name, author, date, and purpose; Written using /; Ignored by the compiler; meant for human readability.
Basic Structure of a C Program
Basic Structure of a C Program
A C program is organized into several distinct sections. Below is the general template:
/* Documentation Section */
// Program description, author, date
/* Link Section (Header Files) */
#include <stdio.h>
#include <conio.h>
/* Definition Section (Macros/Constants) */
#define PI 3.14159
/* Global Declaration Section */
int globalVar;
/* Main Function */
int main()
{
/* Local Declaration */
int a, b;
/* Executable Statements */
printf("Hello, World!\n");
return 0;
}
/* User-Defined Functions */
void myFunction()
{
// function body
}
Explanation of Each Part
1. Documentation Section
- Contains comments about the program such as program name, author, date, and purpose.
- Written using
/* ... */(multi-line) or//(single-line). - Ignored by the compiler; meant for human readability.
2. Link Section (Header Files)
- Contains preprocessor directives that include standard library header files.
- Syntax:
#include <filename.h> - Example:
#include <stdio.h>includes standard input/output functions likeprintf()andscanf(). - These are processed before compilation.
3. Definition Section
- Used to define symbolic constants or macros using
#define. - Example:
#define PI 3.14159 - The preprocessor replaces every occurrence of
PIwith3.14159before compilation.
4. Global Declaration Section
- Variables declared here are global variables, accessible throughout the entire program.
- Declared outside all functions.
- They retain their value for the entire lifetime of the program.
5. Main Function main()
- Every C program must have exactly one
main()function -- it is the entry point of execution. - Contains two parts:
- Local Declaration: Variables declared inside
main(), accessible only within it. - Executable Statements: The actual logic -- assignments, function calls, loops, etc.
- Local Declaration: Variables declared inside
- Returns an integer value to the operating system (
return 0indicates successful execution).
6. User-Defined Functions
- Functions written by the programmer to perform specific tasks.
- Defined after (or before)
main(). - Promotes modularity and code reusability.
Summary Table
| Section | Purpose |
|---|---|
| Documentation | Comments and program description |
| Link Section | Includes header/library files |
| Definition Section | Defines constants and macros |
| Global Declaration | Declares global variables |
main() Function | Entry point; contains program logic |
| User-Defined Functions | Custom functions for specific tasks |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.