Introduction to C Language
C Language is a high level programming language developed at “AT & T’s Bell Laboratories”, USA in 1972.
It was written by Dennis Ritchie and Brian Kernighan.
Features of C Language
- C is a robust language having built-in functions, data types and operators to write complex programs.
- C Language is a case sensitive language i.e. capital letters and small letters are treated differently.
- Program written in C language are very efficient and fast. This is due to its variety of data types and powerful operators.
- It is faster than other programming languages. Hence helps developers in saving their time.
- It has the ability to extend itself.
- It is easy to learn and use C language.
Compilation of C language program
The compilation process of a C program takes place as follows:
- C program must be saved with extension “.C”. For example, the program is saved with name One.C.
- After compilation and linking, three more files are generated named as:
One.bak – Back Up file which contains same code of c program.
One.Obj – Object program.
One.exe -Executable file which can be run directly by the programmer.
Structure of a C Language Program
There are various parts of a C Program They are as follows:
- Documentation Section
- Link Section
- Definition Section
- Global Declaration Section
- main() function Section
- Declaration Part
- Executable Part
- Subprogram Section [User Defined Function Section]
1. Documentation Section
This is the very first section of a C Program. In this section, we use comments to specify various aspects related to the program.
In C language, we can use comments as follows
/* This is a comment */
In C language, Comments start with /* and end with */ Comments can go across multiple lines. They are used anywhere in a program.
2. Link Section
Link section is used to specify the header files to be used in the program and to perform conditional compilation of the program.
Example: #include<stdio.h>
stdio.h header file is included in a program to use input/output functions of C language like printf() and scanf() in our program
3. Definition Section
This section is used to define the macros and symbolic constants to be used in the program.
Examples:
i. #define PI 3.141
Here, symbolic constant with name PI has been defined which contains value 3.141.
ii. #define cube(x) x*x*x
Here, macro with name cube has been defined. It contains one argument named x. When this macro is invoked, it will return cube of argument passed to it.
4. Global Declaration Section
This section is used to define global variables. The global variables can be used throughout the program. They can be accessed across multiple functions and any changes made to them are available throughout the program.
They are declared at the top of a C program. They are defined and initialized like ordinary variables.
5. main() Function Section
Every program written in C Language must have a main() function. The main() function has two sub parts.
(i) Declaration Part
In this part we can declare local variables to be used within function main().
(ii) Executable Part
In this part, we write the statements of C language which are to be executed when we run the program.
6. Subprogram Section
This section contains all the user defined functions designed by the programmer. These functions can be called by the main() function as well as by other functions. These functions can also be defined before main() function.
Example
/* Documentation Section*/
/*
Program to display different sections of C program
By : lovejot
*/
/*Link Section */
#include<stdio.h>
/*Definition Section */
#define PI 3.141
/*Global Declaration Section */
int r;
int Calculate(int r);
/*main() Function */
int main()
{
/*Declaration Part*/
int a;
/* Executable Part */
r=5;
a=Calculate(r); /* Executable Part */
printf(“a=%d”,a); /* Executable Part */
return(0);
}
/* Subprogram section*/
int Calculate(int r)
{
return PI*r*r;
}
Character Set of C Language
Various characters and symbols which can be used in C language are known as the character set of C Language.
- Alphabets: a-z,A-Z
- Digits:- 0-9
- Special Characters:- #, Colon(:),Semi Colon (;), Comma(,) etc.
- White Spaces:- Blank Space, Tab, Backspace, Enter etc.
Token
Token is the smallest identifiable part of a C Program. Different types of tokens available in C are.
- Keyword
- Identifier
- Punctuators
- Variable
- Constant
- Operators
Keyword
Keyword is the reserved word provided by C language. There are basically 32 standard keywords provided by C Language.
auto | asm | break | case | char | const |
continue | default | do | double | else | enum |
extern | float | for | goto | if | int |
long | register | return | short | signed | sizeof |
static | struct | switch | typedef | union | unsigned |
void | while |
Identifier
Identifier refers to those quantities within a program which are defined by a programmer depending upon the requirements.
Rules for naming an identifier are:-
- Both digits and alphabets are allowed.
- Variable name can contain digits only after first character.
- Keyword can’t be used to name a variable.
- No special character other than underscore can be used.
- It can’t contain spaces.
Punctuators
Various punctuators in C are:
a) Colon (:) It is used with control structures, functions an classes to specify their start
b) Comma (,) It is used to separate variables during variable definition and expressions.