C is a language developed by Dennis Ritchie. C has its foundation in BCPL. C is treated as a Middle level language as it has essential features of high-level language and assembly language. Using C, programmers can access and manipulate bits, bytes and addresses. Programs written in C are platform independent.
In C language programmers can define variables to hold values of specific type termed as data type. A data type defines the type of values a variable can hold and operations that can be carried on those variables. Integer, character and float are few data types that are used in C language.
A variable in C can store a single value, if a programmer wants to store more than one value in a variable it is not possible. To do this C language supports arrays. Arrays are contiguous memory allocations to store more than one value. Arrays have an identifier and an index associated with each value. Number of values an array can store depends on program logic and the programmer has to handle errors such as memory overflow or array index out of bounds.
C language supports implicit and explicit type conversion. Example of implicit type conversion is that a variable of float can store integer value. Examples of explicit type conversion include character variables that can store integer value.
C language supports keywords. Keywords have their own definition which can not be changed. C language supports 32 keywords. Table 1 below provides a set of keywords.
Table 1 Keywords of C language | |||
auto | double | int | struct |
break | else | long | switch |
case | enum | register | typedef |
char | extern | return | union |
const | float | short | unsigned |
continue | for | signed | void |
default | goto | sizeof | volatile |
do | if | static | while |
C is a structured language. It supports compartmentalization of code. Code compartaloization keeps code data and programming instructions within it without affecting the rest of the program compartments. Code compartmentalization supports code sharing.
C language has loop constructs like while, do-while and for. Loops are used for repetition of a particular set of programming instructions.
Large programs of C can be broken down into subroutines known as functions. In C, functions are child programs of the parent function program main ( ). When the size of the program increases functions are used to break them into small units each having its own objectives. In C language functions have an opening and closing and within which programming instructions are written. Function opening is marked by this curly brace “{” and closing is marked by this curly brace “}”.
C has the ability to interact with computer hardware. Hence, programs such as Compiler and Interpreter are written in C.
A computer program can be interpreted or compiled. C language is designed to be compiled. To compile C language, Compiler is used. C compiler transforms source code into object code. This transformation is done all at once instead of line-by-line. Object code for upper case characters and lower case characters are different.
All C programs must contain the main ( ) function. Program execution begins with main( ). mian ( ) contains the program logic. Basic functionalities such as input/output(I/O)operations, mathematical computations or character handling required in most of the C program are developed and stored in the standard library used by C language compilers.
Functions developed and stored in the standard library of C are called inbuilt functions and have a specific identifier (name). These functions are stored in the standard library in the form of object code. When a standard C library function calls are made C compiler calls linker to link object code to standard library C functions.
In C a single large program can be written in one or more files. Each file can be compiled separately. All separately compiled files are linked together to build one standalone object code. If a program logic of one file is changed then only that file needs to be compiled and not the whole program, this improves compilation time. Writing programming instructions of large programs into subprograms enables multiple programmers to work on the same program simultaneously.
Object code of C is stored in four memory containers. First container holds executable code, the second holds global variables, the third container is a stack and the fourth container is heap. Stack holds the address of function calls, arguments to functions, and local variables. Stack saves the CPU state. C language uses heaps for memory management.
C language is used to develop operating systems such as Widows, UNIX, Linux and Android. Most of the device driver programmers are written in C language. Many of the electronic devices such as microwave ovens, washing machines and digital cameras require instructions to work, these instructions are written in C language.
Most of the 3D computer games are written in C language. 3D computer game frameworks such as DirectX are written in C.
An example C program:
/* This program finds the sum of three numbers. This is an example program. */ int main ( ) { int p, n; float r, s = 0; p = 1000; n =3; r = 8.5; S = p + n + r; printf(“%f\n”, si); return 0; }
Rules to be followed in developing a C program.
- To improve readability of a C program each instruction should be written on a separate line.
- Program instruction should follow the logic of the program.
- Lower case letters are used to write program instructions.
- Each instruction must end with a semicolon.
Programmers can provide important messages using comments. Comments are used to provide program objectives. Syntax of comment is as follows:
/*Comment statement -1;
Comment statement -2;
.
.
.
Comment statement - n
*/
</pre
This is the syntax to comment multiple lines. To comment single line following syntax is used:
// comment statement -1;
// comment statement -2;
.
.
.
// comment statement – n
To write a C language program C language editor is used. When a program is written, the compiler converts this program into object code. Compilers also use linker to link standard library functions and debugger to find syntax errors.
C language supports three types of instructions –
- Type Declaration Instructions
- Arithmetic Instructions
- Control Instructions
Type declaration instruction
These instructions are used to declare variable types. These instructions form the beginning of the main ( ) function.
Arithmetic Instruction
Arithmetic instructions consist of the “=” operator. Arithmetic expressions are built using variables and constants and put on the right hand side of the “=” operator. Value of the arithmetic expression is assigned to the variable on the left hand side of the “=” operator.
Control Instructions
Control instructions determine the execution path of program instructions. C language supports 4 types of control instructions these are:
- Sequence Control Instructions
- Decision Control Instructions
- Loop Control Instructions
- Case Control Instructions
Sequence control instructions execute instructions as they are written in the program. Decision and case control instructions execute instructions based on a particular instruction and case. To repeatedly execute a particular instruction loop control instructions are used.
C is a general purpose language.
Leave a Reply