Chapter – 2
Introduction to C
C is a general-purpose, structured programming language. Its instructions consists of terms that resemble algebraic expression, augmented by certain English keywords such as if, else, for, do and while, etc. C contains additional features that allow it to be used at a lower level, thus bridging the gap between machine language and the more conventional high level language. This flexibility allows C to be used for system programming (e.g. for writing operating systems as well as for applications programming such as for writing a program to solve mathematical equation or for writing a program to bill customers). It also resembles other high level structure programming language such as Pascal and Fortran.
2.1Historical Development of C:
C was an offspring of the ‘Basic Combined Programming Language’ (BCPL) called B, developed in 1960s at Cambridge University. B language was modified by Dennis Ritchie and was implemented at Bell Laboratories in 1972. The new language was named C. Since it was developed along with the UNIX operating system, it is strongly associated with UNIX. This operating system was developed at Bell Laboratories and was coded almost entirely in C.
C was used mainly in academic environments for many years, but eventually with the release of C compiler for commercial use and the increasing popularity of UNIX, it began to gain widespread support among compiler professionals. Today, C is running under a number of operating systems including Ms-DOS. C was now standardized by American National Standard Institute. Such type of C was named ANSI C.
2.2Importance of C:
Now-a-days, the popularity of C is increasing probably due to its many desirable qualities. It is a robust language whose rich set of built-in functions and operators can be used of built-in functions and operators can be used to write any complex program. The C compiler combines the capabilities of an assemble language with the features of a high-level language and therefore it well suited for writing both system software and business packages. In fact, many of the C compilers available in the market are written in C.
Programs written in C are efficient and fast. This is due to its variety of data types and powerful operators. It is many times faster than BASIC (Beginners All Purpose Symbolic Instruction Code – a high level programming language).
There are only 32 keywords and its strength lies in its built-in functions. Several standard functions are available which can be used for developing programs. C is highly portable. This means that C programs written for one computer can be seen on another with little or no modification. Portability is important if we plan to use a new computer with a different operating system.
C Language is well suited for structure programming thus requiring the user to think of a problem in terms of function modules or blocks. A proper collection of these modules would make a complete program. This modular structure makes program debugging, testing and maintenance.
Another important feature of C is its ability to extend itself. A C program is basically a collection of functions that are supported by the C library. We can continuously add our own function to the C library. With the availability of a large number of functions, the programming task becomes simple.
2.3Basic Structure of C programs:
Every C program consists one or more modules called function. One of the function must be called main( ).A function is a sub-routine that may include one or more statements designed to perform a specific task. A C program may contain one or more sections shown in fig:
Global Declaration Section
Main() Function Section
Fig : Basic Structure of a C program
The documentation section consists of a set of comment lines giving the name of the program, the author and other details which the programmer would like to use later. The link section provides instructions to the compiler to link function from the system library. The definition defines all the symbolic constants. There are some variables that are used in more than one function. Such variables are called global variables and are declared in global declaration section that is outside of all the function.
Every C program must have one main( ) function section. This section consists two
parts: declaration part and executable part. The declaration part declares all the variables used in the executable part. These two parts must appear between the opening and the closing braces. The program execution begins at the opening braces and ends at the closing brace. The closing brace of the main ( ) function section is the logical end of the program. All the statements in the declaration and executable parts ends with a semicolon.
The subprogram section contains all the user-defined functions that are called in the main ( ) function. User-defined functions are generally placed immediately after the main ( ) function, although they may appear in any order. All section, except the main ( ) function section may be absent when they are not required.
Written by Lecturer, Eastern College of Engineering Biratnagar
2.4Executing a C Program
Executing a program written in C involves a series of steps:
1.Creating the program;
2.Compiling the program;
3.Linking the program with functions that are needed from the C library; and
4.Executing the program.
The program can be created using any word processing software in non-document mode. The file name should end with the characters “.c” like program .c, lab1.c, etc. Then the command under Ms DOS operating system would load the program stored in the file program
MSC pay .C
and generate the object code. This code is stored in another file under name ‘program.obj’. In case any language errors are found , the compilation is not completed. The program should then be corrected and compiled again.
The linking is done by the command LINK program.obj
which generates the executable code with the filename program.exe. Now the command
would execute the program and give the results.
First Program written in C
Save it as hello.c
# include <stdio.h>
/* header file */
main ( )
/* main ( ) function */
Print (“Hello, World n”);
/* statement */
output : Hello, World
/* Program to calculate the area of a circle */
/* area.c */
# include <stdio.h>
/* library file access */
/* function heading */
float radius, area;
/* variable decleration */
clrscr();/* Console function :Used
to clear screen */
printf (“Enter radious?=”)
/* output statement */
scanf (“%f”, & radius);
/* input statement */
area = 3.14159 * radius*radius ;
/* assignment statement */
printf (“Area=%f”, area);
/* output statement */
getch();/* Console function :Used
to hold the same screen */
Some Important Questions:
1.ʺC language is a middle level languageʺ. Explain this statement?(PU2008) 2.Write down the importance of C.(PU2004)
3.How can you define the structure of C -program? Explain with general form.(PU2003)