There are few commands and syntax given below which is used in C programming to write a simple C program. Let’s see all the sections of a simple C program line by line.

.h = header file

void main() = doesn’t return any value

scanf(“%d“,&variable); = This command take input value

C Basic commands

Explanation

#include <stdio.h>

This is a preprocessor command that includes standard input output header file(stdio.h) from the C library before compiling a C program

int main()

This is the main function from where execution of any C program begins.

{

This indicates the beginning of the main function.

/*_some_comments_*/

whatever is given inside the command “/*   */” in any C program, won’t be considered for compilation and execution.

printf(“Hello_World! “);

printf command prints the output onto the screen.

getch();

This command waits for any character input from keyboard.

return 0;

This command terminates C program (main function) and returns 0.

}

This indicates the end of the main function.

Structure of C program is defined by set of rules called protocol, to be followed by programmer while writing a C program. All C programs are having sections/parts which are mentioned  below.

  1. Documentation section

  2. Link Section

  3. Definition Section

  4. Global declaration section

  5. Function prototype declaration section

  6. Main function

  7. User defined function definition section

EXAMPLE :

You can compare all the sections of a C program with the below C program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*   Documentation section
    Basics structure of C program
    Author: Biddut */
#include <stdio.h>   /* Link section */
int total = 0;       /* Global declaration, definition section */
int sum (int, int);  /* Function prototype declaration section */
int main ()          /* Main function */
{
    printf (“Hello World \n”);
    total = sum (5, 5);
    printf (“Sum of two numbers : %d \n”, total);
    return 0;
}
int sum (int a, int b) /* User defined function */
{  
    return a + b;      /* definition section */
}

OUTPUT:

DESCRIPTION:

  • Please note that a C program mayn’t have all below mentioned sections except main function and link sections.

  • Also, a C program structure mayn’t be in below mentioned order.

Sections

Description

Documentation section

We can give comments about the program, creation or modified date, author name etc in this section. The characters or words or anything which are given between “/*” and “*/”, won’t be considered by C compiler for compilation process.These will be ignored by C compiler during compilation.

Example : /* Comment */

Link Section

Header files that are required to execute a C program are included in this section

Definition Section

In this section, variables are defined and values are set to these variables.

Global declaration section

Global variables are defined in this section. When a variable is to be used throughout the program, can be defined in this section.

Function prototype declaration section

Function prototype gives many information about a function like return type, parameter names used inside the function.

Main function

Every C program is started from main function and this function contains two major sections called declaration section and executable section.

User defined function section

User can define their own functions in this section which perform particular task as per the user requirement.