Basic Elements of a C Program

A simple C program has the following form.

preprocessor directives

int main()

This is the structure of typical C program. Let's discuss the meaning of each part in somewhat detail.

Preprocessor directives #

Before a program is compiled it goes through a program called preprocessor. Lines start with
a pound(#) symbol are called preprocessor directives or just directives. Preprocessor directives must be placed at the beginning of a file. These directives perform different types of functions, but for now, we will use them to include a header file. So what is header file ? A header file contains function definitions and macro definition. It always sends with .h extension. For example stdio.h header file contains function used for input and output. After including a header file you can use any function defined inside a header file. The preprocessor directives do not end with the semicolon (;). To include stdio.h header file in your program do this:


If your program needs more than one header files then place each of them on its own line. For example, C standard library contains a header file called math.h, which contains mathematical functions and constant. To include stdio.h and math.h in your program do this:


Functions #

A function is a self-contained block of code, other languages call them procedure or subroutine. A function is just a series of statements grouped together and given a name. A function does something very specific for e.g calculate factorial of a number, find the sum of two numbers and so on. A C program may consists of many functions but main() is mandatory. The main() function is special because when OS begin executing the program, main() gets called automatically. So it is necessary for you to define this function.

Statements #

You can think of the statement as a command to the computer to be executed when the program runs. As a general rule, all statements ends with a semicolon(;), though there are some exceptions to it.

Comments #

Comments are used to write some valuable notes while programming, comments increase the readability of the program. Comments explain the purpose of the program and also helps in understanding how the program works. Comments are not programming statements, they are ignored by compiler while compiling the program. Comments can appear almost anywhere in a program.

There are two ways to write comments:

  1. Single line comment.
  2. MultiLine comment.

Single Line comment #

Single line comment starts with // and ends with a newline.

// including stdio.h header file


#include<math.h> // math.h contains all mathematical related function

MultiLine comment #

MultiLine comments start with / and end with / . Everything in between / and / will be ignored by the compiler.

Purpose: Learning C


Now you know the basic structure of the C program. Let's create a C program.

Create a new project in Code Blocks with the name "Hello" and replace the default code with the following.

  Project: Hello

#include<stdio.h> // include stdio.h library

int main(void)
    printf("Hello"); // print "Hello" to console
    return 0; // return 0 to operating system

How it works:

C language doesn't have any facility for input and output. As a result input and output operations are performed by a set of libraries provided by the compiler, they are formally not the part of C language but they are considered standard for input and output operations in C. The set of libraries which perform input and output operations are called standard I/O library.

To include input and output facility in our program we need to include stdio.h header file. To include header files you must use #include preprocessor directive at the beginning of the file like this:


int main(void) This line starts the main() function, int indicates that main() function returns an integer value. So why return a value at all ? When the program runs, it will need some way of deciding whether program ran successfully or not. A return value of 0 means that program ran successfully, on the other hand, a non-zero value means there was a problem. Next, notice the keyword void between the parentheses after keyword main. The void indicates that main() function does not accept any argument. After the word main(void) , a left brace ({) in line 4 starts the body of the function. A corresponding right brace (} ) in line 7 ends the body of the function. You must always close the body of the function otherwise, the compiler will report a syntax error.

Between ( {} ) is the body of function. The main() function consists of only two statements. The statement in line 5 prints the "Hello" to the console using the using printf() library function (later chapters will discuss printf() function in detail). The statement.

return 0;

does two things. First, it causes the main() function to terminate, second it provides a return value of 0. Note that the main() function consists of only two statements(in line 5 and 6) and they both ends with a semicolon (;). This is how a basic C program works.

This chapter has covered basic components of a C program. In the next chapter, we will learn about Data types in C.