C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

 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:
F:\CProgramming\structureC.JPG

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:

  • Documentation Section: Provides information about the program, such as author, date, and a brief description of what the program does.
  • Link Section: Includes the necessary header file stdio.h for input and output operations.
  • Global Declaration Section: In this simple program, no global variables are needed. It contains the declaration of the function addNumbers.
  • Main Function Section: The main function is the entry point of the program. It declares local variables num1, num2, and sum. It takes user input for two numbers, calls the addNumbers function, and displays the result.
  • Subprogram Section: The addNumbers function is defined here. It takes two parameters (a and b), adds them, and returns the result. This function encapsulates the logic for adding two numbers.