Basic Structure of C program with an example | Sections In C language

Introduction of C language:

Introduction of C languageC is a general-purpose high level language that was originally developed by Dennis Ritchie for the Unix operating system. It was first implemented on the Digital Equipment Corporation PDP-11 computer in 1972. The Unix operating system and virtually all Unix applications are written in the C language.

Basic Structure of C Language

Basic Structure of C program with an example

Documentation Section    → Suggested Section
Include Header File Section   → Optional Section
Defining Symbolic Constant Section    → Optional Section
Global Declaration Section    → Optional Section
Main() program Section
{
    Local Variable Declaration
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _              → Compulsory
    Executable Statement
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
}
Function() Sub program section
{
  Local Variable Declaration
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _              →Optional Section
    Executable Statement
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
}
C Program can be viewed as a group of building block called as function. A function is sub-routine or sub-program that may include one or more statement design to perform a specific task. C program contain one or more section as shown in the above structure.

Documentation Section

Documentation Section: It consist of a set of comments lines that provides the name of the program and other details about the program. This is the suggested section of every C program and also called as remark statement. Documentation section are not executive by compiler 'will not generate machine code' and called as Non-executable statement.
Documentation can be given by two ways i.e " // " is use for single line and "\*--------------*/" for multiple lines.


Include Header File Section

Include Header File Section: The section provide instruction through compiler to link function from the system library, the function can be link by using pre-pro cast directive.
#include<stdio.h>It includes all standard input and output sub-routine.
#include<math.h>
math.h header file defines various mathematical functions and one macro.
#include<conio.h>
This header declares several useful library functions for performing "console input and output" from a program.
 Definition Section

Definition Section: In definition section also defined a symbolic name and constant that are used inside the program. The definition section or symbolic constant can be created as below:
Syntax:-
#define symbolic_name constant
Where #define is a pre-processes directive. Symbolic name is a constant name to weight in capital letters and constant is any constant value assign of symbolic name but don't use assignment operator to assign a constant.
e.g  #define PI 3.14
      #define MAX 200

 Global Declaration Section

 Global Declaration Section: In this section there are same variable that are use more than one function and called as global variable. Which can be declare outside the main program i.e declared in global declaration section. The variables are declared with there datatypes and to give the initial values using assignment operator.
The above three sections are optional i.e linking section, definition section and global declaration section and appear in the given order whenever require.


                                        Main Program

Main Program: In C programming to write every C program is always have at-least one function i.e main( ) or main program in this function contains two parts i.e
  1. Local variable declaration: In this part before using variable in C program it is declare all the local variables use in executable part of the same function.
  2. Executable Statement: It consist of at-least one statement, this part is used to handle input or output processing operation. This statement are directly converted into machine language from an executable statement. This two parts must appear between opening and closing braces. The closing of the main function is the logical end of the program. All statement in declaration and executable part must be terminated by ; (semi-colon).
Sub Program

Sub Program: This section all user defined function that can be called by main( ) function. The sub program is the same structure as that of main( ) program. But this is optional and appears after the main program whenever required.

Some Examples

  • Program to print addition of two inputted integer numbers
#include<stdio.h>
main( )
{
    int A, B, C ;
    printf("Enter any two integer numbers:");
    scanf("%d %d",&A, &B);
    C=A+B ;
    printf("Addition = %d",C) ;
    getch( ) ;
}
Output: Enter any two integer numbers: 10 20
Addition = 30

  • Program to interchange two numbers
#include<stdio.h>
main( )
{
    int a, b ;
    printf("Enter any two integer numbers:");
    scanf("%d %d",&a, &b) ;
    a= a+b ;
    b= a-b ;
    a=a-b ;
    printf("The interchanged values are : a= %d and b=%d",a, b) ;
}
Output:
Enter any two integer numbers: 5  2
The interchanged values are: a=2 and b=5

Post a Comment

Previous Post Next Post