×

C PROGRAM STRUCTURE


In this Tutorial, we will find out about the fundamental program structure of an ordinary c program.

Here what a regular C Program resembles. It contains header file inclusion, macro declaration, main() function, variable statement and so forth.

Structure of a typical C Program

Almost every program in C program has the following Sections -

  • Documentation section (Comments)

  • Linking section statements (Preprocessor statements)

  • Definition section (Global declarations)

  • The main() function

    Local variable declarations

    Program statements & Expressions

  • User defined functions


Let's write a simple program to illustrate these sections -

File - program_structure.c
/*
*Author: www.earthcoding.com
*Description: This program will print Sum of two integer number on the screen
*This is the Documentation Section
*/

#include <stdio.h> //Linking of header files i.e. Preprocessor Statement

// User define function
int sum(int x, int y) {
    return x + y;
}

int main() // main function
{
    int a, b, result; // Declaration Section

    a = 10; //Statement
    b = 90; //Statement

    result = sum(a, b); //Statement

    printf("Sum of 10 + 90 = %d", result); //Statement

    return 0;
}
Output
Sum of 10 + 90 = 100

From the above example program, it is easy to understand how a typical C program is structured.

  • Documentation Section -

    It also called a Comment Section. Everything written in this Section will be ignored by the compiler. Comments are not compiled and executed. Let's take an Example -

    // This is a single line comment and will not be executed
    /* This is a multi line comment
     * and will not be executed
     */
  • Preprocessor statements -

    These are some predefined header documents called header files, that contains various useful functions like printf(), scanf() and so forth. These header files are connected by a Linking procedure which is finished by the linker. This is identified in the program by a # sign and they are called Preprocessing Directives. See the examples below -

     #include<stdio.h>
     #define PI 3.14
  • Global Declaration -

    Functions and variables declared in this section can be used anywhere in the program. They are known as global functions and variables.

     // Global variables declaration
     int A;
     int B;
     void main(){
        //statement
     }
  • The main() function -

    This is the entry point of the program. This is the function that compiler first looks with the end goal to execute the program.

  • Local variable declarations -

    The variables and functions that are declared inside a function are known as local variable or local functions. The scope of those declaration are limited withing those function in which they are declared.
    For example -

    Example
    int perform_addition(int x, int y)
    {
    	int addition;
    	addition = x + y;
    	return addition;
    }
    
    void main()
    {
    	printf(perform_addition(3,4)); // This will work
    	printf("%d",addition); // Error: This is not valid
    }   
  • Program statements -

    Every instruction which ends with a semicolon (;) is a program statement. For example -

    Example
    printf("This is a statement");
    
    x=3+5; //this is another statement
  • Expressions -

    The statement that plays out some sort of estimation or activities like expansion or division is an expression. For example -

    Example
    z = x + y; //expression 1
    
    c = a / b; //expression 2
    
    d = z – c; // expression 3
                                    
  • User defined function -

    Functions defined by the programmer which are not available in C library are known as a user-defined function. For example -

    Example
    printf("This is NOT user defined function");
    
    int calculate_area_of_ractangle(int length, int width) // This is user defined function
    {
        return length * width;
    }

In the following Tutorial, We will examine some essential ideas of a programming language

Sharing is caring: