Structure of C Program

Basic Structure of C Program

In this article, I will discuss the Basic Structure of a C Program with Examples. Please read our previous article discussing the Library and IDE in C Programming Language. At the end of this article, you will understand the different parts of a C program and their need and use in detail.

Structure of C Program

To understand the Structure of a C Program, please look at the following image, which shows the different sections of a c program.

Structure of C Program

Let us understand each section of the c program in detail.

Documentation Section in C Program:

This is not a formal part of the program’s structure but good practice. It contains comments that describe the purpose and functionality of the program. Comments can be single-line (// comment) or multi-line (/* comment */).

The documentation section consists of a set of comment lines giving the name of the program, the author, and other details that the programmer would like to use later.

We can also call this section the commenting section. Here, as a developer or programmer, you need to write a few comments about the program, which will help the other programmers on the team. This will help other programmers to understand your program code quickly. The Documentation section contains the following information.

  1. @Project Name
  2. @Author Name
  3. @Created Date
  4. @Program Description, etc.
Header Files or Preprocessor Directives in C Program:

These lines are included at the beginning of a C program that instructs the compiler to include standard or user-defined header files before compilation starts. They are identified by the #include directive.

The statements starting with the “#” symbol are preprocessor directives in a C program. The C program provides many inbuilt preprocessor directives handled by the pre-processor before the compiler starts execution. This includes library files needed by the program. These files contain the definitions and prototypes of functions and constants used in the program.
Format: #include<filename.h> or #include “filename.h”
Examples: #include <stdio.h> for standard input-output functions, #include <stdlib.h> for standard utility functions.

What are Preprocessor Directives?

The name preprocessor is saying itself everything. When we write and compile a C program, the application-building process will start, where the C Compiler will come into action to convert the high-level code (human understandable) into binary code (Machine understandable).

The application-building process won’t be carried out by a single program called the compiler. Multiple programs are involved in this process, such as Preprocessor, Assembler, Compiler, and Linker.

Before the compilation starts, the pre-processor will do initial processing, such as Macro substitution, Comment removal, Conditional compilation, Header file inclusion, etc. If this is unclear now, don’t worry; we will discuss these things in detail as we progress in the course.

Link Section:

The link section provides instructions to the compiler to link functions from the system library.

Definition section: 

The definition section defines all symbolic constants. Here, we need to define all the constants using the #define preprocessor directive. Also, any global variable declarations can be made here. Format: #define PI 3.14

Global Declaration Section:

Some variables are used in more than one function. Such variables are called global variables and are declared outside of all the functions, i.e., in the global declaration section. Global variables are those that are declared outside of all functions and are available throughout the program. Format: Data type followed by variable names; function prototypes.

This section also declares all the user-defined functions. The Statements present in the Global Declaration Section will be accessible by all the functions in the program. 

Function:

A function is a self-contained block of code that performs a specific task. It is not possible to write a C program without a function. Every function in a C program must start with Open curly brace { and ends with Closed curly brace }.

Main() Function Section: 

The main() function is where the execution of the program begins. Every C program must have one main function section. This section contains two parts: the declaration part and the executable part

  1. Declaration part: The declaration part declares all the variables used in the executable part.
  2. Executable part: There is at least one statement in the executable part.

These two parts must appear between the opening and closing braces. The program execution begins at the opening brace and ends at the closing brace. The main function’s closing brace is the program’s logical end. All statements in the declaration and executable part end with a semicolon. Components of the main function:

  • Local Variable Declarations: Variables declared inside the main() function or any function.
  • Statements and Expressions: The actual code performing tasks, enclosed in {}.
  • Return Statement: Usually return 0; or return 1;, indicating the termination status of the program to the operating system.
SubProgram Section: 

The subprogram section contains all the user-defined functions that are called in the main() function. User-defined functions are generally placed immediately after the main() function, although they may appear in any order. Except for the main() function section, all sections may be absent when not required. A function definition includes the function name, return type, parameters (if any), and the body of the function enclosed in braces {}

Statement: A statement is a line of code in a c program, and every statement should end with a semicolon.

Return statement: The return statement is basically used in the c program to return a value from a function. In C programming, we use functions to achieve modularity.

Note: If you don’t understand any or all of the above concepts, then don’t worry. We will also discuss all the above concepts in detail with examples in our upcoming articles.

Sample C Program:
// Documentation Section
// Program to demonstrate the structure of a C program
// Author: John Doe
// Date: Jan 1, 2023

#include <stdio.h>  // Header File Section

#define MAX 100     // Definition Section

int globalVar;      // Global Declarations Section

int add(int a, int b); // Function Prototype

int main() {       // Main Function
    globalVar = 10;
    int result = add(5, globalVar);
    printf("Result is: %d\n", result);
    return 0;
}

int add(int a, int b) { // Function Definition
    return a + b;
}
Key Points
  • Formatting and Style: While the C language doesn’t enforce any specific style, following a consistent and readable format is crucial for code maintainability and collaboration.
  • Comments: Use comments judiciously to explain what your code does, especially for complex logic.
  • Modularity: Breaking down the program into functions (subprograms) helps manage complexity, particularly for larger programs.
Any C Program basically Consists of the Following Features:
  • It consists of a collection of one or more functions.
  • Every function name is followed by a pair of parentheses ( ).
  • It consists of at least one mandatory or compulsory function named “main”.
  • Contents or Statements within a function are written within a pair of braces { }.
  • Execution always starts at the beginning braces “{“ of main and usually proceeds sequentially till the end braces “}” of main is reached.
  • Every function returns a value; otherwise, we use void, i.e., nothing.
  • Usually, every program contains “#” at the top, which is a preprocessor directive that includes files or definitions.
  • It is Case-Sensitive, i.e., differentiates between upper and lower case letters.
  • Every statement ends with a semicolon. 
  • Multiline comments are placed between “/*C Program*/” while “//” can be used at the beginning for single-line comments.

In the next article, I will discuss how to install software for the C program to compile and execute in detail. In this article, I try to explain the basic structure of a c program with Examples, and I hope you enjoy this article.

1 thought on “Structure of C Program”

Leave a Reply

Your email address will not be published. Required fields are marked *