Structure of C Program

Structure of C Program with Examples

In this article, I am going to discuss the Structure of the C Program with Examples. Please read our previous article, where we discussed 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

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

Structure of C Program

Let us understand each section of c program in detail.

Documentation Section:

The documentation section consists of a set of comment lines giving the name of the program, the author, and other details, which 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 of 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.
Preprocessor directives:

In a C program, the statements which are starting with the “#” symbol are called preprocessor directives. The C program provides a lot of inbuilt preprocessor directives that are handled by the pre-processor before the compiler starts execution.

What are preprocessor directives?

The name preprocessor 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. There are multiple programs involved in this process. such as preprocessor, Assembler, Compiler, and Linker.

Before the compilation starts, initial processing will be done by the pre-processor such as Macro substitution, Comment removal, Conditional compilation and Header file inclusion, etc. If this is not clear at the moment, then don’t worry as we progress in the course, we will discuss these things in detail.

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.

Global Declaration Section:

There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section that is outside of all the functions. This section also declares all the user-defined functions. The Statements present in 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: 

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 closing brace of the main function is the logical end of the program. All statements in the declaration and executable part end with a semicolon.

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. All sections, except the main () function section, may be absent when they are not required.

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 are also going to discuss all the above concepts in detail with examples in our upcoming articles.

Sample C Program:

Sample C Program

  1. In C many library functions are grouped category-wise and stored in different files known as header files. For example, stdio.h–>standard input-output header file
  2. To use the functions defined in the header file that needs to be included in the program
  3. This can be achieved by the preprocessing directive #include
  4. #include includes the content of the header file (stdio.h) at the beginning of the program.
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 case and lower case letters.
  • Every statement ends with a semicolon “;”, which is a statement terminator.
  • Multiline comments are placed between “/*C Program*/” while “//” can be used at the beginning for single-line comments.

In the next article, I am going to discuss how to install software for the C program to compile and execute in detail. Here, 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 *