GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

Structure of a C Program

The structure of a C program consists of the following:

  1. Documentation Section.
  2. Pre-processor directives.
  3. Definition section.
  4. Global declaration.
  5. main function.
  6. sub-program section.

We shall discuss these in detail.

You may watch the video posted at the end of this page.

structure of a c program

Documentation Section

  • In this section, we may write.
    • Name of program.
    • Author Name.
    • Date on which program was written.
    • Tester name.
    • Version of the code.
    • Inputs and Outputs.

All these are written in the form of comments.

/*
Name of Program:
Author Name:
Date:
Input:
Output:
Tester Name:
Version Name:
etc.,
*/

pre-processor directives

In this section we write all the header files. In header files there will be multiple functions defined (may be built-in or user-defined), which helps in reusability of the code.

We include header files as: 

#include<stdio.h>
#include<stdlib.h>

Definition section

In this section we write MACROS and mainfest constants. These will get replaced through-out the program by the pre-processor. The pre-processor begins with ‘#’, and whenever we write something that begins with ‘#’ we don’t put semicolon at the end of the statement.

Let us see some examples,

#define PI 3.142
  • Wherever we write PI, the pre-processor will replace it with 3.142 during the 1st pass of the compiler.
  • Normally mainfest constants are written in upper-case. (A standard, not mandatory)

Global declaration section

In this section we write,

  1. Global variables
  2. Function declaration
  3. Static variables

Variables or functions declared in this section can be used anywhere in the program.

main function

  • main function is the starting point of any program, unless or otherwise specified or changed.
  • main is a user-defined function.
  • The return type of the main program is int. 
int main()
{
local declaration
......
return 0;
}

sub-program section

  • These are the user-defined functions that we write here.
  • There can be n number of sub-programs.
  • Each sub-program performs the particular task. (unique task)
  • Sub-programs are written for code reusability purpose.
Scroll to Top