Basic Structure of a C Program
After being equipped with knowledge of all the character sets, keywords and identifiers in the C programming language, we are now ready to write computer programs using C. Let us first understand the basic structure to organize the C instruction to form a full working computer program. Following diagrams shows the schema of a basic C programs:
Based on the above structure, Let us write simple C program to add two numbers using function, and understand each section in detail:
/***********************************************
Documentation Section
***********************************************/
/*
Author: Your Name
Date: January 25, 2024
Description: This program adds two numbers using a function.
*/
/***********************************************
Link Section
***********************************************/
#include <stdio.h>
/***********************************************
Global Declaration Section
***********************************************/
// No global variables are needed for this simple program.
// Function declaration
int addNumbers(int a, int b);
/***********************************************
Main Function Section
***********************************************/
int main() {
// Variable declarations for the main function.
int num1, num2, sum;
// Code for the main functionality.
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Call the addNumbers function and store the result in 'sum'
sum = addNumbers(num1, num2);
// Display the result
printf("Sum: %d\n", sum);
return 0; // Indicates successful program execution.
}
/***********************************************
Subprogram Section
***********************************************/
// Function definition for adding two numbers
int addNumbers(int a, int b) {
// Code for the addNumbers function.
int result = a + b;
return result;
}
Let us understand each section in detail: