Structure of C Program
×


Structure of C Program

29

 Understanding the structure of a C program is vital for writing clear and maintainable code.

 A well-structured program makes it easier to read, modify, and debug.

 Below is a breakdown of the six essential sections that every C program should have:

Sections of a C Program

1 Documentation

Description: Provides an overview of the program, including its purpose, author, and creation date.

Format: Commented lines at the beginning of the program.


2 Preprocessor Section

Description: Includes #include directives for header files that provide additional functionalities.

Example:

 #include<stdio.h>

3 Definition

Description: Uses #define to create constants that can be used throughout the program.

Example:

 #define PI 3.14159

4 Global Declaration

Description: Declares global variables and function prototypes that are accessible throughout the program.

Example:

 int globalVar = 10;

5 Main() Function

Description: The entry point of the program where execution begins.

Format:

int main() {

                 /* code */ return 0;

                }


6 Sub Programs

Description: User-defined functions that perform specific tasks and can be called from main() or other functions.

Example:

int add(int a, int b) {
    return a + b;
}

Example Program:

/**                      
 * File: sum.c 
 * Description: Program to calculate sum.
 */
// Program to calculate sum
#include<stdio.h>       
#define X 20  

int calculateSum(int y);    
  
int main(void) {
    int y = 55; 
    printf("Sum: %d\n", calculateSum(y)); 
    return 0; 
} 

int calculateSum(int y) {
    return y + X; 
}

Output:

Sum: 75
Section
Description
Documentation
Comments providing information about the program.
Preprocessor Section
#include directive for the standard I/O library.
Definition
Defines a constant `X` with a value of 20.  
Global Declaration
Declares a function `calculateSum` and a variable `y.
Main() Function
The entry point of the program where the calculation happens.
Sub Programs
Defines the `calculateSum` function for sum calculation.

Compilation and Execution Steps:

Program Creation: Writing the C code.

Compilation: Converting the source code into machine code.

Execution: Running the compiled code.

Output: Displaying the results or output of the program.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments