Basic Elements of a C Program
Last updated on September 24, 2020
A basic C program has the following form.
1 2 3 4 5 6 7 | comments
preprocessor directives
int main()
{
statements;
}
|
This is the structure of a 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 special program called preprocessor (which is built into the compiler). 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 a header file? A header file contains information about the functions we want to use in our programs. It always ends with .h
extension. For example, the stdio.h
header file contains information about the input and output functions. 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:
#include<stdio.h>
The above line causes the preprocessor to include a copy of stdio.h
header file, at this point in the program. The header files are supplied by the C compiler. 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 constants. To include stdio.h
and math.h
in your program do this:
1 2 | #include<stdio.h>
#include<math.h>
|
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 consist 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 end with a semicolon(;
), though there are some exceptions to it.
Comments #
Comments are used to write some valuable notes while programming. They also increase the readability of the program. Comments may 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:
- Single line comment.
- Multi-Line comment.
Single Line comment #
Single line comment starts with //
and goes on until the end of the line.
1 2 3 4 5 | // including stdio.h header file
#include<stdio.h>
#include<math.h> // math.h contains all mathematical related function
|
Multi-Line comment #
Multi-Line comment starts with /*
and ends with */
. Everything in between /*
and */
will be ignored by the compiler.
1 2 3 4 5 6 | /*
Author: overiq.com
Purpose: Learning C
*/
#include<stdio.h>
|
Now you know the basic structure of the C program. Let's create a simple C program. Create a new project in Code Blocks with the name "Hello"
and replace the default code with the following:
1 2 3 4 5 6 7 8 9 10 11 12 | /*
Project: Hello
Author: overiq.com
*/
#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:
#include<stdio.h>
The line int main(void)
starts the main()
function, int
indicates that the main()
function returns an integer value. So why we return a value? When the program runs, OS needs some way of deciding whether the program ran successfully or not. A return value of 0
means that the 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 9 starts the body of the function. A corresponding right brace (}
) in line 12 ends the body of the function. You must always close the body of the function otherwise, the compiler will report a syntax error. Between the braces ( {}
), we have the body of the function. The main()
function consists of only two statements. The statement in line 10 prints the "Hello"
to the console using the using the printf()
library function (we discuss the printf()
function in detail in lesson Input and Output in C). 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 10 and 11) 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.
Load Comments