Components of C language

Basic Components of C Program

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:

  1. Single line comment.
  2. 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>

 

Share Button

Feedback is important to us.

One thought on “Components of C language

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!