Chapter One: Introductory Concepts
Chapter One: Introductory Concepts
Introductory Concepts
This book offers instruction in computer programming using a popular, structured programming language
called C. We will learn how programs can be written in C. In addition, we will see how problems that are
initially described in general terms can be analyzed, outlined and finally transformed into well-organized C
programs. These concepts are demonstrated in detail using many sample problems in this text.
Computers are present in almost all areas of life, such as in homes and home appliances, vehicles, mobiles,
offices, shopping malls, business showrooms, hospitals, banks, etc. Hence, there will be no exaggeration in
saying that “computers are everywhere”. It is common knowledge that a computer is an electronic device
which processes the data, i.e., it transforms the user inputs to desired outputs based on pre-defined instructions.
These pre-defined instructions are normally stored in a computed in the form of software. Hence, a computer
is a collection of hardware and software.
Today’s computers are significantly different from those initially developed. The first computer was devel-
oped in the 1940s and was bigger than a normal room. This model consumed an enormous amount of power
and dissipated so much heat that its continuous use was restricted to a few hours only. In spite of its huge size
and power requirements, this computer’s computing power was much less than today’s scientific calculators.
Today’s personal computers are smaller in size (desktops, laptops, palmtops, etc.) and at the same time are
much more efficient in terms of speed, storage, power-consumption and above all, are very easy to afford.
Figure 1 shows the two most common types of personal computers: desktop and laptop.
Besides these personal computers, there also exist more powerful computers like servers and super-com-
puters, whose storage capacity, processing power as well as speed are extremely high and such computers are
used for high-end applications, such as weather forecasting, space and nuclear research, etc.
Most of us are well aware and use Internet on our personal computers or mobile phones. It is impossible
to imagine life without the Internet today. Another common use of personal computers is in the form of of-
fice networks in many organizations, where several computers are connected with each other via a wired or
wireless network, and is supported by more powerful servers, etc.
(a) (b)
A digital computer is an electronic programmable machine that can process almost all kinds of data. The
programmable feature has made this machine unique as compared to other machines. The same machine can
be used in a small shop, in medical science, for space-research and various engineering activities. All this is
possible by using different programs for different environments/applications.
What is a program? A program is a set of instructions, written in a particular sequence in a computer-related
language. Details about programs will be discussed in Sections 1.4 and 1.6.
is responsible for sending/receiving control signals from and to all components. The dotted lines in Fig. 1.2
represent the communication of control signals, whereas the solid lines denote the transfer of data. Under the
control of CU, the data comes from input device(s) to memory, it is processed in ALU and the result is stored
back in the memory. The processed results are converted to a form that can be understood easily by human
beings and is displayed with the help of an output device (e.g., monitor, printer).
The memory of a computer is of two types: primary memory and secondary memory. Primary memory is
faster in speed, less in size (normally few megabytes) and costlier. It consists of ROM (Read Only Memory)
and RAM (Random Access Memory). ROM is a very small amount of memory used to make the computer
ready for work (this process is called booting). RAM contains all types of intermediate and temporary data to
be used by the CPU. In fact, the CPU can work/process only that data which is present in the RAM. Any data
present in the secondary memory (e.g., hard disk, floppy, CD) needs to be first brought to RAM and only then
can it be used by the CPU. Secondary memory is usually a very large amount of memory (in gigabytes), which
is comparatively cheaper and slower than primary memory, but is permanent in nature. Thus, anything stored
in secondary memory remains available even if the computer is switched off. On the other hand, the contents
of RAM (not ROM, which is also permanent) are lost as soon as the computer is turned off because RAM is
volatile in nature.
1.4.1 Memory
Every piece of information stored within a computer’s memory is encoded as some unique combination of
zeros and ones. These zeros and ones are called bits (binary digits). Each bit is represented by an electronic
device that is, in some sense, either “off” (zero) or “on” (one).
Small computers have memories that are organized into 8-bit multiples called bytes, as illustrated in
Fig. 1.3. Notice that the individual bits are numbered, beginning with 0 (for the rightmost bit) and extending
to 7 (the leftmost bit). Normally, a single character (e.g., a letter, a single digit or a punctuation symbol) will
occupy one byte of memory. An instruction may occupy 1, 2 or 3
bytes. A single numeric quantity may occupy 2 to 8 bytes, depending
on its precision (i.e., the number of significant figures) and its type bit number 7 6 5 4 3 2 1 0
(integer, floating-point, etc.).
A computer typically consists of many different types of memories
One byte
like RAM, ROM, cache, hard disk, etc. All such memories can be
classified into two categories: Fig. 1.3
Primary memory
Secondary memory
Primary Memory Primary memory is a semiconductor memory and is needed for all kinds of processing of
the CPU. In fact, CPU cannot work directly on the other type of memory, i.e., secondary memory. Whatever
data/program is needed by the CPU, it must be present in the primary memory. Hence, the primary memory is
relatively faster, lesser and costly than the secondary memory. There are mainly two types of primary memo-
ries:
Read Only Memory (ROM)
Random Access Memory (RAM)
As the name suggests, ROM is read only. This memory cannot be changed and can be used only by the
CPU. It is needed in the computer to store the Basic Input Output System (BIOS). BIOS is responsible
to make the computer ready for work by its users. This process is called booting and takes few minutes
normally. You can try switching on a computer and observe the messages appearing on the screen. During
that time, you cannot do any work on it. After a few minutes, the login-screen/desktop appears and then you
can resume work, e.g., playing a game, programming, printing, etc. The process starting from switching on
till the computer becomes ready to use, is called booting process. With the help of ROM-BIOS memory,
the booting starts and the necessary programs and data (both are part of Operating System) are loaded from
secondary memory to RAM. This memory is permanent in storage and is very small in size and present in
the form of an IC (Integrated Chip). This Read Only Memory’s presence is essential to start a computer.
The Random Access Memory (RAM) is a volatile memory, i.e., its contents get destroyed as soon as a
computer is switched off. All kinds of processing of the CPU are done in this memory. It means whatever
programs, letters, data, etc. a user types go first to the RAM and whatever output we see on the output screen/
printers, comes through this RAM. Whenever we start any game or any other utility program on a computer,
it gets run (executed) in the primary memory. RAM is a semiconductor memory and is always faster than
magnetic memories. As RAM is volatile, we always need some other memory which is permanent, i.e., where
data can be stored forever and can be loaded inside the RAM whenever needed.
The size of a computer’s memory is usually expressed as multiple of 210 = 1024 bytes. This is referred as
1 K. Modern small computers have primary memories whose sizes typically range from 2 GigaBytes (GB)
to 16 GigaBytes, where 1 GB is equivalent to 210, i.e., 1024 Mega-Bytes (MB), 1 MB is equivalent to 210,
i.e., 1024 KiloBytes (KB) and 1 KB is 1024 bytes. So 1 GB is equivalent to 210 * 210 ¥ 210 bytes.
Secondary Memory The requirement of permanent storage is fulfilled by the presence of a secondary
memory in the form of a hard disk. Every computer consists of one or more hard disks for permanent stor-
age of all data. These are magnetic memories, quite large in size and quite cheap compared to RAM. In hard
disks, data is stored in the form of sectors, tracks and cylinders, where a sector is the smallest unit of storage
and is of 512 bytes. Every bit is stored with the help of magnetization of the hard disk’s surface. Typical size
of hard disks used in personal computers nowadays is 500 GB to 2 TB (TeraBytes). Here One TB is equal to
1024 GB. The size of the hard disks increases to many Tera Bytes for servers, etc. Due to the magnetization
mechanism, the data remains permanently stored even after switching off the computer. The CPU cannot
work on the secondary memory directly, so all the needed data and programs are transferred to RAM for
processing and stored back in the secondary memory after the work is complete. Pen drives, memory cards,
optical disks (DVDs, CDs), i-Pods and other kinds of secondary memories are usually removable in nature,
i.e., can be attached and removed to/from the computer whenever needed. In earlier times, floppy disks and
magnetic tapes were also used as removable storage, but they have become obsolete now.
The physical components of a computer are known as hardware. CPU, RAM, hard disk, keyboard, mouse,
monitor, etc. constitute the hardware. In most simple applications of a computer, the hardware remains similar
and it is the software that helps in using the same hardware for different applications. A computer is incapable
of performing any task with the help of hardware alone. It requires a set of instructions to carry out the specific
task. This set of instructions is known as a program and one or more programs along with their documentation
are called software.
Software used on a computer can be classified as:
(i) System software
(ii) Application software
(i) System software: This category includes softwares that are used to define the functioning of any computer
irrespective of the area/application where it is to be used. Operating systems, compilers, editors, etc.
are examples of system software.
(ii) Application software: In order to use a computer for a specific task, it needs to be instructed accord-
ingly. Such instructions are provided by application software. For example, railway ticket reservation
software. The required set of instructions for the railway ticket reservation/cancellation/enquiry written
in a specific language constitutes the railway ticket reservation software. Weather forecasting software,
online admission/examination software, etc. are other common examples of application software.
Through the interface, the user is provided the support to copy, print, type, listen music, see movies, etc.
Earlier, most of these facilities were provided through various commands (e.g., in DOS, etc.). Nowadays,
all operating systems provide these facilities through graphical interface using icons, etc. This is known as
Graphical User Interface (GUI) and it helps users in easier and interactive operation of a computer. Another
important responsibility of the OS is to manage the various resources present in a computer. CPU, all types of
memory, various input and output devices, all are resources to be efficiently controlled and managed by the
operating system. Any kind of additional hardware to be attached will need the operating system’s support for
its correct working. The operating system also helps users in installing and executing other additional software
such as, new games, utility programs, various compilers, different application softwares, etc. In general, it can
be said that the operating system is the overall in-charge of the whole computer.
Since the inception of the operating system, there have been many OS in the market, and most of these can
be classified into three categories: DOS, Windows OS (GUI based OS), Unix/Linux OS.
1. DOS Disk Operating System (DOS) was developed in 1981 by Microsoft and named as MS-DOS. This
was a single-user, command-based operating system. Users were provided commands like dir, makedir, chdir,
copy, type, print, etc. and all actions were to be done through command prompt. Users needed to remember
these commands and their exact syntax in order to use the computer. MS-DOS continued up to 1994 with its
latest version as MS-DOS 6.22 and was replaced by MS-Windows.
2. Windows OS (GUI Based OS) The concept of using graphical interface was initiated by Apple Com-
puters on their Macintosh machines. The idea was to provide a more convenient way of working and the users
need not remember various commands. This concept was further extended by Microsoft by launching Win-
dows 3.1 software in 1991 which used to work over DOS. Windows 3.1 was not an OS but provided graphical
support for many activities. Based on the popularity of Windows 3.1, Windows 95 was launched by Microsoft
in 1995 as an operating system which used to work through icons (graphical representation of many utilities
and commands). This was further improved as Windows 98, Windows 2000, Windows XP, Windows 7, and
Windows 8. The latest operating system in this series is Windows 10, which is a very user-friendly, powerful
and secure operating system. But all these operating systems are mainly useful on personal computers only.
Multiple users cannot work on the same computer using these operating systems.
3. Unix/Linux OS Unix operating system was developed to provide support for using the same computer
by many users at the same time. It was developed by AT&T, Bell Labs in 1969 and was a multi-user, multi-
tasking, time-sharing operating system. Many users were able to work on the same computer with help of
sharing the time of the CPU in small slots with help of multiple tasks being handled at the same time. The
time slots allotted to each user were quite small so that other users could hardly notice the delay, but this slot
was quite sufficient for each user to get some significant work completed on the CPU. Another very useful
advantage of the Unix OS was its security, which was very difficult to breach. This was also a command-
based operating system and provided commands like ls, mkdir, cd, cat, cp, mv, etc.
Subsequently, Unix also went through lot of changes and provided good GUI support for normal opera-
tions. Nowadays Unix has become a kind of trademark and there exist many Unix like OS, such as HP-UX,
Sun-Solaris, Linux, etc. All of these provide good GUI support and are very secure as compared to Windows
based OS. Due to their security, very few virus attacks are visible in Unix based operating systems. Linux has
further popularized this category by providing it almost for free. Linux is a Unix like OS developed under
GNU project and is totally free for non-commercial usage. Linux tried to break the monopoly of Windows
and their higher costs of OS and has become quite popular in recent years. Many different implementations
of Linux are available in the market now, e.g., Ubuntu, Fedora, Red hat Linux, SUSE, etc.—all of these
are GUI-based, free, very secure, multi-user operating systems. Many of these Linux OS have become very
popular because their cost is nil or minimum and they have almost all features which Windows OS provide.
At the same time, they are less prone to viruses and hence many users prefer to use Linux as an OS in their
computers. Moreover, these OS are being regularly improved by contributions from many developers across
the world, who work to improve these software without any remuneration.
The most visible use of an operating system for most of the users is in the form of storing and accessing
various files and folders. A folder is a collection of sub-folders and files. All operating systems provide
support for organizing the users’ data in the form of files and folders. Almost all operating systems maintain
their files and folders in different drives in the form of a tree structure for storage. A drive represents a
physical/logical partition of the secondary memory, e.g., hard disk, floppy disk, compact disc, pen drive, etc.
Each drive maintains a tree structure for organizing its data. In the tree structure, each file as well as folder
(or sub folder) will have a unique address, through which that file/folder can be accessed. The tree structure
shown in Fig. 1.4 resembles an upside down tree in real life. It starts with a root at the top, and consists of
many branches and leaves. One branch is equivalent to one folder or directory and the leaf represents a file.
Each of the branches can have further sub-branches (i.e., sub-folders or sub-directories) as well as leaves
(i.e., files). On the other hand, leaves are individual entities and cannot be further divided. For example, the
tree structure of Fig. 1.4 represents three folders at the root level, namely system, games and programming.
The programming folder further consists of two sub-folders, namely c_programs and cpp_programs, and
two files named as input.dat and output.txt. The folder c_program further contains three subfolders
and two files, as shown in the figure.
...
windows.exe
...
. . . . . . ...
Fig. 1.4
Usually a file name consists of an extension telling its type, written after the dot (i.e., .txt, .dat, .c, .doc,
.cpp, etc.) and the folder usually does not contain extensions, but it is not compulsory. No two files/folders
can have the same name within the same folder. However, files and folders of different levels can have the
same names. It means that there cannot be another file or folder with ‘system’ name within the root, but a
file or folder within ‘programming’ folder can easily have the name as ‘system’. The main advantage of this
tree structure is that each file and folder always has a unique and complete address, which starts from root
and reaches up to that file/folder covering all in-between folder names. For example, the complete address
of file quad.c is \programming\c_programs\quad.c. This complete address is unique for this file and can
be used from anywhere in the file system to refer to this file. Similarly, the complete address of the folder
games will be \games and that of windows.exe file will be \system\windows.exe. This complete address is
preceded by the drive name, e.g., C:, D:, A:, E, etc. So, if this tree structure of Fig. 1.6 was corresponding
to the C drive of the computer, the complete address of the file quad.c will be C:\programming\c_programs\
quad.c. This address is called absolute address as it is usable from anywhere in the file system. The files
and folders can be referred using relative addresses also, which is always dependent on our present work-
ing directory/folder (commonly referred as pwd). For example, if we are presently working in c_programs
folder, the file quad.c can be easily referred by name ‘quad.c’ instead of the lengthy absolute address. The
relative address of file input.dat from c_programs folder will be ‘..\input.dat’, where ‘..’ represents the need
to go one level up in the tree structure. If backslash (\) is used after the drive name, it represents the root
location of that drive and any subsequent use of backslash is done to separate the folder name from the
next folder/file name. The DOS and the Windows operating systems use backslash (\) in their addressing,
but Unix and Linux operating systems use forward slash (/) for the same purpose. So in case of Unix and
Linux, the absolute address of file quad.c will be /programming/c_progra,s/quad.c.
1.5.2 Compiler
In order to use a computer for different purposes, it needs a set of instructions to do that particular work. These
instructions are usually written in some English like languages known as high level languages. The computer
understands only machine language and thus cannot directly understand these programs and needs some trans-
lator. The problem is similar to a situation when an Indian goes to Germany. He/she will need a translator to
communicate with the German people. The translator will convert Hindi to German and vice versa. Similarly,
if a programmer wants to give some set of instructions written in any high level language, he/she needs a
conversion from high level language to binary language. This conversion is done with the help of a compiler.
A compiler is a kind of system software that translates programs written in high level language to machine
language. The original high level program is called the source program, and the resulting machine-language
program is called the object program. C-compiler, Pascal-compiler, Java-compiler are some examples of
compilers. Just as we will need a different translator if we go to France, a computer will also require different
compilers for different languages.
Interpreter is a software similar to a compiler. It is a system software which is used to convert high
level language programs to machine language but its working is different from that of a compiler. The
compiler converts the whole program to machine language. This converted program may be stored
somewhere in the memory and then may be executed without requiring the presence of the compiler.
But an interpreter converts high level language program one line at a time to machine language and then
executes that converted line. Then the next line is converted and executed, and so on. Thus, the presence
of the interpreter is a must while executing the program. Usually, an interpreter is a smaller and simpler
software than a compiler, but it may take more time for execution. LISP was one of the languages that
used an interpreter instead of a compiler. Programs are debugged using line-by-line execution with help
of interpreters. Nowadays, the Java interpreter is used to execute the byte code of Java.
Program writing is a systematic process. It is not all about coding only. It is not a magical or random process.
It requires following a certain methodology. In order to write a program, following steps must be formally/
informally followed:
1 . Analyze the problem to identify inputs, outputs and processing requirements.
2. Identify various processing steps needed to solve the problem and represent them in a particular way
(algorithm, pseudo code, flow chart, etc.)
3. Refine Step 2 in a way that all the processing steps are detailed enough such that every processing step
is equivalent to one instruction of the programming language.
4. Add the syntax of the programming language to the above representation and it becomes the program.
5. Type the program and compile it after removing all syntax related errors.
6. Run the program and check it with different types of input to verify its correctness. If the results are in-
correct for any combination of inputs, review all the processing steps, and identify the mistake. Correct
the mistake and again continue with Step 4 above.
Steps 2 and 3 of the above process ensure that the processing requirements are represented in a good
manner so that these can be easily converted to the program later. Three commonly used representations are
algorithm, pseudo-code and flow chart.
1.6.1 Algorithm
It is a complete step-by-step representation of the solution of a problem, represented in English like language.
An algorithm can be quite abstract or quite detailed. A detailed algorithm consists of every step, equivalent to
one instruction of a programming language (C, C++, Java, etc.). Following examples will help you in under-
standing the basis process of logic development and writing an algorithm.
Problem 1: Write a program to find addition of two numbers.
It is one of the simplest problems to be implemented. The problem states that two numbers should be read
and their addition result should be displayed. The corresponding algorithm for this problem can be written as
1. Read first and second number.
2. Add both numbers to get the result.
3. Print the result.
Each step in the above algorithm is simple and does not require any further sub-steps.
Problem 2: Write a program to find the bigger number out of two distinct numbers.
Problem 1 needed three simple steps in a sequence. Compared to that, this problem also requires a decision
to be made based on comparison of two values. This problem demands two numbers be read and the number
which is bigger than the other be displayed. The problem statement clearly mentions that both numbers are
distinct and thus they will never be equal. Its equivalent algorithm will be
1. Read first and second number.
2. Compare first number with second number.
3. If first number is bigger than second then print the first number.
4. Otherwise print the second number.
As it can be observed, each step is still simple and does not require any further sub-steps.
Problem 3: Write a program to find the largest number out of three distinct numbers.
Compared to Problem 2, this problem needs three numbers to be read as input. Similarly, each number needs
to be compared with remaining two numbers to find the result (also called as output). Again note that all
numbers are distinct and thus no two numbers can be equal. It can be represented as the following algorithm:
1. Read first, second and third number.
2. Print first number if it is bigger than second and third.
3. Otherwise print second number if it is bigger than first and third.
4. Otherwise print third number.
Here, Steps 2 and 3 are written in an abstract way and these can be made more detailed. For example, first
number can be compared with other two numbers in two different ways: (a) compare first number with second
as well as third number in same step, and (b) compare first number with second initially and then with third
number in next step.
Following two algorithms will clarify the difference between the two approaches:
Algorithm 3 (a)
1. Read first, second and third number.
2. If first number is bigger than second and first number is bigger than third
3. Print first number as largest
4. Otherwise (means that first is not largest)
5. If second number is bigger than first and second number is bigger than third
6. Print second number as largest
7. Otherwise (means that second is not largest and first is also not largest)
8. Print third number as maximum.
Algorithm 3(b)
1. Read first, second and third number.
2. If first number is bigger than second number
3. If first number is bigger than third number
4. Print first number as largest
5. Otherwise (means third is bigger than first and first was already bigger than second)
6. Print third number as largest
7. Otherwise (means second number is bigger than first & thus second to be compared with third)
8. If second number is bigger than third number
9. Print second number as largest
10. Otherwise (means third is bigger than second and second was already bigger than first)
11. Print third number as largest.
Both algorithms 3(a) and 3(b) have each step equivalent to some programming language instruction and
thus, are complete solutions of the problem.
Problem 4: Write a program to find the average of n numbers.
After analysis of this problem, we can write an abstract algorithm as
1. Read the value of n.
2. Read n numbers.
3. Add these n numbers.
4. Divide the addition result by n to get the average.
5. Print the average.
In the above algorithm, Steps 2 and 3 are not simple. Both of these require repetition of the work. The
details of that repetition can also be specified to make the algorithm clearer. The detailed algorithm with the
repetition details are given below:
1. Read the value of n.
2. Assign 0 to sum.
3. Repeat n times.
4. Read number.
5. Add number to sum.
6. End repeat.
7. Divide sum by n to get the average.
8. Print average.
START
START
Is number1 >
number2 ?
sum ¨ number1 + number2
STOP STOP
(a) (b)
Fig. 1.6
START
STOP
Problem 3(a)
START
STOP
Problem 3(b)
Fig. 1.7
The program A has been divided into 3 components—B, C and D. The D component is found to be solvable
easily and thus need not be further subdivided. However, B and C are further divided into sub-components
and the process continues till each sub-component is not found to be solvable without further sub-division.
The bottom-up design is the reverse of the top-down approach. Here, the process starts with the identifica-
tion of the smallest sub-components of the total program, which can be easily implemented. Such smallest
B C D
E F G H
I J K L M
Fig. 1.9
components are combined to reach to a more abstract level and plan components of the higher level. This
process is continued till the complete program does not get realized. The main drawback of this approach is
that it is rarely possible to identify smallest
sub-components needed for the program,
especially for bigger programs. The typical I J F K L M H D
method of developing a program using the
bottom-up approach will look the reverse of E G
Fig. 1.9, where the designed should be able
to identify smallest components D, F, H, I, J,
K, L and M, which do not require any other B C
component for their implementation. Then
these components need to be successively
grouped together so as to reach the final so-
lution for program A, as shown in Fig. 1.10.
A
1.8.2 Modular Programming
This principle of structured programming aims Fig. 1.10
to have logical divisions of the total solution.
The program design already suggests various components to be used for planning the final solution. Each
component can be implemented as a part of the main program also, and the main program can become quite
lengthy. However, the modular programming principle suggests writing the code as a collection of many mod-
ules. A module is a portion of the program which is responsible to carry out a certain work, and can be used
with other modules. A module may be considered as decomposed into successively subordinate modules and
many modules can be combined also to form a superior module. A module can be repeatedly called, may be
with different input values and can also be reused in other programs with little/as modifications. The advantage
of developing modules is that the superior module need not physically include the codes of its subordinate
modules within itself. All the modules are developed separately and the superior module can call the subordinate
modules. This process is known as ‘calling’ of subordinate module by superior module. Alternatively, it can
be said that subordinate module is ‘called by’ the superior module. The superior module is called the calling
module and the subordinate module is called the called module. The advantages of modular programming are
better readability of a program, easy debugging, fault localization, ease of modification, better maintenance, etc.
There are many different languages that can be used to program a computer. The most basic of these is machine
language—a collection of very detailed, cryptic instructions that control the computer’s internal circuitry. This
is the natural dialect of the computer. Very few computer programs are actually written in machine language,
however, for two significant reasons: First, because machine language is very cumbersome to work with and
second, because every different type of computer has its own unique instruction set. Thus, a machine-language
program written for one type of computer cannot be run on another type of computer without significant
alterations.
Usually, a computer program will be written in some high level language, whose instruction set is more
compatible with human languages and human thought processes. Most of these are general-purpose languages
such as C, Pascal, Fortran and BASIC. There are also various special-purpose languages that are specifically
designed for some particular type of application. Some common examples are CSMP and SIMAN, which are
special-purpose simulation languages, and LISP, a list- processing language that is widely used for artificial
intelligence applications.
As a rule, a single instruction in a high-level language will be equivalent to several instructions in machine
language. This greatly simplifies the task of writing complete, correct programs. Furthermore, the rules for
programming in a particular high-level languages are much the same for all computers, so that a program
written for one computer can generally be run on many different computers with little or no alteration. Thus,
we see that a high-level language offers three significant advantages over a machine language: simplicity,
uniformity and portability (i.e., machine independence).
1.10 INTRODUCTION TO C
C is a general-purpose, structured programming language. Its instructions consist of terms that resemble al-
gebraic expressions, augmented by certain English keywords such as if, else, for, do and while. In this respect
C resembles other high-level structured programming languages such as Pascal and Fortran. C also contains
certain 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 languages. This flexibility allows C to be used for systems
programming (e.g., for writing operating systems) as well as for applications programming (e.g., for writing a
program to solve a complicated system of mathematical equations, or for writing a program to bill customers).
C is characterized by the ability to write very concise source programs, due in part to the large number of
operators included within the language. It has a relatively small instruction set, though actual implementa-
tions include extensive library functions which enhance the basic instructions. Furthermore, the language
encourages users to write additional library functions of their own. Thus, the features and capabilities of the
language can easily be extended by the user.
C compilers are commonly available for computers of all sizes, and C interpreters are becoming increasingly
common. The compilers are usually compact, and they generate object programs that are small and highly
efficient when compared with programs compiled from other high-level languages. The interpreters are less
efficient, though they are easier to use when developing a new program.
Another important characteristic of C is that its programs are highly portable, even more so than with other
high-level languages. The reason for this is that C relegates most computer-dependent features to its library
functions. Thus, every version of C is accompanied by its own set of library functions, which are written for
the particular characteristics of the host computer. These library functions are relatively standardized, how-
ever, and each individual library function is generally accessed in the same manner from one version of C to
another. Therefore, most C programs can be processed on many different computers with little or no alteration.
1.10.1 History of C
C was originally developed in the 1970s by Dennis Ritchie at Bell Telephone Laboratories, Inc. (now a part
of AT&T). It is an outgrowth of two earlier languages, called BCPL and B, which were also developed at Bell
Laboratories. C was largely confined to use within Bell Laboratories until 1978, when Brian Kernighan and
Ritchie published a definitive description of the language.* The Kernighan and Ritchie description is com-
monly referred to as ‘K&R C.’
Following the publication of the K&R description, computer professionals, impressed with C’s many desirable
features, began to promote the use of the language. By the mid 1980s, the popularity of C had become wide-
spread. Numerous C compilers and interpreters had been written for computers of all sizes, and many commercial
* Brian W. Kernighan and Dennis M. Ritchie, The C Programming Language, Prentice-Hall, 1978.
application programs had been developed. Moreover, many commercial software products that were originally
written in other languages were rewritten in C in order to take advantage of its efficiency and its portability.
Early commercial implementations of C differed somewhat from Kernighan and Ritchie’s original
definition, resulting in minor incompatibilities between different implementations of the language. These
differences diminished the portability that the language attempted to provide. Consequently, the American
National Standards Institute** (ANSI committee X3J11) has developed a standardized definition of the C
language. Virtually, all commercial C compilers and interpreters now adhere to the ANSI standard. Many
also provide additional features of their own.
In the early 1980s, another high-level programming language, called C++, was developed by Bjarne
Stroustrup*** at the Bell Laboratories. C++ is built upon C, and hence all standard C features are available
within C++. However, C++ is not merely an extension of C. Rather, it incorporates several new fundamental
concepts that form a basis for object-oriented programming—a new programming paradigm that is of interest
to professional programmers. We will not describe C++ in this book, except to mention that a knowledge of
C is an excellent starting point for learning C++.
This book describes the features of C that are included in the ANSI standard and are supported by commercial C
compilers and interpreters. The reader who has mastered this material should have no difficulty in customizing
a C program to any particular implementation of the language.
*** ANSI Standard X3.159—1989. American National Standards Institute, 1430 Broadway, New York, NY, 10018. (See also Brian
W. Kernighan and Dennis M. Ritchie, The C Programming Language, 2d ed., Prentice-Hall, 1988.)
*** Stroustrup, Bjarne, The C++ Programming Language, 2d ed., Addison-Wesley, 1991.
Example 1.1
Area of a Circle Here is an elementary C program that reads in the radius of a circle, calculates its area and
then writes the calculated result.
/* program to calculate the area of a circle */ /* TITLE (COMMENT) */
#include <stdio.h> /* LIBRARY FILE ACCESS */
main() /* FUNCTION HEADING */
{
float radius, area; /* VARIABLE DECLARATION */
printf("Radius = ? "); /* OUTPUT STATEMENT (PROMPT) */
scanf("%f", &radius); /* INPUT STATEMENT */
area = 3.14159 * radius * radius; /* ASSIGNMENT STATEMENT */
printf("Area = %f", area); /* OUTPUT STATEMENT */
}
The comments at the end of each line have been added in order to emphasize the overall program organiza-
tion. Normally, a C program will not look like this. Rather, it might appear as shown below.
/* program to calculate the area of a circle */
#include <stdio.h>
main()
{
float radius, area;
printf("Radius = ? ");
scanf("%f", &radius);
area = 3.14159 * radius * radius;
printf("Area = %f", area);
}
4. The third line is a heading for the function main. The empty parentheses following the name of the
function indicate that this function does not include any arguments.
5. The remaining five lines of the program are indented and enclosed within a pair of braces. These five
lines comprise the compound statement within main.
6. The first indented line is a variable declaration. It establishes the symbolic names radius and area as
floating-point variables (more about this in the next chapter).
7. The remaining four indented lines are expression statements. The second indented line (print f)
generates a request for information (namely, a value for the radius). This value is entered into the
computer via the third indented line (scan f).
8. The fourth indented line is a particular type of expression statement called an assignment statement.
This statement causes the area to be calculated from the given value of the radius. Within this state-
ment the asterisks (*) represent multiplication signs.
9. The last indented line (print f) causes the calculated value for the area to be displayed. The numeri-
cal value will be preceded by a brief label.
10. Notice that each expression statement within the compound statement ends with a semicolon. This is
required of all expression statements.
11. Finally, notice the liberal use of spacing and indentation, creating whitespace within the program.
The blank lines separate different parts of the program into logically identifiable components, and the
indentation indicates subordinate relationships among the various instructions. These features are not
grammatically essential, but their presence is strongly encouraged as a matter of good programming
practice.
Execution of the program results in an interactive dialog such as that shown below. The user’s response is
underlined for clarity.
Radius = ? 3
Area = 28.274309
Before concluding this chapter let us briefly examine some important characteristics of well-written computer
programs. These characteristics apply to programs that are written in any programming language, not just C.
They can provide us with a useful set of guidelines later in this book, when we start writing our own C programs.
1. Integrity. This refers to the accuracy of the calculations. It should be clear that all other program
enhancements will be meaningless if the calculations are not carried out correctly. Thus, the integrity
of the calculations is an absolute necessity in any computer program.
2. Clarity refers to the overall readability of the program, with particular emphasis on its underlying logic.
If a program is clearly written, it should be possible for another programmer to follow the program
logic without undue effort. It should also be possible for the original author to follow his or her own
program after being away from the program for an extended period of time. One of the objectives in the
design of C is the development of clear, readable programs through an orderly and disciplined approach
to programming.
3. Simplicity. The clarity and accuracy of a program are usually enhanced by keeping things as simple
as possible, consistent with the overall program objectives. In fact, it may be desirable to sacrifice a
REVIEW QUESTIONS
1.1 Draw the block diagram of a computer and explain various blocks.
1.2 What are various types of memory? How do they differ from each other.
1.3 Compare the use and working of primary and secondary memory.
1.4 What is need of an operating system? What are its responsibilities?
1.5 What are the various types of operating systems? Compare their features and working.
1.6 Explain the tree structure of storage system. Differentiate between absolute addressing and relative
addressing of files.
1.7 Write an algorithm, pseudo code and flow chart for finding:
(i) Minimum out of three numbers
(ii) Maximum out of four numbers
(iii) Factorial of a number
(iv) Smallest out of n numbers
1.8 List various phases of software development and describe each briefly.
1.9 How is structured programming done? What problems might be faced if programs are not developed
in a structured way?
1.10 What is machine language? How does machine language differ from high-level languages?
1.11 Name some commonly used high-level languages. What are the advantages of using high-level lan-
guages?
1.12 What is meant by compilation? What is meant by interpretation? How do these two processes differ?
1.13 What are the major components of a C program? What significance is attached to the name main?
1.14 How can comments (remarks) be included within a C program? Where can comments be placed?
1.15 Are C programs required to be typed in lowercase? Is uppercase ever used in a C program? Explain.
1.16 Why are some of the statements within a C program indented? Why are empty lines included within
a typical C program?
1.17 Summarize the meaning of each of the following program characteristics: integrity, clarity, simplicity,
efficiency, modularity and generality. Why is each characteristic important?
1.18 Compare top-down and bottom-up approaches of program design.