BASIC STRUCTURE OF C PROGRAM


 

BASIC STRUCTURE OF C PROGRAM


The basic structure of a C program is divided into 6 parts which makes it easy to read, modify, document, and understand in a particular format. C program must follow the below mentioned outline in order to successfully compile and execute. Debugging is easier in a well-structured C program.


Sections of the C Program

  1. Documentation
  2. Preprocessor Section
  3. Definition
  4. Global Declaration
  5. Main() Function
  6. Sub Programs

1. Documentation

This section consists of the description of the program, the name of the program, and the creation date and time of the program. It is specified at the start of the program in the form of comments. Documentation can be represented as:

// description, name of the program, programmer name, datee,

2. Preprocessor Section

All the header files of the program will be declared in the preprocessor section of the program. Header files help us to access other’s improved code into our code. A copy of these multiple files is inserted into our program before the process of compilation. 

Example:

include<stdio.h>,#include<math.h>,#include<conio.h>
3. Definition

Preprocessors are the programs that process our source code before the process of compilation. There are multiple steps which are involved in the writing and execution of the program. Preprocessor directives start with the ‘#’ symbol. The #define preprocessor is used to create a constant throughout the program. Whenever this name is encountered by the compiler, it is replaced by the actual piece of defined code.

Example:

#define long long ll

4. Global Declaration

The global declaration section contains global variables, function declaration, and static variables. Variables and functions which are declared in this scope can be used anywhere in the program. 

Example:

int num = 18;

6. Sub Programs

User-defined functions are called in this section of the program. The control of the program is shifted to the called function whenever they are called from the main or outside the main() function. These are specified as per the requirements of the programmer. 

Example:

int sum(int x, int y)
{
    return x+y; }

5. Main() Function

Every C program must have a main function. The main() function of the program is written in this section. Operations like declaration and execution are performed inside the curly braces of the main program. The return type of the main() function can be int as well as void too. void() main tells the compiler that the program will not return any value. The int main() tells the compiler that the program will return an integer value.

Example:

void main()
int main()

6. Sub Programs

User-defined functions are called in this section of the program. The control of the program is shifted to the called function whenever they are called from the main or outside the main() function. These are specified as per the requirements of the programmer. 

Example:

int sum(int x, int y)
{
    return x+y;
}
int sum(int x, int y)
{
    return x+y;}



PROGRAM OF BASIC STRUCTURE OF C -

/**                     //Documentation
 * file: age.c
 * author: you
 * description: program to find our age.
 */

#include <stdio.h>      //Preprocessor section 

#define BORN 2000       //Definition

int age(int current);   //Global Declaration

int main(void)          //Main() Function
{
  int current = 2021;
  printf("Age: %d", age(current));
  return 0;
}

int age(int current) {     //Subprograms
    return current - BORN;
}
OUTPUT: AGE 21
KEYWORDS: What is c programming,basic structure of c ,history of c language, 

Previous Post Next Post

Contact Form