elements of a c program

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.

Example of Basic Elements of a C Program

#  Preprocessor Directives

// Comments

// Functions
Function1()
{ 
    Statements;
}
Function2()
{
   Statements;
}

// Main Function
int main() 
{
    Statements;
}

As shown above, there are four basic elements of a C Program:

  • Preprocessor Directives
  • Comments
  • Functions
  • Statements

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 the Program. Any program before compilation goes through a special program called preprocessor. This special program is usually built into the compiler and works on its own after the programmer gives the Compile command. Preprocessor directives perform various functions in C, but for now, we will use them to include Header files.[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 to be shared between several other source files.[2] In laymen’s term think of header files as including code not written by us into our Program. We will learn about them in details later.

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 (;).

#include<stdio.h>

The example above causes the preprocessor to include a copy of stdio.h header file into our program. This header file is supplied by the C compiler and contains function definitions for Input & Output operations (more on this later). To include more than one header file, we must place the others 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:

#include<stdio.h>
#include<math.h>

Comments

Comments are valuable notes, explanation or annotation in the source code, written by the programmer to make the source code easier for humans to understand. They may explain 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 // 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 5):

// 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 /* and ends with */. Everything in between /* and */ will be ignored by the C compiler.

/*
Title: Basic Elements of a C Program
Author: the Bored Engineer
Course: C Programming for Beginners
*/

#include<stdio.h>
#include<math.h>

Functions

A function is a self-contained group of statements or block of code, that together perform a specific task. It is one of the basic elements of 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[3]. We will talk about functions at length, later in the course. 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 a sub-routine 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 execution of the program, the main() function gets called automatically. This means that the program execution begins here and 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.

/*
  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

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 (;).

References

  1. Preprocessor Directives (GeekForGeeks)
  2. Header Files (GCC-GNU)
  3. Functions in C (Tutorialspoint)

Leave a Reply