Back to: C Programming for Beginners
In this lesson, we will talk about the basic elements of a C Program. We will also discuss the structure and meaning of each part in some detail.
Table of Contents
Example of Basic Elements of a C Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Documentation Section # Linking Section # Definition Section // Global Declaration Section Main Section() { // Statements // Statements } Sub Program Section() { // Statements } |
As shown above, there are seven basic elements of a C Program. It is not essential to include each of these elements of a C program in the code. Though, some elements are mandatory and required to be used; the use of others is entirely dependent on the coder.
- Documentation
- Linking
- Definitions
- Global Declarations
- Main Section
- Sub-Programs
- Statements
Documentation
The documentation section of any program consists of single and multi-line comments that make the code more readable. Comments are valuable notes, explanations or annotations in the source code, written by the programmer to make the source code easier for humans to understand and are ignored by the compiler at the time of compilation. They may contain an explanation of the purpose of a code line or help in understanding the program flow. They are one of the vital elements of a C program, even though they are ignored by the compiler at the time of code compilation and can be written anywhere in the source code. There are two ways in C to write comments:
- Single-Line Comment
- Multi-Line Comment
Single-Line Comment
As clear from the name itself, single-line comments are used to comment on a single line of information. They start with the symbol //
and go until the end of the line. In the example below, we can see that a line can contain just the comment (Line 1) or any other basic element of C followed by a comment (Line 3):
1 2 3 |
// including stdio.h header file #include<stdio.h> #include<math.h> // This Header file contains all mathematical related functions and constants |
Multi-Line comment
This is used in cases where we need to put comments along multiple lines. A multi-line comment starts with the symbol /*
and ends with */
. Everything in between /*
and */
will be ignored by the C compiler.
1 2 3 4 5 6 7 |
/* Title: Basic Elements of a C Program Author: the Bored Engineer Course: C Programming for Beginners */ #include<stdio.h> #include<math.h> |
Preprocessor Directives
In a C Program, lines starting with the pound symbol(#
) are known as preprocessor directives or just directives. They must be placed at the beginning of a program. Any program before compilation goes through a special program called a preprocessor. This special program is usually built into the compiler and works on its own. Preprocessor directives perform various functions in C, but for now, we will try to understand two of them i.e. header files and definitions.[1]
What is a Header File?
A header file is a file that contains information about the functions that we want to use in our program along with C declarations and macro definitions (more on this below) to be shared between several other source files.[2] In layman’s terms think of header files as including code in our program that is either written by someone else or kept in a separate file.
A header file should always end with .h
extension. Once we include a particular header file in our program, we can use all the functions defined inside it. Keep in mind that preprocessor directives do not end with the semicolon (;
).
The task of the preprocessor is to link all the header files included in a program to their original source code. This allows the compiler to understand and compile code included in the header files.
1 |
#include<stdio.h> |
The code snippet above is an example of including a header file in our code. This line of code will require the preprocessor program to include a copy of stdio.h
header file in our program during the process of compilation. This header file used in the example is supplied by the C compiler and it contains various function definitions for input & output operations (more on this later).
To include more than one header file, we must place them one after the other in separate lines. For example, C standard library contains a header file called math.h
, which contains mathematical functions and constants. To include this header file as well we will write as below:
1 2 |
#include<stdio.h> #include<math.h> |
Definition Section
In our program, we may have some user-defined symbolic constant values (either numerical or characters) that are used in multiple locations. It is then a good and convenient practice to include these constants at the start of the program under the definition section. Being a preprocessor directive; the definition section also starts with the #
symbol, followed by the word define
. It is then followed by the user-defined constant name and its value.
For example, let’s assume we wanted to use the value of PI(Ï€
) which is 3.1415 in multiple calculations in our program. Now, instead of writing the actual value every time we can create a user-defined symbolic constant for PI as shown below:
1 |
#define PI 3.1415 |
Now, we can use the symbol PI directly in our code whenever required without the need to write the actual numerical value repeatedly. The other advantage of using symbolic constants in a program is that we can easily change the actual constant value at the top and the same would be reflected at all other places wherever the constant was used in the code. For the above example, let us assume we want the value of PI(Ï€
) to be more accurate in our calculation. Instead of 3.1415, we want to use 3.14159. Now, we have the luxury of just changing this value in the definition section of the code and the same would be reflected in all other places wherever the constant was used. Without this definition section we would have to manually go, search and change the value ourselves.
1 |
#define PI 3.14159 |
Global Declaration Section
This section refers to the declaration of global variables used in a program as well as the declaration of all user-defined functions. We will learn more about both of these in later lessons. For now, consider this section as a place where we declare variables and functions. The meaning of declaration here is letting the compiler know beforehand about all the variables and functions that the user is going to be using in the program later.
Main Section and Sub-Programs
A sub-program also known as a function is a self-contained group of statements or blocks of code, that together perform a specific task. It is one of the basic elements of the C Program. Every C program will contain at least one function, which is the main() function. A function declaration tells the compiler about a function’s name, return type and parameters. A function definition provides the actual body of the function. We will talk about functions at length, later in the course.[3]
For now, consider functions as a series of statements grouped together and given a name for a particular task. How we group these statements is entirely up to us, but logically the division is such that each function performs a specific task. A function is also referred to as a method, procedure or subroutine in other programming languages.
Main Function
The main()
function is a special function in C that is mandatory for each program to have. This is because when the operating system begins the task of execution, the main()
function gets called first automatically. This means that the program execution begins at the main function and the code written inside this function will get executed first. It is, therefore, necessary to define this function in C and also think about what to write inside this sub-routine as that would define the program flow.
Statements
We can think of statements as commands to be executed by the computer when the program runs. All statements must end with a semicolon (;
) in C, though there are some exceptions to it. We will discuss what sort of statements we can write in C in future lessons.
How does the ‘Hello World’ Example work?
Now that we know about the basic elements of a C program, let us visit the ‘Hello World‘ example from the previous lesson and try to understand the Program flow.
1 2 3 4 5 6 7 8 9 10 11 |
/* Project: Hello World Author: the Bored Engineer */ #include <stdio.h> // include stdio.h library int main() //main function { printf("Hello World"); // print "Hello World" to console return 0; //return 0 to the Operating Sysytem } |
Expected Outcome
1 2 |
Hello World ...Program finished with exit code 0 |
The first few lines of the Program are multi-line comments followed by a preprocessor directive. C language does not have the facility for input and output. Therefore, all I/O operations are performed by a set of libraries provided by the compiler. Though they are not part of the C language in any formal way, they are still considered standard for I/O operations in C. To include these libraries we need to add the stdio.h
header file using the #include
preprocessor directive at the beginning of the program.
The line int main()
starts the main()
function. The int
here denotes that the function returns an integer value. This is done so that at the time of execution of the program, the OS can decide whether the program ran successfully or not. A return value of 0
means that the program ran successfully and a non-zero value means that there was a problem.
The curly brackets { }
in Line 9 and 12 denote the body of the main()
function. All statements corresponding to this function must be written inside these curly brackets. Currently, we only have two statements inside the function. The statement in Line 10 prints the "Hello World"
message in the Code-Blocks console using the inbuilt printf()
function. Again, we will discuss this function in details later. The statement return 0
in Line 11 does two things. Firstly, it causes the main()
function to terminate and secondly, it provides a return vaue of 0
to the OS as discussed earlier. We should also note that both the statements in this function end with a semicolon (;
).
Program for Addition & Subtraction of numbers
In the above ‘Hello World‘ example, all the elements of a C program that we discussed till now were not utilised. So let’s create another small program that will utilise all the elements of a C program that we discussed earlier. This time we will write a program that will add as well as subtract two numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/* Documentation Section Project: Add_Sub Author: the Bored Engineer */ // Linking Section #include <stdio.h> // include stdio.h library //Definition Section #define Num1 23 //Defining a numeric constant #define Num2 12 //Defining a numeric constant //Global Decleration Section int Sum = 0; //Declaring a global variable to store the addition result int Sub = 0; //Declaring a global variable to store the subtraction result void addition(void); //Decleration of the addition function void subtraction(void); //Decleration of the subtraction function //Main Section int main() //main function { addition(); //calling the addition function from Main subtraction(); //calling the subtraction function from Main return 0; //return 0 to the Operating Sysytem } //Sub-Program Section void addition(void) //Function Definition for addition { Sum = Num1 + Num2; //Statement to add two numbers printf("Addition:%d\n", Sum); //Printing the result in console } void subtraction(void) //Function Definition for subtraction { Sub = Num1 - Num2; //Statement to sub two numbers printf("Subtraction:%d\n", Sub); //Printing the result in console } |
Expected Outcome
1 2 3 4 |
Addition:35 Subtraction:11 ...Program finished with exit code 0 |
Please explore the code above as I have properly documented it. I will not explain the program flow here as we are going to discuss the same in later chapters. If you have any doubts about this lesson on elements of a C program or the code feel free to ask that in the comment section and I will definitely get back to you.
References
Best C Programming Books
List of C Programming books curated for beginners as well as experienced programmers.
Sharing is Caring
If you liked this post, then feel free to share it with your loved ones!