Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Mcs 011

Download as pdf or txt
Download as pdf or txt
You are on page 1of 197

Problem Solving

UNIT 1 PROBLEM SOLVING


Structure
1.0 Introduction
1.1 Objectives
1.2 Problem - Solving Techniques
1.2.1 Steps for Problem - Solving
1.2.2 Using Computer as a Problem-Solving Tool
1.3 Design of Algorithms
1.3.1 Definition
1.3.2 Features of Algorithm
1.3.3 Criteria to be followed by an Algorithm
1.3.4 Top Down Design
1.4 Analysis of Algorithm Efficiency
1.4.1 Redundant Computations
1.4.2 Referencing Array Elements
1.4.3 Inefficiency Due to Late Termination
1.4.4
1.4.5
o m
Early Detection of Desired Output Condition
Trading Storage for Efficient Gains
1.5
1.5.1
. c om
Analysis of Algorithm Complexity
Computational Complexity
1.5.2
1.5.3
o t .c
The Order of Notation
Rules for using the Big - O Notation

1.6
1.5.4
Flowcharts
s p ot
Worst and Average Case Behavior

1.7
1.6.1
Summary
o g sp
Basic Symbols used in Flowchart Design

1.8
b l og
Solutions / Answers
1.8
. bl
Further Readings

6
1.0 0 6.
INTRODUCTION
0
i a 00
In our daily life, we routinely encounter and solve problems. We pose problems that

o n ia
we need or want to solve. For this, we make use of available resources, and solve

p o on
them. Some categories of resources include: the time and efforts of yours and others;
tools; information; and money. Some of the problems that you encounter and solve are

l po
quite simple. But some others may be very complex.

i
u n il
In this unit we introduce you to the concepts of problem-solving, especially as they
pertain to computer programming.
s un
The problem-solving is a skill and there are no universal approaches one can take to
ssolving problems. Basically one must explore possible avenues to a solution one by
one until s/he comes across a right path to a solution. In general, as one gains
experience in solving problems, one develops one’s own techniques and strategies,
though they are often intangible. Problem-solving skills are recognized as an integral
component of computer programming. It is a demand and intricate process which is
equally important throughout the project life cycle especially – study, designing,
development, testing and implementation stages. The computer problem solving
process requires:

• Problem anticipation
• Careful planning
• Proper thought process
• Logical precision
• Problem analysis
• Persistence and attention.
7
An Introduction to C At the same time it requires personal creativity, analytic ability and expression. The
chances of success are amplified when the problem solving is approached in a
systematic way and satisfaction is achieved once the problem is satisfactorily solved.
The problems should be anticipated in advance as far as possible and properly defined
to help the algorithm definition and development process.

Computer is a very powerful tool for solving problems. It is a symbol-manipulating


machine that follows a set of stored instructions called a program. It performs these
manipulations very quickly and has memory for storing input, lists of commands and
output. A computer cannot think in the way we associate with humans. When using
the computer to solve a problem, you must specify the needed initial data, the
operations which need to be performed (in order of performance) and what results you
want for output. If any of these instructions are missing, you will get either no results
or invalid results. In either case, your problem has not yet been solved. Therefore,
several steps need to be considered before writing a program. These steps may free
you from hours of finding and removing errors in your program (a process called
debugging). It should also make the act of problem solving with a computer a much
simpler task.

All types of computer programs are collectively referred to as software. Programming


o m
c
languages are also part of it. Physical computer equipment such as electronic circuitry,
.
o t
input/output devices, storage media etc. comes under hardware. Software governs the
functioning of hardware. Operations performed by software may be built into the

s p
hardware, while instructions executed by the hardware may be generated in software.
The decision to incorporate certain functions in the hardware and others in the

o g
software is made by the manufacturer and designer of the software and hardware.
Normal considerations for this are: cost, speed, memory required, adaptability and
l
reliability of the system. Set of instructions of the high level language used to code a
b
6 .
problem to find its solution is referred to as Source Program. A translator program
called a compiler or interpreter, translates the source program into the object

0 0
program. This is the compilation or interpretation phase. All the testing of the source
program as regards the correct format of instructions is performed at this stage and the

i a
errors, if any, are printed. If there is no error, the source program is transformed into
the machine language program called Object Program. The Object Program is

o n
executed to perform calculations. This stage is the execution phase. Data, if required

p o by the program, are supplied now and the results are obtained on the output device.

i l Data,

u n Source
Program
Computer
System
Object
Program if

s required

Results

1.1 OBJECTIVES
After going through this unit, you should be able to:

• apply problem solving techniques;


• define an algorithm and its features;
• describe the analysis of algorithm efficiency;
• discuss the analysis of algorithm complexity; and
• design flowcharts.

8
Problem Solving
1.2 PROBLEM - SOLVING TECHNIQUES
Problem solving is a creative process which defines systematization and
mechanization. There are a number of steps that can be taken to raise the level of
one’s performance in problem solving.

1.2.1 Steps for Problem - Solving


A problem-solving technique follows certain steps in finding the solution to a problem.
Let us look into the steps one by one:

Problem definition phase


The success in solving any problem is possible only after the problem has been fully
understood. That is, we cannot hope to solve a problem, which we do not understand.
So, the problem understanding is the first step towards the solution of the problem. In
problem definition phase, we must emphasize what must be done rather than how is it
to be done. That is, we try to extract the precisely defined set of tasks from the
problem statement. Inexperienced problem solvers too often gallop ahead with the
task of problem - solving only to find that they are either solving the wrong problem
o m
or solving just one particular problem.
. c
Getting started on a problem
o t
There are many ways of solving a problem and there may be several solutions. So, it
s p
is difficult to recognize immediately which path could be more productive. Sometimes

o
you do not have any idea where to begin solving a problem, even if the problem has g
b l
been defined. Such block sometimes occurs because you are overly concerned with
the details of the implementation even before you have completely understood or

6 .
worked out a solution. The best advice is not to get concerned with the details. Those

0
can come later when the intricacies of the problem has been understood.

0
The use of specific examples
i a
o n
To get started on a problem, we can make use of heuristics i.e., the rule of thumb. This
approach will allow us to start on the problem by picking a specific problem we wish
o
to solve and try to work out the mechanism that will allow solving this particular
p
i l
problem. It is usually much easier to work out the details of a solution to a specific
problem because the relationship between the mechanism and the problem is more

u n
clearly defined. This approach of focusing on a particular problem can give us the
foothold we need for making a start on the solution to the general problem.

sSimilarities among problems


One way to make a start is by considering a specific example. Another approach is to
bring the experience to bear on the current problem. So, it is important to see if there
are any similarities between the current problem and the past problems which we have
solved. The more experience one has the more tools and techniques one can bring to
bear in tackling the given problem. But sometimes, it blocks us from discovering a
desirable or better solution to the problem. A skill that is important to try to develop
in problem - solving is the ability to view a problem from a variety of angles. One
must be able to metaphorically turn a problem upside down, inside out, sideways,
backwards, forwards and so on. Once one has developed this skill it should be
possible to get started on any problem.

Working backwards from the solution


In some cases we can assume that we already have the solution to the problem and
then try to work backwards to the starting point. Even a guess at the solution to the
9
An Introduction to C problem may be enough to give us a foothold to start on the problem. We can
systematize the investigations and avoid duplicate efforts by writing down the various
steps taken and explorations made. Another practice that helps to develop the problem
solving skills is, once we have solved a problem, to consciously reflect back on the
way we went about discovering the solution.

1.2.2 Using Computer as a Problem - Solving Tool

The computer is a resource - a versatile tool - that can help you solve some of the
problems that you encounter. A computer is a very powerful general-purpose tool.
Computers can solve or help to solve many types of problems. There are also many
ways in which a computer can enhance the effectiveness of the time and effort that
you are willing to devote to solving a problem. Thus, it will prove to be well worth the
time and effort you spend to learn how to make effective use of this tool.
In this section, we discuss the steps involved in developing a program. Program
development is a multi-step process that requires you to understand the problem,
develop a solution, write the program, and then test it. This critical process determines
the overall quality and success of your program. If you carefully design each program

o
using good structured development techniques, your programs will be efficient, error- m
free, and easy to maintain. The following are the steps in detail:
. c
1. Develop an Algorithm and a Flowchart.
o t
2. Write the program in a computer language (for example say C programming
language).
s p
3. Enter the program using some editor.
4. Test and debug the program.
o g
l
5. Run the program, input data, and get the results.

b
1.3
6 .
DESIGN OF ALGORITHMS

0 0
The first step in the program development is to devise and describe a precise plan of
a
what you want the computer to do. This plan, expressed as a sequence of operations,
i
o n
is called an algorithm. An algorithm is just an outline or idea behind a program.
something resembling C or Pascal, but with some statements in English rather than

p o within the programming language. It is expected that one could translate each pseudo-
code statement to a small number of lines of actual code, easily and mechanically.

i l 1.3.1 Definition

u n An algorithm is a finite set of steps defining the solution of a particular problem. An

s algorithm is expressed in pseudocode - something resembling C language or Pascal,


but with some statements in English rather than within the programming language.
Developing an efficient algorithm requires lot of practice and skill. It must be noted
that an efficient algorithm is one which is capable of giving the solution to the
problem by using minimum resources of the system such as memory and processor’s
time. Algorithm is a language independent, well structured and detailed. It will enable
the programmer to translate into a computer program using any high-level language.

1.3.2 Features of Algorithm


Following features should be present in an algorithm:
Proper understanding of the problem
For designing an efficient algorithm, the expectations from the algorithm should be
clearly defined so that the person developing the algorithm can understand the
expectations from it. This is normally the outcome of the problem definition phase.

10
Use of procedures / functions to emphasize modularity Problem Solving

To assist the development, implementation and readability of the program, it is


usually helpful to modularize (section) the program. Independent functions perform
specific and well defined tasks. In applying modularization, it is important to watch
that the process is not taken so far to a point at which the implementation becomes
difficult to read because of fragmentation. The program then can be implemented as
calls to the various procedures that will be needed in the final implementations.

Choice of variable names


Proper variable names and constant names can make the program more meaningful
and easier to understand. This practice tends to make the program more self
documenting. A clear definition of all variables and constants at the start of the
procedure / algorithm can also be helpful. For example, it is better to use variable day
for the day of the weeks, instead of the variable a or something else.

Documentation of the program


Brief information about the segment of the code can be included in the program to
facilitate debugging and providing information. A related part of the documentation is
o m
the information that the programmer presents to the user during the execution of the
. c
program. Since, the program is often to be used by persons who are unfamiliar with
the working and input requirements of the program, proper documentation must be
o t
provided. That is, the program must specify what responses are required from the user.
Care should also be taken to avoid ambiguities in these specifications. Also the
s p
program should “catch” incorrect responses to its requests and inform the user in an
appropriate manner.
o g
b l
1.3.3 Criteria to be followed by an Algorithm
6 .
0
The following is the criteria to be followed by an algorithm:

0


i a
Input: There should be zero or more values which are to be supplied.
Output: At least one result is to be produced.


n
Definiteness: Each step must be clear and unambiguous.
o
p o
Finiteness: If we trace the steps of an algorithm, then for all cases, the algorithm
must terminate after a finite number of steps.

l
Effectiveness: Each step must be sufficiently basic that a person using only paper

i
and pencil can in principle carry it out. In addition, not only each step is definite,

u n it must also be feasible.

sExample 1.1
Let us try to develop an algorithm to compute and display the sum of two numbers

1. Start
2. Read two numbers a and b
3. Calculate the sum of a and b and store it in sum
4. Display the value of sum
5. Stop

Example 1.2

Let us try to develop an algorithm to compute and print the average of a set of data
values.

1. Start
2. Set the sum of the data values and the count to zero.
11
An Introduction to C 3. As long as the data values exist, add the next data value to the sum and add 1 to
the count.
4. To compute the average, divide the sum by the count.
5. Display the average.
6. Stop
Example 1.3

Write an algorithm to calculate the factorial of a given number.

1. Start
2. Read the number n
3. [Initialize]
i ← 1 , fact ← 1
4. Repeat steps 4 through 6 until i = n
5. fact ← fact * i
6. i ← i + 1
7. Print fact
8. Stop

o m
Example 1.4
. c
o t
Write an algorithm to check that whether the given number is prime or not.

1. Start
s p
2. Read the number num
3. [Initialize]
o g
i ← 2 , flag ← 1
b l
5. rem ← num mod i
6 .
4. Repeat steps 4 through 6 until i < num or flag = 0

6. if rem = 0 then
flag ← 0
0 0
i a
else
i←i+1

o n
7. if flag = 0 then

p o Print Number is not prime


Else

i l Print Number is prime

u n 8. Stop

s 1.3.4 Top Down Design


Once we have defined the problem and have an idea of how to solve it, we can then
use the powerful techniques for designing algorithms. Most of the problems are
complex or large problems and to solve them we have to focus on to comprehend at
one time, a very limited span of logic or instructions. A technique for algorithm
design that tries to accommodate this human limitation is known as top-down design
or stepwise refinement.

Top down design provides the way of handling the logical complexity and detail
encountered in computer algorithm. It allows building solutions to problems in step by
step. In this way, specific and complex details of the implementation are encountered
only at the stage when sufficient groundwork on the overall structure and relationships
among the various parts of the problem.

Before the top down design can be applied to any problem, we must at least have the
outlines of a solution. Sometimes this might demand a lengthy and creative
12
investigation into the problem while at another time the problem description may in Problem Solving
itself provide the necessary starting point for the top-down design.

Top-down design suggests taking the general statements about the solution one at a
time, and then breaking them down into a more precise subtask / sub-problem. These
sub-problems should more accurately describe how the final goal can be reached. The
process of repeatedly breaking a task down into a subtask and then each subtask into
smaller subtasks must continue until the sub-problem can be implemented as the
program statement. With each spitting, it is essential to define how sub-problems
interact with each other. In this way, the overall structure of the solution to the
problem can be maintained. Preservation of the overall structure is important for
making the algorithm comprehensible and also for making it possible to prove the
correctness of the solution.

o m
. c
o t
s p
o g
b l
6 .
0 0
i a
o n
p o
Figure 1.1:
i l Schematic breakdown of a problem into subtasks as employed in

u n top down design

s1.4 ANALYSIS OF ALGORITHM EFFICENCY


Every algorithm uses some of the computer’s resources like central processing time
and internal memory to complete its task. Because of high cost of computing
resources, it is desirable to design algorithms that are economical in the use of CPU
time and memory. Efficiency considerations for algorithms are tied in with the design,
implementation and analysis of algorithm. Analysis of algorithms is less obviously
necessary, but has several purposes:

• Analysis can be more reliable than experimentation. If we experiment, we


only know the behavior of a program on certain specific test cases, while
analysis can give us guarantees about the performance on all inputs.
• It helps one choose among different solutions to problems. As we will see,
there can be many different solutions to the same problem. A careful analysis
and comparison can help us decide which one would be the best for our
purpose, without requiring that all be implemented and tested.
13
An Introduction to C • We can predict the performance of a program before we take the time to write
code. In a large project, if we waited until after all the code was written to
discover that something runs very slowly, it could be a major disaster, but if
we do the analysis first we have time to discover speed problems and work
around them.
• By analyzing an algorithm, we gain a better understanding of where the fast
and slow parts are, and what to work on or work around in order to speed it
up.
There is no simpler way of designing efficient algorithm, but a few suggestions as
shown below can sometimes be useful in designing an efficient algorithm.

1.4.1 Redundant Computations


Redundant computations or unnecessary computations result in inefficiency in the
implementation of the algorithms. When redundant calculations are embedded inside
the loop for the variable which remains unchanged throughout the entire execution
phase of the loop, the results are more serious. For example, consider the following
code in which the value a*a*a*c is redundantly calculated in the loop:
x=0;
o m
for i=0 to n
x=x+1;
. c
y=(a*a*a*c)*x*x+b*b*x;
print x,y
o t
next i
s p
o g
This redundant calculation can be removed by small modification in the program:
x=0;
b l
d=a*a*a*c;
e= b*b;
6 .
for i = 0 to n
0 0
i a
x = x+1;
y = d*x*x+e*x;

o n
next i
print x,y

o
i lp 1.4.2 Referencing Array Elements
For using the array element, we require two memory references and an additional

u n operation to locate the correct value for use. So, efficient program must not refer to
the same array element again and again if the value of the array element does not

s change. We must store the value of array element in some variable and use that
variable in place of referencing the array element. For example:
Version (1)
x=1;
for i = 0 to n
if (a[i] > a[x]) x=i;
next i
max = a[x];
Version (2)
x=1;
max=a[1];
for i = 0 to n
if(a[i]>max)
x=i;
max=a[i];
next i

Version (2) is more efficient algorithm than version (1) algorithm.


14
Problem Solving
1.4.3 Inefficiency Due to Late Termination
Another place where inefficiency can come into an implementation is where
considerably more tests are done than are required to solve the problem at hand. For
example, if in the linear search process, all the list elements are checked for a
particular element even if the point is reached where it was known that the element
cannot occur later (in case of sorted list). Second example can be in case of the bubble
sort algorithm, where the inner loop should not proceed beyond n-i, because last i
elements are already sorted (in the algorithm given below).
for i = 0 to n
for j = 0 to n – 1
if(a[j] > a[j+1])
//swap values a[j], a[j+1]

The efficient algorithm in which the inner loop terminates much before is given as:
for i=0 to n
for j=0 to n – 1
if(a[j]>a[j+1])
o m
//swap values a[j], a[j+1]
. c
1.4.4 Early Detection of Desired Output Condition
o t
Sometimes the loops can be terminated early, if the desired output conditions are met.
s p
o g
This saves a lot of unfruitful execution. For example, in the bubble sort algorithm, if
during the current pass of the inner loop there are no exchanges in the data, then the

b l
list can be assumed to be sorted and the search can be terminated before running the
outer loop for n times.

6 .
1.4.5 Trading Storage for Efficient Gains
0 0
i a
A trade between storage and efficiency is often used to improve the performance of an
algorithm. This can be done if we save some intermediary results and avoid having to
n
do a lot of unnecessary testing and computation later on.
o
p o
One strategy for speeding up the execution of an algorithm is to implement it using

i l
the least number of loops. It may make the program much harder to read and debug. It
is therefore sometimes desirable that each loop does one job and sometimes it is

u n
required for computational speedup or efficiency that the same loop must be used for
different jobs so as to reduce the number of loops in the algorithm. A kind of trade off
sis to be done while determining the approach for the same.

1.5 ANALYSIS OF ALGORITHM COMPLEXITY


Algorithms usually possess the following qualities and capabilities:

• Easily modifiable if necessary.


• They are easy, general and powerful.
• They are correct for clearly defined solution.
• Require less computer time, storage and peripherals i.e. they are more economical.
• They are documented well enough to be used by others who do not have a
detailed knowledge of the inner working.
• They are not dependable on being run on a particular computer.
• The solution is pleasing and satisfying to its designer and user.
• They are able to be used as a sub-procedure for other problems.

15
An Introduction to C Two or more algorithms can solve the same problem in different ways. So,
quantitative measures are valuable in that they provide a way of comparing the
performance of two or more algorithms that are intended to solve the same problem.
This is an important step because the use of an algorithm that is more efficient in
terms of time, resources required, can save time and money.

1.5.1 Computational Complexity


We can characterize an algorithm’s performance in terms of the size (usually n) of the
problem being solved. More computing resources are needed to solve larger problems
in the same class. The table below illustrates the comparative cost of solving the
problem for a range of n values.

Log2 n n n log2 n n2 n3 2n
1 2 2 4 8 4
3.322 10 33.22 102 103 >103
6.644 102 664.4 104 106 >>1025
9.966 103 9966.0 106 109 >>10250
13.287 104 132877 108 1012 >>102500
o m
.
The above table shows that only very small problems can be solved with an algorithm
c
o t
that exhibit exponential behaviour. An exponential problem with n=100 would take
immeasurably longer time. At the other extreme, for an algorithm with logarithmic

s p
dependency would merely take much less time (13 steps in case of log2n in the above
table). These examples emphasize the importance of the way in which algorithms
g
behave as a function of the problem size. Analysis of an algorithm also provides the
o
l
theoretical model of the inherent computational complexity of a particular problem.

b
6 .
To decide how to characterize the behaviour of an algorithm as a function of size of
the problem n, we must study the mechanism very carefully to decide just what

0 0
constitutes the dominant mechanism. It may be the number of times a particular
expression is evaluated, or the number of comparisons or exchanges that must be
a
made as n grows. For example, comparisons, exchanges, and moves count most in
i
o n
sorting algorithm. The number of comparisons usually dominates so we use
comparisons in computational model for sorting algorithms.

o
lp
1.5.2 The Order of Notation

ni
The O-notation gives an upper bound to a function within a constant factor. For a
given function g(n), we denote by O(g(n)) the set of functions.

su O(g(n)) = { f(n) : there exist positive constants c and n0, such that 0 <= f(n) <= cg(n)
for all n >= n0 }

Using O-notation, we can often describe the running time of an algorithm merely by
inspecting the algorithm’s overall structure. For example a double nested loop
structure of the following algorithm immediately yields O(n2) upper bound on the
worst case running time.
for i=0 to n
for j=0 to n
print i,j
next j
next i

What we mean by saying “the running time is O(n2)” is that the worst case running
time ( which is a function of n) is O(n2). Or equivalently, no matter what particular
input of size n is chosen for each value of n, the running time on that set of inputs is
O(n2).
16
Problem Solving
1.5.3 Rules for using the Big-O Notation
Big-O bounds, because they ignore constants, usually allow for very simple
expression for the running time bounds. Below are some properties of big-O that
allow bounds to be simplified. The most important property is that big-O gives an
upper bound only. If an algorithm is O(N2), it doesn’t have to take N2 steps (or a
constant multiple of N2). But it can’t take more than N2. So any algorithm that is
O(N), is also an O(N2) algorithm. If this seems confusing, think of big-O as being like
“<”. Any number that is < N is also <N2.

1. Ignoring constant factors: O(c f(N)) = O(f(N)), where c is a constant; e.g. O(20
N3) = O(N3)
2. Ignoring smaller terms: If a<b then O(a+b) = O(b), for example, O(N2+N) =
O(N2)
3. Upper bound only: If a<b then an O(a) algorithm is also an O(b) algorithm. For
example, an O(N) algorithm is also an O(N2) algorithm (but not vice versa).
4. N and log N are bigger than any constant, from an asymptotic view (that means
for large enough N). So if k is a constant, an O(N + k) algorithm is also O(N), by
ignoring smaller terms. Similarly, an O(log N + k) algorithm is also O(log N).
o m
5. Another consequence of the last item is that an O(N log N + N) algorithm, which
is O(N(log N + 1)), can be simplified to O(N log N).
. c
o t
1.5.4 Worst and Average Case Behavior

s p
Worst and average case behaviors of the algorithm are the two measures of

o
performance that are usually considered. These two measures can be applied to both g
b l
space and time complexity of an algorithm. The worst case complexity for a given
problem of size n corresponds to the maximum complexity encountered among all
.
problems of size n. For determination of the worst case complexity of an algorithm,
6
progress at each step towards its final goal.
0 0
we choose a set of input conditions that force the algorithm to make the least possible

i a
In many practical applications it is very important to have a measure of the expected

o n
complexity of an algorithm rather than the worst case behavior. The expected
complexity gives a measure of the behavior of the algorithm averaged over all

p o
possible problems of size n.

i l
As a simple example: Suppose we wish to characterize the behavior of an algorithm

u n
that linearly searches an ordered list of elements for some value x.
1 2 3 4 5 … … …. N
sIn the worst case, the algorithm examines all n values in the list before terminating.

In the average case, the probability that x will be found at position 1 is 1/n, at position
2 is 2/n and so on. Therefore,

Average search cost = 1/n(1+2+3+ …..+n)


= 1/n(n/2(n+1)) = (n+1)/2

Let us see how to represent the algorithm in a graphical form using a flowchart in the
following section.

1.6 FLOWCHARTS

The next step after the algorithm development is the flowcharting. Flowcharts are
used in programming to diagram the path in which information is processed through a
computer to obtain the desired results. Flowchart is a graphical representation of an 17
An Introduction to C algorithm. It makes use of symbols which are connected among them to indicate the
flow of information and processing. It will show the general outline of how to solve a
problem or perform a task. It is prepared for better understanding of the algorithm.

1.6.1 Basic Symbols used in flowchart design

Start/Stop

Question, Decision (Use in


Branching)

o m
. c
Input/Output

o t
s p
o g
Lines or arrows represent the direction of the

b l flow of control.

6 . Connector (connect one part of the

0 0 flowchart to another)

i a
o n
p o Process, Instruction

i l
u n
s Comments, Explanations,
Definitions.

Additional Symbols Related to more advanced programming

Preparation (may be used with “do Loops” )

Refers to separate flowchart

18
Example 1.5 Problem Solving

The flowchart for the Example 1.1 is shown below:

Start

Read a b

Sum = a + b

Print sum

Stop

Example 1.6
o m
. c
The flowchart for the Example 1.3 (to find factorial of a given number) is shown
below:
o t
s p
Start
o g
b l
Read n
6 .
0 0
i a
o on i=1

l p fact = 1

n i
su Is i<= n ?
No

yes

i=i+1

Print fact

fact = fact * i

Stop
19
An Introduction to C

Example 1.7:

The flowchart for Example 1.4 is shown below:

Start

Read num

i=2
flag = 1

o m
is
i<num?
no

. c
o t
yes
s p
o no g
is
flag = 0?
b l
6 .
0 0 yes

i a rem =
num mod i

o n
p o
i l is rem
no

u n != 0?

s i=i+1
yes

flag = 0

no
Print is flag
“number is = 1?
not prime”

yes

Print
“Number
stop is prime
20
Problem Solving
Check Your Progress
1. Differentiate between flowchart and algorithm.
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………

2. Compute and print the sum of a set of data values.


………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………

3. Write the following steps are suggested to facilitate the problem solving process
using computer.
o m
………………………………………………………………………………………
. c
………………………………………………………………………………………
o t
………………………………………………………………………………………
s p
………………………………………………………………………………………

o g
Ax^2 + Bx + C = 0.
b l
4. Draw an algorithm and flowchart to calculate the roots of quadratic equation

………………………………………………………………………………….…

6 .
…………………………………………………………………………………….
………………………………………………………………………………………
0 0
………………………………………………………………………………………
i a
o n
1.7 SUMMARY
p o
i l
To solve a problem different problem - solving tools are available that help in finding

u n
the solution to problem in an efficient and systematic way. Steps should be followed

sto solve the problem that includes writing the algorithm and drawing the flowchart for
the solution to the stated problem. Top down design provides the way of handling the
logical complexity and detail encountered in computer algorithm. It allows building
solutions to problems in a stepwise fashion. In this way, specific and complex details
of the implementation are encountered only at the stage when sufficient groundwork
on the overall structure and relationships among the carious parts of the problem. We
present C language - a standardized, industrial-strength programming language known
for its power and portability as an implementation vehicle for these problem solving
techniques using computer.

1.8 SOLUTIONS / ANSWERS


Check Your Progress
1. The process to devise and describe a precise plan (in the form of sequence of
operations) of what you want the computer to do, is called an algorithm. An
algorithm may be symbolized in a flowchart or pseudocode.
21
An Introduction to C 2. 1. Start
2. Set the sum of the data values and the count of the data values to zero.
3. As long as the data values exist, add the next data value to the sum
and add 1 to the count.
4. Display the average.
5. Stop

3. The following steps are suggested to facilitate the problem solving process:
a) Define the problem
b) Formulate a mathematical model
c) Develop an algorithm
d) Design the flowchart
e) Code the same using some computer language
f) Test the program

1.9 FURTHER READINGS


1. How to solve it by Computer, 5th Edition, R G Dromey, PHI, 1992.
o m
2. Introduction to Computer Algorithms, Second Edition, Thomas H. Cormen, MIT
press, 2001.
. c
o t
3. Fundamentals of Algorithmics, Gilles Brassword, Paul Bratley, PHI, 1996.
4. Fundamental Algorithms, Third Edition, Donald E Knuth, Addison-Wesley, 1997.

s p
o g
b l
6 .
0 0
i a
o n
p o
i l
u n
s

22
Basics of C
UNIT 2 BASICS OF C
Structure
2.0 Introduction
2.1 Objectives
2.2 What is a Program and what is a Programming Language?
2.3 C Language
2.3.1 History of C
2.3.2 Salient Features of C
2.4 Structure of a C Program
A simple C Program
2.5 Writing a C Program
2.6 Compiling a C Program
2.6.1 The C Compiler
2.6.2 Syntax and Semantic Errors
2.7 Link and Run the C Program
2.7.1 Run the C Program through the Menu
2.7.2
2.7.3
Run from an Executable File
Linker Errors
o m
2.8
2.7.4 Logical and Runtime Errors
Diagrammatic Representation of Program Execution Process
. c
2.9 Summary
o t
2.10
2.11
Solutions / Answers
Further Readings
s p
2.0 INTRODUCTION o g
b l
6 .
In the earlier unit we introduced you to the concepts of problem-solving, especially as
they pertain to computer programming. In this unit we present C language - a

0 0
standardized, industrial-strength programming language known for its power and

computer.
i a
portability as an implementation vehicle for these problem solving techniques using

o n
A language is a mode of communication between two people. It is necessary for those

p o
two people to understand the language in order to communicate. But even if the two
people do not understand the same language, a translator can help to convert one
l
language to the other, understood by the second person. Similar to a translator is the
i
u n
mode of communication between a user and a computer is a computer language. One
form of the computer language is understood by the user, while in the other form it is

sunderstood by the computer. A translator (or compiler) is needed to convert from


user’s form to computer’s form. Like other languages, a computer language also
follows a particular grammar known as the syntax.

In this unit we will introduce you the basics of programming language C.

2.1 OBJECTIVES
After going through this unit you will be able to:

• define what is a program?


• understand what is a C programming language?
• compile a C program;
• identify the syntax errors;
• run a C program; and
• understand what are run time and logical errors.

23
An Introduction to C
2.2 WHAT IS A PROGRAM AND WHAT IS A
PROGRAMMING LANGUAGE?
We have seen in the previous unit that a computer has to be fed with a detailed set of
instructions and data for solving a problem. Such a procedure which we call an
algorithm is a series of steps arranged in a logical sequence. Also we have seen that a
flowchart is a pictorial representation of a sequence of instructions given to the
computer. It also serves as a document explaining the procedure used to solve a
problem. In practice it is necessary to express an algorithm using a programming
language. A procedure expressed in a programming language is known as a computer
program.

Computer programming languages are developed with the primary objective of


facilitating a large number of people to use computers without the need for them to
know in detail the internal structure of the computer. Languages are designed to be
machine-independent. Most of the programming languages ideally designed, to
execute a program on any computer regardless of who manufactured it or what model
it is.

o m
Programming languages can be divided into two categories:
. c
(i) Low Level Languages or Machine Oriented Languages: The language
o t
whose design is governed by the circuitry and the structure of the machine is
p
known as the Machine language. This language is difficult to learn and use. It
s
o g
is specific to a given computer and is different for different computers i.e. these
languages are machine-dependent. These languages have been designed to

b l
give a better machine efficiency, i.e. faster program execution. Such languages
are also known as Low Level Languages. Another type of Low-Level Language

6 .
is the Assembly Language. We will code the assembly language program in the
form of mnemonics. Every machine provides a different set of mnemonics to be

0 0
used for that machine only depending upon the processor that the machine is

a
using.

i
on
(ii) High Level Languages or Problem Oriented Languages: These languages
are particularly oriented towards describing the procedures for solving the

p o problem in a concise, precise and unambiguous manner. Every high level

i l language follows a precise set of rules. They are developed to allow application
programs to be run on a variety of computers. These languages are machine-

u n independent. Languages falling in this category are FORTRAN, BASIC,


PASCAL etc. They are easy to learn and programs may be written in these

s languages with much less effort. However, the computer cannot understand
them and they need to be translated into machine language with the help of
other programs known as Compilers or Translators.

2.3 C LANGUAGE
Prior to writing C programs, it would be interesting to find out what really is C
language, how it came into existence and where does it stand with respect to other
computer languages. We will briefly outline these issues in the following section.

2.3.1 History of C
C is a programming language developed at AT&T’s Bell Laboratory of USA in 1972.
It was designed and written by Dennis Ritchie. As compared to other programming
languages such as Pascal, C allows a precise control of input and output.
24
Now let us see its historical development. The late 1960s were a turbulent era for Basics of C
computer systems research at Bell Telephone Laboratories. By 1960, many
programming languages came into existence, almost each for a specific purpose. For
example COBOL was being used for Commercial or Business Applications,
FORTRAN for Scientific Applications and so on. So, people started thinking why
could not there be a one general purpose language. Therefore, an International
Committee was set up to develop such a language, which came out with the invention
of ALGOL60. But this language never became popular because it was too abstract and
too general. To improve this, a new language called Combined Programming
Language (CPL) was developed at Cambridge University. But this language was very
complex in the sense that it had too many features and it was very difficult to learn.
Martin Richards at Cambridge University reduced the features of CPL and developed
a new language called Basic Combined Programming Language (BCPL). But
unfortunately it turned out to be much less powerful and too specific. Ken Thompson
at AT & T’s Bell Labs, developed a language called B at the same time as a further
simplification of CPL. But like BCPL this was also too specific. Ritchie inherited the
features of B and BCPL and added some features on his own and developed a
language called C. C proved to be quite compact and coherent. Ritchie first
implemented C on a DEC PDP-11 that used the UNIX Operating System.

o m
For many years the de facto standard for C was the version supplied with the UNIX
. c
version 5 operating system. The growing popularity of microcomputers led to the
creation of large number of C implementations. At the source code level most of these
o t
implementations were highly compatible. However, since no standard existed there
were discrepancies. To overcome this situation, ANSI established a committee in
s p
1983 that defined an ANSI standard for the C language.
o g
2.3.2 Salient features of C
b l
6 .
C is a general purpose, structured programming language. Among the two types of

0 0
programming languages discussed earlier, C lies in between these two categories.
That’s why it is often called a middle level language. It means that it combines the

i a
elements of high level languages with the functionality of assembly language. It
provides relatively good programming efficiency (as compared to machine oriented

o n
language) and relatively good machine efficiency as compared to high level
languages). As a middle level language, C allows the manipulation of bits, bytes and

p o
addresses – the basic elements with which the computer executes the inbuilt and

i l
memory management functions. C code is very portable, that it allows the same C
program to be run on machines with different hardware configurations. The flexibility

u n
of C allows it to be used for systems programming as well as for application
programming.
sC is commonly called a structured language because of structural similarities to
ALGOL and Pascal. The distinguishing feature of a structured language is
compartmentalization of code and data. Structured language is one that divides the
entire program into modules using top-down approach where each module executes
one job or task. It is easy for debugging, testing, and maintenance if a language is a
structured one. C supports several control structures such as while, do-while and for
and various data structures such as strucs, files, arrays etc. as would be seen in the
later units. The basic unit of a C program is a function - C’s standalone subroutine.
The structural component of C makes the programming and maintenance easier.

Check Your Progress 1


1. “A Program written in Low Level Language is faster.” Why?
………………………………………………………………………………………
………………………………………………………………………………………
……………………………………………………………………………………… 25
An Introduction to C 2. What is the difference between high level language and low level language?
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
3. Why is C referred to as middle level language?
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………

2.4 STRUCTURE OF A C PROGRAM


As we have already seen, to solve a problem there are three main things to be
considered. Firstly, what should be the output? Secondly, what should be the inputs
that will be required to produce this output and thirdly, the steps of instructions which
use these inputs to produce the required output. As stated earlier, every programming
language follows a set of rules; therefore, a program written in C also follows
o m
predefined rules known as syntax. C is a case sensitive language. All C programs

.
consist of one or more functions. One function that must be present in every C c
o t
program is main(). This is the first function called up when the program execution
begins. Basically, main() outlines what a program does. Although main is not given
p
in the keyword list,it cannot be used for naming a variable. The structure of a C
s
represent user defined functions.
o g
program is illustrated in Figure.2.1 where functions func1( ) through funcn( )

b l
6 .
Preprocessor directives

0 0
Global data declarations
main ( ) /* main function*/

i a {
Declaration part;

o n
p o }
Program statements;

i l /*User defined functions*/

u n func1( )

s {

}
…………

func2 ( )
{
…………
}
.
.
.
funcn ( )
{
…………
}

Figure. 2.1: Structure of a C Program.


26
Basics of C

A Simple C Program
From the above sections, you have become familiar with, a programming language
and structure of a C program. It’s now time to write a simple C program. This
program will illustrate how to print out the message “This is a C program”.

Example 2.1: Write a program to print a message on the screen.


/*Program to print a message*/
#include <stdio.h> /* header file*/
main() /* main function*/
{
printf("This is a C program\n"); /* output statement*/
}

Though the program is very simple, a few points must be noted.

Every C program contains a function called main(). This is the starting point of the
o m
program. This is the point from where the execution begins. It will usually call other
functions to help perform its job, some that we write and others from the standard
. c
libraries provided.

o t
#include <stdio.h> is a reference to a special file called stdio.h which contains
information that must be included in the program when it is compiled. The inclusion
s p
o
of this required information will be handled automatically by the compiler. You will g
b l
find it at the beginning of almost every C program. Basically, all the statements
starting with # in a C program are called preprocessor directives. These will be

6
some predefined functions such as, printf(), in this case. .
considered in the later units. Just remember, that this statement allows you to use

0 0
i a
main() declares the start of the function, while the two curly brackets { } shows the
start and finish of the function. Curly brackets in C are used to group statements

o n
together as a function, or in the body of a loop. Such a grouping is known as a
compound statement or a block. Every statement within a function ends with a
terminator semicolon (;).
p o
i l
printf(“This is a C program\n”); prints the words on the screen. The text to be

u n
printed is enclosed in double quotes. The \n at the end of the text tells the program to
print a newline as part of the output. That means now if we give a second printf

sstatement, it will be printed in the next line.

Comments may appear anywhere within a program, as long as they are placed within
the delimiters /* and */. Such comments are helpful in identifying the program’s
principal features or in explaining the underlying logic of various program features.
While useful for teaching, such a simple program has few practical uses. Let us
consider something rather more practical. Let us look into the example given below,
the complete program development life cycle.

Example 2.1

Develop an algorithm, flowchart and program to add two numbers.

Algorithm
1. Start
2. Input the two numbers a and b
3. Calculate the sum as a+b
4. Store the result in sum 27
An Introduction to C 5. Display the result
6. Stop.

Flowchart

START

INPUT
a, b

Sum = a + b

o m
PRINT
. c
Sum
o t
s p
o g
STOP
b l
6 .
0 0
Figure 2.2: Flow chart to add two numbers

Program
i a
o n
#include <stdio.h>

o
lp
main()
{

ni
int a,b,sum; /* variables declaration*/

su printf(“\n Enter the values for a and b: \n”);


scanf(“%d, %d”, &a, &b);

sum=a+b;

printf("\nThe sum is %d",sum); /*output statement*/


}

OUTPUT
Enter the values of a and b:
23
The sum is 5

In the above program considers two variables a and b. These variables are declared as
integers (int), it is the data type to indicate integer values. Next statement is the printf
statement meant for prompting the user to input the values of a and b. scanf is the
function to intake the values into the program provided by the user. Next comes the
processing / computing part which computes the sum. Again the printf statement is a
28
bit different from the first program; it includes a format specifier (%d). The format Basics of C
specifier indicates the kind of value to be printed. We will study about other data
types and format specifiers in detail in the following units. In the printf statement
above, sum is not printed in double quotes because we want its value to be printed.
The number of format specifiers and the variable should match in the printf statement.

At this stage, don’t go much in detail. However, in the following units you will be
learning all these details.

2.5 WRITING A C PROGRAM


A C program can be executed on platforms such as DOS, UNIX etc. DOS stores C
program with a file extension .c. Program text can be entered using any text editor
such as EDIT or any other. To edit a file called testprog.c using edit editor, gives:
C:> edit testprog.c

If you are using Turbo C, then Turbo C provides its own editor which can be used for
writing the program. Just give the full pathname of the executable file of Turbo C and
o m
you will get the editor in front of you. For example:

. c
C:> turboc\bin\tc
Here, tc.exe is stored in bin subdirectory of turboc directory. After you get the menu
o t
just type the program and store it in a file using the menu provided. The file
s p
automatically gets the extension of .c.

o g
b l
UNIX also stores C program in a file with extension is .c. This identifies it as a C
program. The easiest way to enter your text is using a text editor like vi, emacs or
xedit. To edit a file called testprog.c using vi type
6 .
$ vi testprog.c
0 0
i a
The editor is also used to make subsequent changes to the program.

o n
2.6
o
COMPILING A C PROGRAM
p
i l
After you have written the program the next step is to save the program in a file with
extension . c . This program is in high-level language. But this language is not
n
understood by the computer. So, the next step is to convert the high-level language
u
sprogram (source code) to machine language (object code). This task is performed by a
software or program known as a compiler. Every language has its own compiler that
converts the source code to object code. The compiler will compile the program
successfully if the program is syntactically correct; else the object code will not be
produced. This is explained pictorially in Figure 2.3.

High Level Machine


Translator
Language Language
(Compiler)
Program Program

Source Object
Code Code
Figure 2.3: Process of Translation

29
An Introduction to C 2.6.1 The C Compiler
If you are working on UNIX platform, then if the name of the program file is
testprog.c, to compile it, the simplest method is to type

cc testprog.c

This will compile testprog.c, and, if successful, will produce a executable file called
a.out. If you want to give the executable file any other, you can type

cc testprog.c -o testprog

This will compile testprog.c, creating an executable file testprog.

If you are working with TurboC on DOS platform then the option for compilation is
provided on the menu. If the program is syntactically correct then this will produce a
file named as testprog.obj. If not, then the syntax errors will be displayed on the
screen and the object file will not be produced. The errors need to be removed before
compiling the program again. This process of removing the errors from the program is
called as the debugging.
o m
2.6.2 Syntax and Semantic Errors
. c
o t
Every language has an associated grammar, and the program written in that language

s p
has to follow the rules of that grammar. For example in English a sentence such a

o g
“Shyam, is playing, with a ball”. This sentence is syntactically incorrect because
commas should not come the way they are in the sentence.

b l
Likewise, C also follows certain syntax rules. When a C program is compiled, the

6 .
compiler will check that the program is syntactically correct. If there are any syntax
errors in the program, those will be displayed on the screen with the corresponding
line numbers.
0 0
a
Let us consider the following program.
i
o n
Example 2.3: Write a program to print a message on the screen.

o
lp
/* Program to print a message on the screen*/

ni #include <stdio.h

su main( )
{
printf(“Hello, how are you\n”)

Let the name of the program be test.c .If we compile the above program as it is we
will get the following errors:
Error test.c 1:No file name ending
Error test.c 5: Statement missing ;
Error test.c 6: Compound statement missing }

Edit the program again, correct the errors mentioned and the corrected version appears
as follows:
#include <stdio.h>
main( )
{
printf (“Hello, how are you\n”);
30 }
Apart from syntax errors, another type of errors that are shown while compilation are Basics of C
semantic errors. These errors are displayed as warnings. These errors are shown if a
particular statement has no meaning. The program does compile with these errors, but
it is always advised to correct them also, since they may create problems while
execution. The example of such an error is that say you have declared a variable but
have not used it, and then you get a warning “code has no effect”. These variables are
unnecessarily occupying the memory.

Check Your Progress 2


1. What is the basic unit of a C program?
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
2. “The program is syntactically correct”. What does it mean?
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
o m
3. Indicate the syntax errors in the following program code:
. c
include <stdio.h>
o t
s p
main( )
[
o g
]
printf(“hello\n”);
b l
……………………………………………………………………………………
6 .
……………………………………………………………………………………
0 0
……………………………………………………………………………………

i a
2.7
o n
LINK AND RUN THE C PROGRAM

p o
i l
After compilation, the next step is linking the program. Compilation produces a file
with an extension .obj. Now this .obj file cannot be executed since it contains calls to

u n
functions defined in the standard library (header files) of C language. These functions
have to be linked with the code you wrote. C comes with a standard library that
sprovides functions that perform most commonly needed tasks. When you call a
function that is not the part of the program you wrote, C remembers its name. Later
the linker combines the code you wrote with the object code already found in the
standard library. This process is called linking. In other words, Linker is a program
that links separately compiled functions together into one program. It combines the
functions in the standard C library with the code that you wrote. The output of the
linker in an executable program i.e., a file with an extension .exe.

2.7.1 Run the C Program Through the Menu


When we are working with TurboC in DOS environment, the menu in the GUI that
pops up when we execute the executable file of TurboC contains several options for
executing the program:
i) Link , after compiling
ii) Make, compiles as well as links
iii) Run
31
An Introduction to C All these options create an executable file and when these options are used we also get
the output on user screen. To see the output we have to shift to user screen window.

2.7.2 Run From an Executable File


An .exe file produced by can be directly executed.

UNIX also includes a very useful program called make. Make allows very
complicated programs to be compiled quickly, by reference to a configuration file
(usually called makefile). If your C program is a single file, you can usually use make
by simply typing –

make testprog

This will compile testprog.c as well as link your program with the standard library so
that you can use the standard library functions such as printf and put the executable
code in testprog.

In case of DOS environment , the options provided above produce an executable file
and this file can be directly executed from the DOS prompt just by typing its name
o m
. c
without the extension. That is if the name of the program is test.c, after compiling and
linking the new file produced is test.exe only if compilation and linking is successful.

o t
This can be executed as:

s p
c>test

o g
2.7.3 Linker Errors

b l
6 .
If a program contains syntax errors then the program does not compile, but it may
happen that the program compiles successfully but we are unable to get the executable

0 0
file, this happens when there are certain linker errors in the program. For example, the
object code of certain standard library function is not present in the standard C library;
a
the definition for this function is present in the header file that is why we do not get a
i
o n
compiler error. Such kinds of errors are called linker errors. The executable file would
be created successfully only if these linker errors are corrected.

o
lp
2.7.4 Logical and Runtime Errors

ni
After the program is compiled and linked successfully we execute the program. Now
there are three possibilities:

su 1)
2)
3)
The program executes and we get correct results,
The program executes and we get wrong results, and
The program does not execute completely and aborts in between.

The first case simply means that the program is correct. In the second case, we get
wrong results; it means that there is some logical mistake in our program. This kind of
error is known as logical error. This error is the most difficult to correct. This error is
corrected by debugging. Debugging is the process of removing the errors from the
program. This means manually checking the program step by step and verifying the
results at each step. Debugging can be made easier by a tracer provided in Turbo C
environment. Suppose we have to find the average of three numbers and we write the
following code:

Example 2.4: Write a C program to compute the average of three numbers

/* Program to compute average of three numbers *?


#include<stdio.h>
32
main( ) Basics of C
{
int a,b,c,sum,avg;

a=10;
b=5;
c=20;

sum = a+b+c;
avg = sum / 3;
printf(“The average is %d\n”, avg);
}

OUTPUT

The average is 8.

The exact value of average is 8.33 and the output we got is 8. So we are not getting
the actual result, but a rounded off result. This is due to the logical error. We have
declared variable avg as an integer but the average calculated is a real number,
o m
therefore only the integer part is stored in avg. Such kinds of errors which are not
. c
detected by the compiler or the linker are known as logical errors.

o t
The third kind of error is only detected during execution. Such errors are known as
run time errors. These errors do not produce the result at all, the program execution
s p
stops in between and the run time error message is flashed on the screen. Let us look
at the following example:
o g
b l
difference
6 .
Example 2.5: Write a program to divide a sum of two numbers by their

0 0
/* Program to divide a sum of two numbers by their difference*/

#include <stdio.h>
i a
o n
main( )
{
p o
int a,b;
i l
u nfloat c;

s a=10;
b=10;

c = (a+b) / (a-b);
printf(“The value of the result is %f\n”,c);
}

The above program will compile and link successfully, it will execute till the first
printf statement and we will get the message in this statement, as soon as the next
statement is executed we get a runtime error of “Divide by zero” and the program
halts. Such kinds of errors are runtime errors.

2.8 DIAGRAMMATIC REPRESENTATION OF


PROGRAM EXECUTION PROCESS
The following figure 2.4 shows the diagrammatic representation of the program
execution process. 33
An Introduction to C
WRITE A C PROGRAM

COMPILE THE PROGRAM

DEBUG SYNTAX
ERRORS (IF ANY),
SAVE AND RECOMPILE

LINK THE PROGRAM

o m
. c
EXECUTE THE PROGRAM
o t
s p
o g
TEST AND VERIFY THE
b l
RESULTS
6 .
0 0
Figure 2.4: Program Execution Process

i a
Check Your Progress 3

o
1.n What is the extension of an executable file?

p o ……………………………………………………………………………………

i l ……………………………………………………………………………………

u n ……………………………………………………………………………………

s 2. What is the need for linking a compiled file?


……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
3. How do you correct the logical errors in the program?
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………

2.9 SUMMARY
In this unit, you have learnt about a program and a programming language. You can
now differentiate between high level and low level languages. You can now define
what is C, features of C. You have studied the emergence of C. You have seen how C
34
is different, being a middle level Language, than other High Level languages. The Basics of C
advantage of high level language over low level language is discussed.

You have seen how you can convert an algorithm and flowchart into a C program. We
have discussed the process of writing and storing a C program in a file in case of
UNIX as well as DOS environment.

You have learnt about compiling and running a C program in UNIX as well as on
DOS environment. We have also discussed about the different types of errors that are
encountered during the whole process, i.e. syntax errors, semantic errors, logical
errors, linker errors and runtime errors. You have also learnt how to remove these
errors. You can now write simple C programs involving simple arithmetic operators
and the printf( ) statement. With these basics, now we are ready to learn the C
language in detail in the following units.

2.10 SOLUTIONS / ANSWERS


Check Your Progress 1

o m
1. A program written in Low Level Language is faster to execute since it needs no
conversion while a high level language program need to be converted into low
. c
level language.

o t
2. Low level languages express algorithms on the form of numeric or mnemonic
codes while High Level Languages express algorithms in the using concise,
s p
o
precise and unambiguous notation. Low level languages are machine dependent g
b l
while High level languages are machine independent. Low level languages are
difficult to program and to learn, while High level languages are easy to program

6 .
and learn. Examples of High level languages are FORTRAN, Pascal and
examples of Low level languages are machine language and assembly language.

0 0
3.
a
C is referred to as middle level language as with C we are able to manipulate

i
bits, bytes and addresses i.e. interact with the hardware directly. We are also able

n
to carry out memory management functions.

o
Check Your Progress 2
p o
l
1. The basic unit of a C program is a C function.
i
u n
2. It means that program contains no grammatical or syntax errors.

s3. Syntax errors:


a) # not present with include
b) {brackets should be present instead of [ brackets.

Check Your Progress 3

1. The extension of an executable file is .exe.

2. The C program contains many C pre-defined functions present in the C library.


These functions need to be linked with the C program for execution; else the C
program may give a linker error indicating that the function is not present.

3. Logical errors can be corrected through debugging or self checking.

35
An Introduction to C

2.11 FURTHER READINGS


1. The C Programming Language, Kernighan & Richie, PHI Publication.
2. Programming with C, Second Edition, Byron Gottfried, Tata Mc Graw Hill,
2003.
3. The C Complete Reference, Fourth Editon, Herbert Schildt, Tata Mc Graw Hill,
2002.
4. Programming with ANSI and Turbo C, Ashok N. Kamthane, Pearson Education
Asia, 2002.
5. Computer Science A structured programming approach using C Second Edition,
Behrouza A. Forouzan, Richard F. Gilberg, Brooks/Cole, Thomson Learning,
2001.

o m
. c
o t
s p
o g
b l
6 .
0 0
i a
o n
p o
i l
u n
s

36
Variables and
UNIT 3 VARIABLES AND CONSTANTS Constants

Structure
3.0 Introduction
3.1 Objectives
3.2 Character Set
3.3 Identifiers and Keywords
3.3.1 Rules for Forming Identifiers
3.3.2 Keywords
3.4 Data Types and Storage
3.5 Data Type Qualifiers
3.6 Variables
3.7 Declaring Variables
3.8 Initialising Variables
3.9 Constants
3.9.1 Integer Constants
3.9.2 Floating Point Constants
3.9.3
3.9.4
Character Constants
String Constants
o m
3.10
3.11
Symbolic Constants
Summary
. c
3.12 Solutions / Answers
o t
3.13 Further Readings

s p
3.0 INTRODUCTION
o g
b l
As every natural language has a basic character set, computer languages also have a

are used to write the programs.


6 .
character set, rules to define words. Words are used to form statements. These in turn

0 0
Computer programs usually work with different types of data and need a way to store

i a
the values being used. These values can be numbers or characters. C language has two

o n
ways of storing number values—variables and constants—with many options for
each. Constants and variables are the fundamental elements of each program. Simply

p o
speaking, a program is nothing else than defining them and manipulating them. A
variable is a data storage location that has a value that can change during program
l
execution. In contrast, a constant has a fixed value that can’t change.
i
u n
This unit is concerned with the basic elements used to construct simple C program
statements. These elements include the C character set, identifiers and keywords, data
stypes, constants, variables and arrays, declaration and naming conventions of
variables.

3.1 OBJECTIVES
After going through this unit, you will be able to:
• define identifiers, data types and keywords in C;
• know name the identifiers as per the conventions;
• describe memory requirements for different types of variables; and
• define constants, symbolic constants and their use in programs.

3.2 CHARACTER SET

When you write a program, you express C source files as text lines containing
characters from the character set. When a program executes in the target environment,
37
An Introduction to C it uses characters from the character set. These character sets are related, but need not
have the same encoding or all the same members.

Every character set contains a distinct code value for each character in the basic C
character set. A character set can also contain additional characters with other code
values. The C language character set has alphabets, numbers, and special characters as
shown below:
1. Alphabets including both lowercase and uppercase alphabets - A-Z and a-z.

2. Numbers 0-9

3. Special characters include:

; : { , ‘ “ |
} > < / \ ~ _
[ ] ! $ ? * +
= ( ) - % # ^
@ & .

o m
. c
3.3 IDENTIFIERS AND KEYWORDS
o t
s p
Identifiers are the names given to various program elements such as constants,
variables, function names and arrays etc. Every element in the program has its own

o g
distinct name but one cannot select any name unless it conforms to valid name in C

l
language. Let us study first the rules to define names or identifiers.

b
3.3.1
.
Rules for Forming Identifiers
6
0
Identifiers are defined according to the following rules:
0
i a
1. It consists of letters and digits.
2. First character must be an alphabet or underscore.
n
3. Both upper and lower cases are allowed. Same text of different case is not
o
p o equivalent, for example: TEXT is not same as text.
4. Except the special character underscore ( _ ), no other special symbols can be

i l used.

sun For example, some valid identifiers are shown below:


X
X123
_XI
temp
tax_rate

For example, some invalid identifiers are shown below:


123 First character to be alphabet.
“X.” Not allowed.
order-no Hyphen allowed.
error flag Blankspace allowed.

3.3.2 Keywords
Keywords are reserved words which have standard, predefined meaning in C. They
cannot be used as program-defined identifiers.

38
The lists of C keywords are as follows: Variables and
Constants
char while do typedef auto
int if else switch case
printf double struct break static
long enum register extern return
union const float short unsigned
continue for signed void default
goto sizeof volatile

Note: Generally all keywords are in lower case although uppercase of same names
can be used as identifiers.

3.4 DATA TYPES AND STORAGE


To store data inside the computer we need to first identify the type of data elements
we need in our program. There are several different types of data, which may be
represented differently within the computer memory. The data type specifies two
things:
o m
1.
2.
Permissible range of values that it can store.
Memory requirement to store a data type.
. c
o t
C Language provides four basic data types viz. int, char, float and double. Using
these, we can store data in simple ways as single elements or we can group them
s p
together and use different ways (to be discussed later) to store them as per
o
requirement. The four basic data types are described in the following table 3.1: g
b l
Table 3.1: Basic Data Types

6 .
DATA TYPE
int
TYPE OF DATA
Integer
0
MEMORY
2 Bytes 0 RANGE
− 32,768 to 32,767
char character
i a 1 Byte − 128 to 128
3.4e − 38 to 3.4e
float

o n
Floating point number 4 bytes
+38
double
p o
Floating point number
with higher precision
8 bytes 1.7e − 308 to 1.7e
+ 308

i l
u n
Memory requirements or size of data associated with a data type indicates the range of
numbers that can be stored in the data item of that type.

s3.5 DATA TYPE QUALIFIERS


Short, long, signed, unsigned are called the data type qualifiers and can be used with
any data type. A short int requires less space than int and long int may require more
space than int. If int and short int takes 2 bytes, then long int takes 4 bytes.

Unsigned bits use all bits for magnitude; therefore, this type of number can be larger.
For example signed int ranges from –32768 to +32767 and unsigned int ranges from
0 to 65,535. Similarly, char data type of data is used to store a character. It requires 1
byte. Signed char values range from –128 to 127 and unsigned char value range from
0 to 255. These can be summarized as follows:
Data type Size (bytes) Range

Short int or int 2 −32768 to 32,767

Long int 4 −2147483648 to 2147483647


39
An Introduction to C
Signed int 2 −32768 to 32767

Unsigned int 2 0 to 65535

Signed char 1 −128 to 127

Unsigned char 1 0 to 255

3.6 VARIABLES

Variable is an identifier whose value changes from time to time during execution. It is
a named data storage location in your computer’s memory. By using a variable’s
name in your program, you are, in effect, referring to the data stored there. A variable
represents a single data item i.e. a numeric quantity or a character constant or a string
constant. Note that a value must be assigned to the variables at some point of time in
the program which is termed as assignment statement. The variable can then be

it may give garbage value. The data type of a variable doesn’t change whereas the
o m
accessed later in the program. If the variable is accessed before it is assigned a value,

value assigned to can change. All variables have three essential attributes:
. c
• the name
o t


the value
the memory, where the value is stored.
s p
o g
For example, in the following C program a, b, c, d are the variables but variable e is

b l
not declared and is used before declaration. After compiling the source code and look
what gives?

6 .
{
main( )
0 0
a
int a, b, c;
i
o nchar d;
a = 3;

p o b = 5;
c = a + b;

i l d = ‘a’;
e=d;

u n ……….

s ……….
}
After compiling the code, this will generate the message that variable e not defined.

3.7 DECLARING VARIABLES


Before any data can be stored in the memory, we must assign a name to these
locations of memory. For this we make declarations. Declaration associates a group of
identifiers with a specific data type. All of them need to be declared before they
appear in program statements, else accessing the variables results in junk values or a
diagnostic error. The syntax for declaring variables is as follows:
data- type variable-name(s);

For example,
int a;
short int a, b;
40
int c, d; Variables and
Constants
long c, f;
float r1, r2;

3.8 INITIALISING VARIABLES


When variables are declared initial, values can be assigned to them in two ways:
a) Within a Type declaration
The value is assigned at the declaration time.
For example,

int a = 10;
float b = 0.4 e –5;
char c = ‘a’;

b) Using Assignment statement


o m
The values are assigned just after the declarations are made.
. c
For example,
o t
a = 10;
s p
b = 0.4 e –5;
c = ‘a’;
o g
b l
Check Your Progress 1

6 .
1)
0
Identify keywords and valid identifiers among the following:

0
hello
student_1
function
max_value
i a
day-of-the-week
“what”
1_student int

o n union

p o
………………………………………………………………………………………

i l
………………………………………………………………………………………

u n ………………………………………………………………………………………

s2) Declare type variables for roll no, total_marks and percentage.

………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………

3) How many bytes are assigned to store for the following?

a) Unsigned character b) Unsigned integer c) Double

………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
41
An Introduction to C

3.9 CONSTANTS
A constant is an identifier whose value can not be changed throughout the execution
of a program whereas the variable value keeps on changing. In C there are four basic
types of constants. They are:
1. Integer constants
2. Floating point constants
3. Character constants
4. String constants

Integer and Floating Point constants are numeric constants and represent numbers.

Rules to form Integer and Floating Point Constants

• No comma or blankspace is allowed in a constant.




It can be preceded by – (minus) sign if desired.
The value should lie within a minimum and maximum permissible range decided
o m
by the word size of the computer.
. c
3.9.1 Integer Constants
o t
s p
Further, these constant can be classified according to the base of the numbers as:
1. Decimal integer constants
o g
b l
These consist of digits 0 through 9 and first digit should not be 0.

For example,
6 .
0 0
1

i a 443 32767
are valid decimal integer constants.

o
2.n Invalid Decimal integer Constants

o
lp
12 ,45 , not allowed
36.0 Illegal char.

n i 1 010 Blankspace not allowed

su 10 – 10
0900
Illegal char –
The first digit should not be a zero

3. Octal integer constants


These consist of digits 0 through 7. The first digit must be zero in order to
identify the constant as an octal number.

Valid Octal INTEGER constants are:


0 01 0743 0777

Invalid Octal integer constants are:


743 does not begin with 0
0438 illegal character 8
0777.77 illegal char .

4. Hexadecimal integer constants


42
These constants begin with 0x or OX and are followed by combination of digits Variables and
Constants
taken from hexadecimal digits 0 to 9, a to f or A to F.

Valid Hexadecimal integer constants are:


OX0 OX1 OXF77 Oxabcd.

Invalid Hexadecimal integer constants are:


OBEF x is not included
Ox.4bff illegal char (.)
OXGBC illegal char G

Maximum values these constants can have are as follows:


Integer constants Maximum value

Decimal integer 32767


Octal integer 77777
Hexadecimal integer 7FFF
o m
Unsigned interger constants: Exceed the ordinary integer by magnitude of 2,
. c
they are not negative. A character U or u is prefixed to number to make it
unsigned.
o t
Long Integer constants: These are used to exceed the magnitude of ordinary
s p
integers and are appended by L.
o g
For example,
b l
50000U decimal unsigned.
6 .
1234567889L
0123456L
decimal long.
otal long.
0 0
0777777U otal unsigned.
i a
n
3.9.2 Floating Point Constants
o
o
What is a base 10 number containing decimal point or an exponent.
p
i l
Examples of valid floating point numbers are:

u n
0. 1.

s 000.2
50000.1
5.61123456
0.000741
1.6667E+3 0.006e-3

Examples of Invalid Floating Point numbers are:


1 decimal or exponent required.
1,00.0 comma not allowed.
2E+10.2 exponent is written after integer quantity.
3E 10 no blank space.

A Floating Point number taking the value of 5 x 104 can be represented as:

5000. 5e4
5e+4 5E4
5.0e+4 .5e5

43
An Introduction to C The magnitude of floating point numbers range from 3.4E –38 to a maximum of
3.4E+38, through 0.0. They are taken as double precision numbers. Floating Point
constants occupy 2 words = 8 bytes.

3.9.3 Character Constants


This constant is a single character enclosed in apostrophes ‘ ’ .
For example, some of the character constants are shown below:
‘A’, ‘x’, ‘3’, ‘$’

‘\0’ is a null character having value zero.

Character constants have integer values associated depending on the character set
adopted for the computer. ASCII character set is in use which uses 7-bit code with 27
= 128 different characters. The digits 0-9 are having ASCII value of 48-56 and ‘A’
have ASCII value from 65 and ‘a’ having value 97 are sequentially ordered. For
example,

‘A’ has 65, blank has 32


o m
. c
ESCAPE SEQUENCE

o t
There are some non-printable characters that can be printed by preceding them with ‘\’

s p
backslash character. Within character constants and string literals, you can write a

o g
variety of escape sequences. Each escape sequence determines the code value for a
single character. You can use escape sequences to represent character codes:


l
you cannot otherwise write (such as \n)
b

6 .
that can be difficult to read properly (such as \t)
that might change value in different target character sets (such as \a)

0
that must not change in value among different target environments (such as \0)

0
a
The following is the list of the escape sequences:
i
o on
Character
"
Escape Sequence
\"

l p '
?
\'
\?

n i \
BEL
\\
\a

su BS
FF
NL
\b
\f
\n
CR \r
HT \t
VT \v

3.9.4 String Constants


It consists of sequence of characters enclosed within double quotes. For example,
“ red ” “ Blue Sea ” “ 41213*(I+3) ”.

3.10 SYMBOLIC CONSTANTS


Symbolic Constant is a name that substitutes for a sequence of characters or a numeric
constant, a character constant or a string constant. When program is compiled each
occurrence of a symbolic constant is replaced by its corresponding character
44 sequence. The syntax is as follows:
#define name text Variables and
Constants

where name implies symbolic name in caps.


text implies value or the text.

For example,

#define printf print


#define MAX 100
#define TRUE 1
#define FALSE 0
#define SIZE 10

The # character is used for preprocessor commands. A preprocessor is a system


program, which comes into action prior to Compiler, and it replaces the replacement
text by the actual text. This will allow correct use of the statement printf.

Advantages of using Symbolic Constants are:

• They can be used to assign names to values


o m
• Replacement of value has to be done at one place and wherever the name
. c
appears in the text it gets the value by execution of the preprocessor. This
o t
saves time. if the Symbolic Constant appears 20 times in the program; it needs
to be changed at one place only.
s p
Check Your Progress 2
o g
1)
b l
Write a preprocessor directive statement to define a constant PI having the value
3.14.
………………………………………………………………………………………
6 .
………………………………………………………………………………………
0 0
i a
2)
n
Classify the examples into Interger, Character and String constants.
o
‘A’
077.7
0147
p
“A”
0xEFH
26.4 o
“EFH”
i l‘\r’ abc

u n ………………………………………………………………………………………
………………………………………………………………………………………
s3) Name different categories of Constants.
………………………………………………………………………………………
………………………………………………………………………………………

3.11 SUMMARY
To summarize we have learnt certain basics, which are required to learn a computer
language and form a basis for all languages. Character set includes alphabets, numeric
characters, special characters and some graphical characters. These are used to form
words in C language or names or identifiers. Variable are the identifiers, which
change their values during execution of the program. Keywords are names with
specific meaning and cannot be used otherwise.

We had discussed four basic data types - int, char, float and double. Some qualifiers
are used as prefixes to data types like signed, unsigned, short, and long.
45
An Introduction to C
The constants are the fixed values and may be either Integer or Floating point or
Character or String type. Symbolic Constants are used to define names used for
constant values. They help in using the name rather bothering with remembering and
writing the values.

3.12 SOLUTIONS / ANSWERS


Check Your Progress 1
1. Keywords: int, union
Valid Identifiers: hello, student_1, max_value

2. int rollno;
float total_marks, percentage;

3. a) 1 byte b) 2 bytes c) 8 bytes

Check Your Progress 2


o m
1. # define PI 3.14
. c
2. Integer constant: 0147
o t
Character constants:
String constants:
’A’, ‘\r’
”A”, “EFH”
s p
o g
3.13 FURTHER READINGS
b l
1.
6 .
The C Programming Language, Kernighan & Ritchie, PHI Publication.
2.
0 0
Computer Science A structured programming approach using C, Behrouza A.

i a
Forouzan, Richard F. Gilberg, Second Edition, Brooks/Cole, Thomson Learning,
2001.
3.

o n Programming with C, Gottfried, Second Edition, Schaum Outlines, Tata Mc


Graw Hill, 2003.

p o
i l
u n
s

46
Expressions and
UNIT 4 EXPRESSIONS AND OPERATORS Operators

Structure
4.0 Introduction
4.1 Objectives
4.2 Assignment Statements
4.3 Arithmetic Operators
4.4 Relational Operators
4.5 Logical Operators
4.6 Comma and Conditional Operators
4.7 Type Cast Operator
4.8 Size of Operator
4.9 C Shorthand
4.10 Priority of Operators
4.11 Summary
4.12 Solutions / Answers
4.13 Further Readings

o m
4.0 INTRODUCTION
. c
In the previous unit we have learnt variables, constants, datatypes and how to declare
o t
them in C programming. The next step is to use those variables in expressions. For
writing an expression we need operators along with variables. An expression is a
s p
sequence of operators and operands that does one or a combination of the following:
• specifies the computation of a value
o g
• designates an object or function
b l
• generates side effects.

6 .
An operator performs an operation (evaluation) on one or more operands. An operand
is a subexpression on which an operator acts.
0 0
i a
This unit focuses on different types of operators available in C including the syntax

n
and use of each operator and how they are used in C.

o
p o
A computer is different from calculator in a sense that it can solve logical expressions
also. Therefore, apart from arithmetic operators, C also contains logical operators.
l
Hence, logical expressions are also discussed in this unit.
i
u
4.1n OBJECTIVES
sAfter going through this unit you will be able to:

• write and evaluate arithmetic expressions;


• express and evaluate relational expressions;
• write and evaluate logical expressions;
• write and solve compute complex expressions (containing arithmetic, relational
and logical operators), and
• check simple conditions using conditional operators.

4.2 ASSIGNMENT STATEMENT


In the previous unit, we have seen that variables are basically memory locations and
they can hold certain values. But, how to assign values to the variables? C provides an
assignment operator for this purpose. The function of this operator is to assign the
values or values in variables on right hand side of an expression to variables on the
left hand side.
47
An Introduction to C The syntax of the assignment expression is as follows:
Variable = constant / variable/ expression;

The data type of the variable on left hand side should match the data type of
constant/variable/expression on right hand side with a few exceptions where
automatic type conversions are possible. Some examples of assignment statements are
as follows:
b =a; /* b is assigned the value of a */
b=5; /* b is assigned the value 5*/
b = a+5; /* b is assigned the value of expr a+5 */

The expression on the right hand side of the assignment statement can be:

• an arithmetic expression;
• a relational expression;
• a logical expression;
• a mixed expression.

o m
The above mentioned expressions are different in terms of the type of operators

.
connecting the variables and constants on the right hand side of the variable. c
following sections.
o t
Arithmetic operators, relational operators and logical operators are discussed in the

For example,
s p
int a;
o g
float b,c ,avg, t;
avg = (b+c) / 2;
b l
/*arithmetic expression */
a = b && c;
6
a = (b+c) && (b<c); . /*logical expression*/
/* mixed expression*/

0 0
4.3
i a
ARITHMETIC OPERATORS

o n
The basic arithmetic operators in C are the same as in most other computer languages,

p o and correspond to our usual mathematical/algebraic symbolism. The following


arithmetic operators are present in C:

i l
un
Operator Meaning

s +

-
Addition

Subtraction

* Multiplication

/ Division

% Modular Division

Some of the examples of algebraic expressions and their C notation are given below:

Expression C notation
b* g (b *g) / d
d

48 a3+cd (a*a*a) + (c*d)


Expressions and
Operators
The arithmetic operators are all binary operators i.e. all the operators have two
operands. The integer division yields the integer result. For example, the expression
10/3 evaluates to 3 and the expression 15/4 evaluates to 3. C provides the modulus
operator, %, which yields the reminder after integer division. The modulus operator is
an integer operator that can be used only with integer operands. The expression x%y
yields the reminder after x is divided by y. Therefore, 10%3 yields 1 and 15%4 yields
3. An attempt to divide by zero is undefined on computer system and generally results
in a run- time error. Normally, Arithmetic expressions in C are written in straight-line
form. Thus ‘a divided by b’ is written as a/b.

The operands in arithmetic expressions can be of integer, float, double type. In order
to effectively develop C programs, it will be necessary for you to understand the rules
that are used for implicit conversation of floating point and integer values in C.

They are mentioned below:

• An arithmetic operator between an integer and integer always yields an integer


result.
Operator between float and float yields a float result.
o m
• Operator between integer and float yields a float result.
. c
If the data type is double instead of float, then we get a result of double data type.
o t
For example,
s p
Operation Result
o g
5/3 1
b l
6 .
5.0/3

0 0 1.3

5/3.0

i a 1.3

o
5.0/3.0
on 1.3

For example,
l p
Parentheses can be used in C expression in the same manner as algebraic expression

n i a * (b + c).

su
It may so happen that the type of the expression and the type of the variable on the left
hand side of the assignment operator may not be same. In such a case the value for the
expression is promoted or demoted depending on the type of the variable on left hand
side of = (assignment operator). For example, consider the following assignment
statements:
int i;
float b;
i = 4.6;
b = 20;

In the first assignment statement, float (4.6) is demoted to int. Hence i gets the value
4. In the second statement int (20) is promoted to float, b gets 20.0. If we have a
complex expression like:
float a, b, c;
int s;
s = a * b / 5.0 * c;
49
An Introduction to C
Where some operands are integers and some are float, then int will be promoted or
demoted depending on left hand side operator. In this case, demotion will take place
since s is an integer.

The rules of arithmetic precedence are as follows:


1. Parentheses are at the “highest level of precedence”. In case of nested parenthesis,
the innermost parentheses are evaluated first.

For example,

( ((3+4)*5)/6 )

The order of evaluation is given below.

( ( (3+4) * 5) / 6 )

o m
1 2 3

. c
t
2. Multiplication, Division and Modulus operators are evaluated next. If an
o
s p
expression contains several multiplication, division and modulus operators,
evaluation proceeds from left to right. These three are at the same level of
precedence.

o g
For example,
b l
5*5+6*7
6 .
0 0
The order of evaluation is given below.

i a 5*5+6*7

o n
p o 1 2

i l 3

un
3. Addition, subtraction are evaluated last. If an expression contains several
addition and subtraction operators, evaluation proceeds from left to right. Or the
s associativity is from left to right.

For example,

8/5-6+5/2

The order of evaluation is given below.

8/5-6+5/2

1 3 4 2

50
Apart from these binary arithmetic operators, C also contains two unary operators Expressions and
Operators
referred to as increment (++) and decrement (--) operators, which we are going to be
discussed below:
The two-unary arithmetic operators provided by C are:

• Increment operator (++)


• Decrement operator (- -)

The increment operator increments the variable by one and decrement operator
decrements the variable by one. These operators can be written in two forms i.e.
before a variable or after a variable. If an increment / decrement operator is written
before a variable, it is referred to as preincrement / predecrement operators and if it is
written after a variable, it is referred to as post increment / postdecrement operator.
For example,
a++ or ++a is equivalent to a = a+1 and
a-- or - -a is equivalent to a = a -1

The importance of pre and post operator occurs while they are used in the expressions.
o m
Preincrementing (Predecrementing) a variable causes the variable to be incremented
(decremented) by 1, then the new value of the variable is used in the expression in
. c
which it appears. Postincrementing (postdecrementing) the variable causes the
current value of the variable is used in the expression in which it appears, then the
o t
variable value is incremented (decrement) by 1.
s p
The explanation is given in the table below:
o g
Expression Explanation
b l
++a
6 .
Increment a by 1, then use the new value of a

a++
0 0
Use value of a, then increment a by 1

i a
--b

o n Decrement b by 1, then use the new value of b

b--

p o Use the current value of b, then decrement by 1

examples:
i l
The precedence of these operators is right to left. Let us consider the following

u n
sint a = 2, b=3;
int c;
c = ++a – b- -;
printf (“a=%d, b=%d,c=%d\n”,a,b,c);

OUTPUT

a = 3, b = 2, c = 0.

Since the precedence of the operators is right to left, first b is evaluated, since it is a
post decrement operator, current value of b will be used in the expression i.e. 3 and
then b will be decremented by 1.Then, a preincrement operator is used with a, so first
a is incremented to 3. Therefore, the value of the expression is evaluated to 0.

Let us take another example,

int a = 1, b = 2, c = 3;
int k; 51
An Introduction to C k = (a++)*(++b) + ++a - --c;
printf(“a=%d,b=%d, c=%d, k=%d”,a,b,c,k);

OUTPUT

a = 3, b = 3, c = 2, k = 6

The evaluation is explained below:


k = (a++) * (++b)+ ++a - --c
= (a++) * (3) + 2 - 2 step1
= (2) * (3) + 2 - 2 step2
= 6 final result

Check Your Progress 1


1. Give the C expressions for the following algebraic expressions:

a*4c2 - d
i)
m+n
o m
ii)ab - (e+f)4
. c
c
………………………………………………………………………………………
o t
………………………………………………………………………………………
s p
2.
o
Give the output of the following C code: g
main()
b l
{
int a=2,b=3,c=4;
6 .
0
k = ++b + --a*c + a;
0
}
i a
printf(“a= %d b=%d c=%d k=%d\n”,a,b,c,k);

o n ………………………………………………………………………………………

p o ………………………………………………………………………………………

i l 3. Point out the error:

u n Exp = a**b;
………………………………………………………………………………………

s ………………………………………………………………………………………

4.4 RELATIONAL OPERATORS

Executable C statements either perform actions (such as calculations or input or


output of data) or make decision. Using relational operators we can compare two
variables in the program. The C relational operators are summarized below, with their
meanings. Pay particular attention to the equality operator; it consists of two equal
signs, not just one. This section introduces a simple version of C’s if control structure
that allows a program to make a decision based on the result of some condition. If the
condition is true then the statement in the body of if statement is executed else if the
condition is false, the statement is not executed. Whether the body statement is
executed or not, after the if structure completes, execution proceeds with the next
statement after the if structure. Conditions in the if structure are formed with the
relational operators which are summarized in the Table 4.1.
52
Table 1: Relational Operators in C Expressions and
Operators

Relational Operator Condition Meaning


== x==y x is equal to y
!= x!=y x is not equal to y
< x<y x is less than y
<= x<=y x is less than or equal to y
> x>y x is greater than y
>= x>=y x is greater or equal to y

Relational operators usually appear in statements which are inquiring about the truth
of some particular relationship between variables. Normally, the relational operators
in C are the operators in the expressions that appear between the parentheses.
For example,

(i) if (thisNum < minimumSoFar) minimumSoFar = thisNum

(ii) if (job == Teacher) salary == minimumWage

o m
(iii) if (numberOfLegs != 8) thisBug = insect
. c
(iv) if (degreeOfPolynomial < 2) polynomial = linear
o t
s
Let us see a simple C program containing the If statement (will be introduced in detail p
in the next unit). It displays the relationship between two numbers read from the
keyboard.
o g
b l
Example: 4.1

6 .
/*Program to find relationship between two numbers*/

0 0
#include <stdio.h>
main ( )
i a
{
o n
int a, b;
p o
i l
printf ( “Please enter two integers: ”);
scanf (“%d%d”, &a, &b);

u n
if (a <= b)
printf (“ %d <= %d\n”,a,b);
selse
printf (“%d > %d\n”,a,b);
}

OUTPUT
Please enter two integers: 12 17
12 <= 17

We can change the values assigned to a and b and check the result.

4.5 LOGICAL OPERATORS


Logical operators in C, as with other computer languages, are used to evaluate
expressions which may be true or false. Expressions which involve logical operations
are evaluated and found to be one of two values: true or false. So far we have studied
simple conditions. If we want to test multiple conditions in the process of making a
53
An Introduction to C decision, we have to perform simple tests in separate IF statements(will be introduced
in detail in the next unit). C provides logical operators that may be used to form more
complex conditions by combining simple conditions.

The logical operators are listed below:

Operator Meaning

&& Logical AND

|| Logical OR

! Logical NOT

Thus logical operators (AND and OR) combine two conditions and logical NOT is
used to negate the condition i.e. if the condition is true, NOT negates it to false and
vice versa.Let us consider the following examples:
(i) Suppose the grade of the student is ‘B’ only if his marks lie within the range 65 to
75,if the condition would be:
o m
if ((marks >=65) && (marks <= 75))
. c
printf (“Grade is B\n”);

o t
s p
(ii) Suppose we want to check that a student is eligible for admission if his PCM is
greater than 85% or his aggregate is greater than 90%, then,

o g
b l
if ((PCM >=85) ||(aggregate >=90))
printf (“Eligible for admission\n”);

6 .
Logical negation (!) enables the programmer to reverse the meaning of the condition.

0 0
Unlike the && and || operators, which combines two conditions (and are therefore
Binary operators), the logical negation operator is a unary operator and has one single

i a
condition as an operand. Let us consider an example:

o on if !(grade==’A’)
printf (“the next grade is %c\n”, grade);

l p The parentheses around the condition grade==A are needed because the logical

n i operator has higher precedence than equality operator. In a condition if all the
operators are present then the order of evaluation and associativity is provided in the

su table. The truth table of the logical AND (&&), OR (||) and NOT (!) are given below.

These table show the possible combinations of zero (false) and nonzero (true) values
of x (expression1) and y (expression2) and only one expression in case of NOT
operator. The following table 4.2 is the truth table for && operator.

Table 4. 2: Truth table for && operator

x y x&&y
zero zero 0

Non zero zero 0

zero Non zero 0

Non zero Non zero 1

54
The following table 4.3 is the truth table for || operator. Expressions and
Operators

Table 4.3: Truth table for || operator

x y x || y
zero zero 0

Non zero zero 1

zero Non zero 1

Non zero Non zero 1

The following table 4.4 is the truth table for ! operator.

Table 4.4: Truth table for ! operator

x !x o m
zero 1
. c
Non zero 0
o t
s p
The following table 4.5 shows the operator precedence and associativity
o g
Table 4.5: (Logical operators precedence and associativity)
b l
Operator
!
Associativity
Right to left
6 .
0 0
&&
a
Left to right

i
||

o n Left to right

p o
4.6
i l
COMMA AND CONDITIONAL OPERATORS

u n
sConditional Operator
C provides an called as the conditional operator (?:) which is closely related to the
if/else structure. The conditional operator is C’s only ternary operator - it takes three
operands. The operands together with the conditional operator form a conditional
expression. The first operand is a condition, the second operand represents the value
of the entire conditional expression it is the condition is true and the third operand is
the value for the entire conditional expression if the condition is false.

The syntax is as follows:


(condition)? (expression1): (expression2);

If condition is true, expression1 is evaluated else expression2 is evaluated.


Expression1/Expression2 can also be further conditional expression i.e. the case of
nested if statement (will be discussed in the next unit).

55
An Introduction to C Let us see the following examples:

(i) x= (y<20) ? 9: 10;


This means, if (y<20), then x=9 else x=10;

(ii) printf (“%s\n”, grade>=50? “Passed”: “failed”);


The above statement will print “passed” grade>=50 else it will print “failed”

(iii) (a>b) ? printf (“a is greater than b \n”): printf (“b is greater than a \n”);

If a is greater than b, then first printf statement is executed else second printf
statement is executed.

Comma Operator

A comma operator is used to separate a pair of expressions. A pair of expressions


separated by a comma is evaluated left to right, and the type and value of the result are
the value of the type and value of the right operand. All side effects from the
evaluation of the left operand are completed before beginning evaluation of the right
o m
operand. The left side of comma operator is always evaluated to void. This means that
. c
expression. For example,
o t
the expression on the right hand side becomes the value of the total comma-separated

x = (y=2, y - 1);
s p
o g
first assigns y the value 2 and then x the value 1. Parenthesis is necessary since

l
comma operator has lower precedence than assignment operator.
b
.
Generally, comma operator (,) is used in the for loop (will be introduced in the next
6
unit)

0 0
For example,

i a
o n {
for (i = 0,j = n;i<j; i++,j--)

p o }
printf (“A”);

i l
u n In this example for is the looping construct (discussed in the next unit). In this loop,
i = 0 and j = n are separated by comma (,) and i++ and j—are separated by comma (,).

s The example will be clear to you once you have learnt for loop (will be introduced in
the next unit).

Essentially, the comma causes a sequence of operations to be performed. When it is


used on the right hand side of the assignment statement, the value assigned is the
value of the last expression in the comma-separated list.

Check Your Progress 2


1. Given a=3, b=4, c=2, what is the result of following logical expressions:
(a < --b) && (a==c)
………………………………………………………………………………………
………………………………………………………………………………………
2. Give the output of the following code:
main()
{
56 int a=10, b=15,x;
x = (a<b)?++a:++b; Expressions and
Operators
printf(“x=%d a=%d b=%d\n”,x,a,b);
}
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………

3. What is the use of comma operator?


………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………

4.7 TYPE CAST OPERATOR


o m
We have seen in the previous sections and last unit that when constants and variables
. c
of different types are mixed in an expression, they are converted to the same type.
That is automatic type conversion takes place. The following type conversion rules are
o t
followed:
s p
1.
o
All chars and short ints are converted to ints. All floats are converted to g
doubles.

b l
2.
.
In case of binary operators, if one of the two operands is a long double, the other
operand is converted to long double,
6
0 0
else if one operand is double, the other is converted to double,
a
else if one operand is long, the other is converted to long,
i
n
else if one operand is unsigned, the other is converted to unsigned,

o
p o
C converts all operands “up” to the type of largest operand (largest in terms of
memory requirement for e.g. float requires 4 bytes of storage and int requires 2 bytes

l
of storage so if one operand is int and the other is float, int is converted to float).
i
u n
All the above mentioned conversions are automatic conversions, but what if int is to

sbe converted to float. It is possible to force an expression to be of specific type by


using operator called a cast. The syntax is as follows:
(type) expression

where type is the standard C data type. For example, if you want to make sure that the
expression a/5 would evaluate to type float you would write it as
( float ) a/5

cast is an unary operator and has the same precedence as any other unary operator.
The use of cast operator is explained in the following example:
main()
{
int num;
printf(“%f %f %f\n”, (float)num/2, (float)num/3, float)num/3);
}
57
An Introduction to C Tha cast operator in this example will ensure that fractional part is also displayed on
the screen.

4.8 SIZE OF OPERATOR


C provides a compile-time unary operator called sizeof that can be used to compute
the size of any object. The expressions such as:
sizeof object and sizeof(type name)

result in an unsigned integer value equal to the size of the specified object or type in
bytes. Actually the resultant integer is the number of bytes required to store an object
of the type of its operand. An object can be a variable or array or structure. An array
and structure are data structures provided in C, introduced in latter units. A type name
can be the name of any basic type like int or double or a derived type like a structure
or a pointer.

For example,
sizeof(char) = 1bytes
o m
sizeof(int) = 2 bytes
. c
4.9 C SHORTHAND
o t
s p
statements. For example:
o g
C has a special shorthand that simplifies coding of certain type of assignment

a = a+2;
b l
can be written as:
6 .
a += 2;
0 0
i a
o n
The operator +=tells the compiler that a is assigned the value of a + 2;
This shorthand works for all binary operators in C. The general form is:

o
lp
variable operator = variable / constant / expression

ni
These operators are listed below:

su
Operators Examples Meaning
+= a+=2 a=a+2

-= a-=2 a=a-2

= a*=2 a = a*2

/= a/=2 a=a/2

%= a%=2 a=a%2

Operators Examples Meaning


&&= a&&=c a=a&&c

||= a||=c a=a||c

58
Expressions and
4.10 PRIORITY OF OPERATORS Operators

Since all the operators we have studied in this unit can be used together in an
expression, C uses a certain hierarchy to solve such kind of mixed expressions. The
hierarchy and associatively of the operators discussed so far is summarized in Table 6.
The operators written in the same line have the same priority. The higher precedence
operators are written first
Table 4.6: Precedence of the operators

Operators Associativity
() Left to right
! ++ -- (type) sizeof Right to left
/% Left to right
+- Left to right
< <= > >= Left to right
== != Left to right
&& Left to right
|| Left to right
o m
?:
= += -= *= /= %= &&= ||=
Right to left
Right to left
. c
, Left to right

o t
s p
Check Your Progress 3
o g
1. Give the output of the following C code:
b l
main( )
6 .
{
int a,b=5;
0 0
float f;

i a
a=5/2;
f=(float)b/2.0;
o n
p
(a<f)? b=1:b=0;
o
}
i l
printf(“b = %d\n”,b);

u n ………………………………………………………………………………………

s ………………………………………………………………………………………
………………………………………………………………………………………

2. What is the difference between && and &. Explain with an example.
………………………………………………………………………………………
………………………………………………………………………………………
……………………………………………………………………………….
3. Use of Bit Wise operators makes the execution of the program.
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………

59
An Introduction to C

4.11 SUMMARY
In this unit, we discussed about the different types of operators, namely arithmetic,
relational, logical present in C and their use. In the following units, you will study
how these are used in C’s other constructs like control statements, arrays etc.

This unit also focused on type conversions. Type conversions are very important to
understand because sometimes a programmer gets unexpected results (logical error)
which are most often caused by type conversions in case user has used improper types
or if he has not type cast to desired type.

This unit also referred to C shorthand. C is referred to as a compact language which is


because lengthy expressions can be written in short form. Conditional operator is one
of the examples, which is the short form of writing the if/else construct (next unit).
Also increment/decrement operators reduce a bit of coding when used in expressions.

Since Logical operators are used further in all types of looping constructs and if/else
construct (in the next unit), they should be thoroughly understood.
o m
. c
4.12 SOLUTIONS / ANSWERS
o t
Check Your Progress 1
s p
1. C expression would be
o g
i) ((a*4*c*c)-d)/(m+n)
b l
ii) a*b-(e+f)*4/c
6 .
2.
0 0
The output would be:

a
a=1 b=4 c=4 k=10

i
on
3. There is no such operator as **.

po Check Your Progress 2

il 1. The expression is evaluated as under:

sun (3 < - -4) && (3== 2)


(3 < 3) && (3==2)
0 && 0
0
Logical false evaluates to 0 and logical true evaluates to 1.

2. The output would be as follows:


x=11, a=11, b=16

3. Comma operator causes a sequence of operators to be performed.

Check Your Progress 3


1. Here a will evaluate to 2 and f will evaluate to 2.5 since type cast operator is
used in the latter so data type of b changes to float in an expression. Therefore,
output would be b=1.

60
2. && operator is a logical and operator and & is a bit wise and operator. Expressions and
Operators
Therefore, && operator always evaluates to true or false i.e 1 or 0 respectively
while & operator evaluates bit wise so the result can be any value. For example:

2 && 5 => 1(true)


2 & 5 => 0(bit-wise anding)

3. Use of Bit Wise operators makes the execution of the program faster.

4.13 FURTHER READINGS


1. The C Programming Language, Kernighan & Richie, PHI Publication.
2. Computer Science A structured programming approach using C, Behrouza A.
Forouzan, Richard F. Gilberg, Second Edition, Brooks/Cole, Thomson Learning,
2001.
3. Programming with C, Second Edition, Byron Gottfried, Schaum Outline, Tata
Mc Graw Hill, 2003.

o m
. c
o t
s p
o g
b l
6 .
0 0
i a
o n
p o
i l
u n
s

61
Decision and Loop
UNIT 5 DECISION AND LOOP CONTROL Control
Statements
STATEMENTS
Structure
5.0 Introduction
5.1 Objectives
5.2 Decision Control Statements
5.2.1 The if Statement
5.2.2 The switch Statement
5.3 Loop Control Statements
5.3.1 The while Loop
5.3.2 The do-while Statement
5.3.3 The for Loop
5.3.4 The Nested Loop
5.4 The Goto Statement
5.5 The Break Statement
5.6 The Continue Statement
5.7 Summary
o m
5.8
5.9
Solutions / Answers
Further Readings
. c
o t
5.0 INTRODUCTION
s p
A program consists of a number of statements to be executed by the computer. Not
o
many of the programs execute all their statements in sequential order from beginning g
l
to end as they appear within the program. A C program may require that a logical test
b
6 .
be carried out at some particular point within the program. One of the several possible
actions will be carried out, depending on the outcome of the logical test. This is called

0 0
Branching. In the Selection process, a set of statements will be selected for execution,
among the several sets available. Suppose, if there is a need of a group of statements

i a
to be executed repeatedly until some logical condition is satisfied, then looping is
required in the program. These can be carried out using various control statements.

o n
These Control statements determine the “flow of control” in a program and enable us

p o
to specify the order in which the various instructions in a program are to be executed

i l
by the computer. Normally, high level procedural programming languages require
three basic control statements:


u n Sequence instruction

s•

Selection/decision instruction
Repetition or Loop instruction

Sequence instruction means executing one instruction after another, in the order in
which they occur in the source file. This is usually built into the language as a default
action, as it is with C. If an instruction is not a control statement, then the next
instruction to be executed will simply be the next one in sequence.

Selection means executing different sections of code depending on a specific


condition or the value of a variable. This allows a program to take different courses of
action depending on different conditions. C provides three selection structures.

• if
• if…else
• switch

5
Control Statements, Repetition/Looping means executing the same section of code more than once. A
Arrays and
Functions
section of code may either be executed a fixed number of times, or while some
condition is true. C provides three looping statements:

• while
• do…while
• for

This unit introduces you the decision and loop control statements that are available in
C programming language along with some of the example programs.

5.1 OBJECTIVES
After going through this unit you will be able to:

• work with different control statements;


• know the appropriate use of the various control statements in programming;
• transfer the control from within the loops;


use the goto, break and continue statements in the programs; and
o m
write programs using branching, looping statements.

. c
5.2 DECISION CONTROL STATEMENTS
o t
s p
In a C program, a decision causes a one-time jump to a different part of the program,

o g
depending on the value of an expression. Decisions in C can be made in several ways.
The most important is with the if...else statement, which chooses between two

b l
alternatives. This statement can be used without the else, as a simple if statement.

6 .
Another decision control statement, switch, creates branches for multiple alternative
sections of code, depending on the value of a single variable.

0
5.2.1 The if Statement 0
i a
It is used to execute an instruction or sequence/block of instructions only if a
n
condition is fulfilled. In if statements, expression is evaluated first and then,
o
p o depending on whether the value of the expression (relation or condition) is “true” or
“false”, it transfers the control to a particular statement or a group of statements.

i l Different forms of implementation if-statement are:

u n • Simple if statement
s •


If-else statement
Nested if-else statement
Else if statement

Simple if statement
It is used to execute an instruction or block of instructions only if a condition is
fulfilled.
The syntax is as follows:
if (condition)
statement;

where condition is the expression that is to be evaluated. If this condition is true,


statement is executed. If it is false, statement is ignored (not executed) and the
program continues on the next instruction after the conditional statement.
6
Decision and Loop
Control
Statements
This is shown in the Figure 5.1 given below:

Figure 5.1: Simple if statement

If we want more than one statement to be executed, then we can specify a block of
statements within the curly bracets { }. The syntax is as follows:
o m
if (condition)
. c
{
block of statements;
o t
}
s p
Example 5.1
o g
b l
Write a program to calculate the net salary of an employee, if a tax of 15% is levied
on his gross-salary if it exceeds Rs. 10,000/- per month.

6 .
0
/*Program to calculate the net salary of an employee */
0
#include <stdio.h>
i a
main( )
{
o n
o
float gross_salary, net_salary;

p
i l
printf(“Enter gross salary of an employee\n”);
scanf(“%f ”,&gross_salary );

u n
sif (gross_salary <10000)
net_salary= gross_salary;
if (gross_salary >= 10000)
net_salary = gross_salary- 0.15*gross_salary;

printf(“\nNet salary is Rs.%.2f\n”, net_salary);


}

OUTPUT

Enter gross salary of an employee


9000
Net salary is Rs.9000.00

Enter gross salary of any employee


10000
Net salary is Rs. 8500.00
7
Control Statements, If … else statement
Arrays and
Functions
If…else statement is used when a different sequence of instructions is to be executed
depending on the logical value (True / False) of the condition evaluated.
Its form used in conjunction with if and the syntax is as follows:
if (condition)
Statement _1;
else
Statement_ 2;
statement_3;

Or

if (condition)
{
Statements_1_Block;
}
else
o m
{
Statements_2_Block;
. c
}
Statements _3_Block;
o t
s p
o g
If the condition is true, then the sequence of statements (Statements_1_Block)
executes; otherwise the Statements_2_Block following the else part of if-else

b l
statement will get executed. In both the cases, the control is then transferred to
Statements_3 to follow sequential execution of the program.

.
This is shown in figure 5.2 given below:
6
0 0
i a
o n
p o
i l
u n
s

Figure 5.2: If…else statement

Let us consider a program to illustrate if…else statement,

Example 5.2
Write a program to print whether the given number is even or odd.
8
Decision and Loop
Control
Statements
/* Program to print whether the given number is even or odd*/
#include <stdio.h>
main ( )
{
int x;
printf(“Enter a number:\n”);
scanf("%d",&x);
if (x % 2 == 0)
printf(“\nGiven number is even\n”);
else
printf(“\nGiven number is odd\n”);
}

OUTPUT

Enter a number:
6
Given number is even
o m
. c
Enter a number
o t
7
Given number is odd
s p
Nested if…else statement
o g
b l
In nested if… else statement, an entire if…else construct is written within either the

6 .
body of the if statement or the body of an else statement. The syntax is as follows:
if (condition_1)
0 0
{
if (condition_2)
i a
{
Statements_1_Block;
o n
}
p o
else
i l
u n {
Statements_2_Block;

s }
}

else
{
Statements_3_Block;
}
Statement_4_Block;

Here, condition_1 is evaluated. If it is false then Statements_3_Block is executed and


is followed by the execution of Statements_4_Block, otherwise if condition_1 is true,
then condition_2 is evaluated. Statements_1_Block is executed when condition_2 is
true otherwise Statements_2_Block is executed and then the control is transferred to
Statements_4_Block.

This is shown in the figure 5.3 given in the next page:

9
Control Statements,
Arrays and
Functions

o m
. c
Figure 5.3: Nested if…else statement
o t
s p
Let us consider a program to illustrate Nested if…else statement,

o g
Example 5.3

b l
conditions:
6 .
Write a program to calculate an Air ticket fare after discount, given the following

0 0
• If passenger is below 14 years then there is 50% discount on fare
• If passenger is above 50 years then there is 20% discount on fare
a
• If passenger is above 14 and below 50 then there is 10% discount on fare.
i
o n
/* Program to calculate an Air ticket fare after discount */

o
lp
#include <stdio.h>
main( )

n i {

su int age;
float fare;
printf(“\n Enter the age of passenger:\n”);
scanf(“%d”,&age);
printf(“\n Enter the Air ticket fare\n”);
scanf(“%f”,&fare);
if (age < 14)
fare = fare - 0.5 * fare;
else
if (age <= 50)
{
fare = fare - 0.1 * fare;
}
else
{
fare = fare - 0.2 * fare;
}
printf(“\n Air ticket fare to be charged after discount is %.2f”,fare);
10 }
Decision and Loop
Control
OUTPUT Statements
Enter the age of passenger
12
Enter the Air ticket fare
2000.00
Air ticket fare to be charged after discount is 1000.00

Else if statement
To show a multi-way decision based on several conditions, we use the else if
statement. This works by cascading of several comparisons. As soon as one of the
conditions is true, the statement or block of statements following them is executed and
no further comparisons are performed. The syntax is as follows:
if (condition_1)
{
Statements_1_Block;
}
else if (condition_2)
o m
{
Statements_2_Block;
. c
}
------------
o t
else if (condition_n)
s p
{
Statements_n_Block;
o g
else
}

b l
Statements_x;
6 .
0
Here, the conditions are evaluated in order from top to bottom. As soon as any
0
i a
condition evaluates to true, then the statement associated with the given condition is
executed and control is transferred to Statements_x skipping the rest of the

o n
conditions following it. But if all conditions evaluate false, then the statement
following final else is executed followed by the execution of Statements_x. This is
o
shown in the figure 5.4 given below:
p
i l
u n
s

Figure 5.4: Else if statement


11
Control Statements, Let us consider a program to illustrate Else if statement,
Arrays and
Functions
Example 5.4
Write a program to award grades to students depending upon the criteria mentioned
below:
• Marks less than or equal to 50 are given “D” grade
• Marks above 50 but below 60 are given “C” grade
• Marks between 60 to 75 are given “B” grade
• Marks greater than 75 are given “A” grade.

/* Program to award grades */


#include <stdio.h>
main()
{
int result;
printf("Enter the total marks of a student:\n");
scanf("%d",&result);
if (result <= 50)
printf("Grade D\n");
o m
else if (result <= 60)
. c
printf("Grade C\n");
else if (result <= 75)
o t
printf("Grade B\n");
else
s p
}
printf("Grade A\n");
o g
b l
OUTPUT

6 .
Enter the total marks of a student:
80
Grade A
0 0
i a
Check Your Progress 1

o n
o 1. Find the output for the following program:

i lp #include <stdio.h>
main()

u n {
int a=1, b=1;

s if(a==0)
if(b==0)
printf(“HI”);
else
printf(“Bye”);
}

…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………

2. Find the output for the following program:


#include <stdio.h>
main()
12 {
int a,b=0; Decision and Loop
Control
if (a=b=1) Statements
printf(“hello”);
else
printf(“world”);
}
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………

5.2.2 The Switch Statement


Its objective is to check several possible constant values for an expression, something
similar to what we had studied in the earlier sections, with the linking of several if and
else if statements. When the actions to be taken depending on the value of control
variable, are large in number, then the use of control structure Nested if…else makes
the program complex. There switch statement can be used. Its form is the following:

o m
switch (expression){
case expression 1:
. c
block of instructions 1
break;
o t
case expression 2:
s p
block of instructions 2
break;
o g
.
.
b l
default:
default block of instructions
6 .
}
0 0
i a
It works in the following way: switch evaluates expression and checks if it is

o n
equivalent to expression1. If it is, it executes block of instructions 1 until it finds the
break keyword, moment at finds the control will go to the end of the switch. If

p o
expression was not equal to expression 1 it will check whether expression is
equivalent to expression 2. If it is, it will execute block of instructions 2 until it finds

i l
the break keyword.

u n
Finally, if the value of expression has not matched any of the previously specified

sconstants (you may specify as many case statements as values you want to check), the
program will execute the instructions included in the default: section, if it exists, as it
is an optional statement.

Let us consider a program to illustrate Switch statement,

Example 5.5
Write a program that performs the following, depending upon the choice selected by
the user.
i). calculate the square of number if choice is 1
ii). calculate the cube of number if choice is 2 and 4
iii). calculate the cube of the given number if choice is 3
iv). otherwise print the number as it is

main()
{
int choice,n; 13
Control Statements, printf(“\n Enter any number:\n “);
Arrays and
Functions
scanf(“%d”,&n);
printf(“Choice is as follows:\n\n”);
printf(“1. To find square of the number\n”);
printf(“2. To find square-root of the number\n”);
printf(“3. To find cube of a number\n”);
printf(“4. To find the square-root of the number\n\n”);
printf(“Enter your choice:\n”);
scanf(“%d”,&choice);
switch (choice)
{
case 1 : printf(“The square of the number is %d\n”,n*n);
break;
case 2 :
case 4 : printf(“The square-root of the given number is %f”,sqrt(n));
break;
case 3: printf(“ The cube of the given number is %d”,n*n*n);
default : printf(“The number you had given is %d”,n);

}
break;

o m
}
. c
OUTPUT

o t
Enter any number:
4
s p
Choice is as follows:
o g
1. To find square of the number
b l
6 .
2. To find square-root of the number\n");
3. To find cube of a number

0
4. To find the square-root of the number

0
2
i a
Enter your choice:

o n
The square-root of the given number is 2

o
lp
In this section we had discussed and understood various decision control statements.
Next section explains you the various loop control statements in C.

n i
su 5.3 LOOP CONTROL STATEMENTS
Loop control statements are used when a section of code may either be executed a
fixed number of times, or while some condition is true. C gives you a choice of three
types of loop statements, while, do- while and for.

• The while loop keeps repeating an action until an associated condition returns
false. This is useful where the programmer does not know in advance how
many times the loop will be traversed.
• The do while loop is similar, but the condition is checked after the loop body is
executed. This ensures that the loop body is run at least once.
• The for loop is frequently used, usually where the loop will be traversed a fixed
number of times.

5.3.1 The While Loop


When in a program a single statement or a certain group of statements are to be
executed repeatedly depending upon certain test condition, then while statement is
14 used.
The syntax is as follows: Decision and Loop
Control
Statements
while (test condition)
{
body_of_the_loop;
}

Here, test condition is an expression that controls how long the loop keeps running.
Body of the loop is a statement or group of statements enclosed in braces and are
repeatedly executed till the value of test condition evaluates to true. As soon as the
condition evaluates to false, the control jumps to the first statement following the
while statement. If condition initially itself is false, the body of the loop will never be
executed. While loop is sometimes called as entry-control loop, as it controls the
execution of the body of the loop depending upon the value of the test condition. This
is shown in the figure 5.5 given below:

o m
. c
o t
s p
o g
b l
6 .
0 0
i a
n
Figure 5.5: The while loop statement

o
p o
Let us consider a program to illustrate while loop,

i
Example 5.6 l
u n
Write a program to calculate the factorial of a given input natural number.

s/* Program to calculate factorial of given number */

#include <stdio.h>
#include <math.h>
#include <stdio.h>
main( )
{
int x;
long int fact = 1;
printf(“Enter any number to find factorial:\n”); /*read the number*/
scanf(“%d”,&x);
while (x > 0)
{
fact = fact * x; /* factorial calculation*/
x=x-1;
}
printf(“Factorial is %ld”,fact); 15
Control Statements, }
Arrays and
Functions
OUTPUT

Enter any number to find factorial:


4
Factorial is 24

Here, condition in while loop is evaluated and body of loop is repeated until condition
evaluates to false i.e., when x becomes zero. Then the control is jumped to first
statement following while loop and print the value of factorial.

5.3.2 The do...while Loop


There is another loop control structure which is very similar to the while statement –
called as the do.. while statement. The only difference is that the expression which
determines whether to carry on looping is evaluated at the end of each loop. The
syntax is as follows:

o m
do
{
. c
statement(s);
} while(test condition);
o t
s p
o g
In do-while loop, the body of loop is executed at least once before the condition is
evaluated. Then the loop repeats body as long as condition is true. However, in while

b l
loop, the statement doesn’t execute the body of the loop even once, if condition is
false. That is why do-while loop is also called exit-control loop. This is shown in the
figure 5.6 given below.
6 .
0 0
i a
o n
p o
i l
u n
s
Figure 5.6: The do…while statement

Let us consider a program to illustrate do..while loop,

Example 5.7
Write a program to print first ten even natural numbers.

/* Program to print first ten even natural numbers */


#include <stdio.h>
main()
16 {
int i=0; Decision and Loop
Control
int j=2; Statements
do {
printf(“%d”,j);
j =j+2;
i=i+1; } while (i<10); }

OUTPUT
2 4 6 8 10 12 14 16 18 20

5.3.3 The for Loop


for statement makes it more convenient to count iterations of a loop and works well
where the number of iterations of the loop is known before the loop is entered. The
syntax is as follows:
for (initialization; test condition; increment or decrement)
{

}
Statement(s);

o m
The main purpose is to repeat statement while condition remains true, like the while
. c
loop. But in addition, for provides places to specify an initialization instruction and an
increment or decrement of the control variable instruction. So this loop is specially
o t
designed to perform a repetitive action with a counter.
s p
The for loop as shown in figure 5.7, works in the following manner:
o g
b l
1. initialization is executed. Generally it is an initial value setting for a counter
variable. This is executed only once.

6 .
2. condition is checked, if it is true the loop continues, otherwise the loop finishes and
statement is skipped.
0 0
3. Statement(s) is/are executed. As usual, it can be either a single instruction or a

i a
block of instructions enclosed within curly brackets { }.

o n
4. Finally, whatever is specified in the increment or decrement of the control variable
field is executed and the loop gets back to step 2.

p o
i l
u n
s

Figure 5.7: The for statement 17


Control Statements,
Arrays and
Functions
Let us consider a program to illustrate for loop,

Example 5.8
Write a program to print first n natural numbers.

/* Program to print first n natural numbers */

#include <stdio.h>
main( )
{
int i,n;
printf(“Enter value of n \n”);
scanf(“%d”,&n);
printf(“\nThe first %d natural numbers are :\n”, n);
for (i=1;i<=n;++i)
{
printf(“%d”,i);
o m
}
}

. c
OUTPUT
o t
Enter value of n
s p
6
The first 6 natural numbers are:
o g
123456
b l
6 .
The three statements inside the braces of a for loop usually meant for one activity

0 0
each, however any of them can be left blank also. More than one control variables can
be initialized but should be separated by comma.

i a
Various forms of loop statements can be:

o n
(a) for(;condition;increment/decrement)

p o body;
A blank first statement will mean no initialization.

i l
u n (b) for (initialization;condition;)
body;

s A blank last statement will mean no running increment/decrement.

(c) for (initialization;;increment/decrement)


body;

A blank second conditional statement means no test condition to control the exit
from the loop. So, in the absence of second statement, it is required to test the
condition inside the loop otherwise it results in an infinite loop where the control
never exits from the loop.

(d) for (;;increment/decrement)


body;
Initialization is required to be done before the loop and test condition is checked
inside the loop.

(e) for (initialization;;)


body;
18
Test condition and control variable increment/decrement is to be done inside the Decision and Loop
Control
body of the loop. Statements

(f) for (;condition;)


body;
Initialization is required to be done before the loop and control variable
increment/decrement is to be done inside the body of the loop.

(g) for (;;;)


body;
Initialization is required to be done before the loop, test condition and control
variable increment/decrement is to be done inside the body of the loop.

5.3.4 The Nested Loops


C allows loops to be nested, that is, one loop may be inside another. The program
given below illustrates the nesting of loops.

Let us consider a program to illustrate nested loops,

o m
Example 5.9
. c
Write a program to generate the following pattern given below:
o t
1
1 2
s p
1 2 3
o g
1 2 3 4

b l
/* Program to print the pattern */

6 .
#include <stdio.h>
main( )
0 0
{
i a
int i,j;
for (i=1;i<=4;++i)
o n
{

p
printf("%d\n",i); o
l
for(j=1;j<=i;++j)
i
u n }
printf("%d\t",j);

s}
Here, an inner for loop is written inside the outer for loop. For every value of i, j
takes the value from 1 to i and then value of i is incremented and next iteration of
outer loop starts ranging j value from 1 to i.

Check Your Progress 2


1. Predict the output :
#include <stdio.h>
main()
{
int i;
for (i=0;i<=10;i++,printf(“%d ”,i));
}
…………………………………………………………………………………
…………………………………………………………………………………
19
Control Statements,
Arrays and
Functions
2. What is the output?

#include <stdio.h>
main( )
{
int i;
for(i=0;i<3;i++)
printf("%d ",i);
}
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
3. What is the output for the following program?

#include <stdio.h>
main( )
o m
{
int i=1;
. c
do
o t
{
printf(“%d”,i);
s p
}
}while(i=i-1);
o g
…………………………………………………………………………………
b l
.
…………………………………………………………………………………
6
0
…………………………………………………………………………………
0
a
4. Give the output of the following:
i
on
#include <stdio.h>
main( )

p o {

i l int i=3;
while(i)

u n {
int x=100;

s printf(“\n%d..%d”,i,x);
x=x+1;
i=i+1;
}
}
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………

5.4 THE goto STATEMENT


The goto statement is used to alter the normal sequence of program instructions by
transferring the control to some other portion of the program. The syntax is as follows:
goto label;
20
Here, label is an identifier that is used to label the statement to which control will be Decision and Loop
Control
transferred. The targeted statement must be preceded by the unique label followed by Statements
colon.

label : statement;

Although goto statement is used to alter the normal sequence of program execution
but its usage in the program should be avoided. The most common applications are:
i). To branch around statements under certain conditions in place of use of if-
else statement,
ii). To jump to the end of the loop under certain conditions bypassing the rest of
statements inside the loop in place of continue statement,
iii). To jump out of the loop avoiding the use of break statement.

goto can never be used to jump into the loop from outside and it should be preferably
used for forward jump.

Situations may arise, however, in which the goto statement can be useful. To the
possible extent, the use of the goto statement should generally be avoided.
o m
Let us consider a program to illustrate goto and label statements.
. c
o t
Example 5.10

s p
Write a program to print first 10 even numbers

o g
/* Program to print 10 even numbers */

b l
#include <stdio.h>
main()
6 .
{
0 0
int i=2;
while(1)
i a
{
printf(“%d ”,i);
o n
i=i+2;
p
if (i>=20) o
i l goto outside;

u n }
outside : printf(“over”);

s }

OUTPUT
2 4 6 8 10 12 14 16 18 20 over

5.5 THE break STATEMENT


Sometimes, it is required to jump out of a loop irrespective of the conditional test
value. Break statement is used inside any loop to allow the control jump to the
immediate statement following the loop. The syntax is as follows:
break;

When nested loops are used, then break jumps the control from the loop where it has
been used. Break statement can be used inside any loop i.e., while, do-while, for and
also in switch statement.

Let us consider a program to illustrate break statement. 21


Control Statements, Example 5.11
Arrays and
Functions Write a program to calculate the first smallest divisor of a number.

/*Program to calculate smallest divisor of a number */

#include <stdio.h>
main( )
{
int div,num,i;
printf(“Enter any number:\n”);
scanf(“%d”,&num);
for (i=2;i<=num;++i)
{
if ((num % i) == 0)
{
printf(“Smallest divisor for number %d is %d”,num,i);
break;

}
}

o m
}
OUTPUT
. c
Enter any number:
o t
9
Smallest divisor for number 9 is 3
s p
o g
In the above program, we divide the input number with the integer starting from 2

b l
onwards, and print the smallest divisor as soon as remainder comes out to be zero.
Since we are only interested in first smallest divisor and not all divisors of a given

6 .
number, so jump out of the for loop using break statement without further going for

0
the next iteration of for loop.

0
i a
Break is different from exit. Former jumps the control out of the loop while exit stops
the execution of the entire program.

o n
p o 5.6 THE continue STATEMENT

i l Unlike break statement, which is used to jump the control out of the loop, it is

u n sometimes required to skip some part of the loop and to continue the execution with
next loop iteration. Continue statement used inside the loop helps to bypass the

s section of a loop and passes the control to the beginning of the loop to continue the
execution with the next loop iteration. The syntax is as follows:
continue;

Let us see the program given below to know the working of the continue statement.

Example 5.12
Write a program to print first 20 natural numbers skipping the numbers divisible by 5.

/* Program to print first 20 natural numbers skipping the numbers divisible by 5 */

#include <stdio.h>
main( )
{
int i;
for (i=1;i<=20;++i)
22 {
if ((i % 5) == 0) Decision and Loop
Control
continue; Statements
printf(“%d ”,i);
}
}

OUTPUT

1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19

Here, the printf statement is bypassed each time when value stored in i is divisible by
5.

Check Your Progress 3


1. How many times will hello be printed by the following program?
#include <stdio.h>
main( )
{
o m
int i = 5;
while(i)
. c
{
o t
i=i-1;
if (i==3)
s p
continue;
printf(“\nhello”);
o g
}
}
b l
…………………………………………………………………………………
6 .
…………………………………………………………………………………
0 0
…………………………………………………………………………………
i a
#include <stdio.h>
o n
2. Give the output of the following program segment:

main( )
p o
{

i l
int num,sum;

u nfor (num=2,sum=0;;)
{
s sum = sum + num;
if (num > 10)
break;
num=num+1;
}
printf("%d",sum);
}
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
3. What is the output for the following program?

#include <stdio.h>
main( )
{
int i, n = 3; 23
Control Statements, for (i=3;n<=20;++n)
Arrays and
Functions
{
if (n%i == 0)
break;
if (i == n)
printf(“%d\n”,i);
}
}
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………

5.7 SUMMARY
A program is usually not limited to a linear sequence of instructions. During its
process it may require to repeat execution of a part of code more than once depending
upon the requirements or take decisions. For that purpose, C provides control and
o m
by C language namely while, do…while and for.
. c
looping statements. In this unit, we had seen the different looping statements provided

o t
Using break statement, we can leave a loop even if the condition for its end is not

s p
fulfilled. It can be used to end an infinite loop, or to force it to end before its natural
end. The continue statement causes the program to skip the rest of the loop in the
g
present iteration as if the end of the statement block would have reached, causing it to
o
jump to the following iteration.

b l
6 .
Using the goto statement, we can make an absolute jump to another point in the
program. You should use this feature carefully since its execution ignores any type of

0 0
nesting limitation. The destination point is identified by a label, which is then used as
argument for the goto instruction. A label is made of a valid identifier followed by a
colon (:).
i a
o n
5.8 SOLUTIONS / ANSWERS
o
lp Check Your Progress 1

ni 1 Nothing

su 2 hello

Check Your Progress 2


1 1 2 3 4 5 6 7 8 9 10 11

2 012

3 102

4 3..100
2..100
1..100
…..
…..
…...
till infinity
24
Decision and Loop
Control
Check Your Progress 3 Statements
1 4 times

2 65

3 3

5.9 FURTHER READINGS

1. The C programming language, Brain W. Kernighan, Dennis M. Ritchie, PHI.


2. Programming with C, Second Edition, Byron Gottfried, Tata McGraw Hill,
2003.
3. C,The Complete Reference, Fourth Edition, Herbert Schildt, Tata McGraw Hill,
4. 2002.
5. Computer Science: A Structured Programming Approach Using C, Second
Edition, Behrouz A. Forouzan, Richard F. Gilberg, Brooks/Cole Thomas
Learning, 2001.
o m
6. The C Primer, Leslie Hancock, Morris Krieger, Mc Graw Hill, 1983.

. c
o t
s p
o g
b l
6 .
0 0
i a
o n
p o
i l
u n
s

25
Control Statements,
Arrays and UNIT 6 ARRAYS
Functions
Structure
6.0 Introduction
6.1 Objectives
6.2 Array Declaration
6.2.1 Syntax of Array Declaration
6.2.2 Size Specification
6.3 Array Initialization
6.3.1 Initialization of Array Elements in the Declaration
6.3.2 Character Array Initialization
6.4 Subscript
6.5 Processing the Arrays
6.6 Multi-Dimensional Arrays
6.6.1 Multi-Dimensional Array Declaration
6.6.2 Initialization of Two-Dimensional Arrays
6.7 Summary
6.8
6.9
Solutions / Answers
Further Readings
o m
. c
6.0 INTRODUCTION
o t
p
C language provides four basic data types - int, char, float and double. We have learnt
s
o g
about them in Unit 3. These basic data types are very useful; but they can handle only
a limited amount of data. As programs become larger and more complicated, it

b l
becomes increasingly difficult to manage the data. Variable names typically become
longer to ensure their uniqueness. And, the number of variable names makes it

6 .
difficult for the programmer to concentrate on the more important task of correct
coding. Arrays provide a mechanism for declaring and accessing several data items

0 0
with only one identifier, thereby simplifying the task of data management.

i a
Many programs require the processing of multiple, related data items that have

o n
common characteristics like list of numbers, marks in a course, or enrolment numbers.
This could be done by creating several individual variables. But this is a hard and

p o tedious process. For example, suppose you want to read in five numbers and print

i l them out in reverse order. You could do it the hard way as:

u n main()
{

s int al,a2,a3,a4,a5;
scanf(“%d %d %d %d %d”,&a1,&a2,&a3,&a4,&a5);
printf(“%d %d %d %d %d”',a5,a4,a3,a2,a1);
}

Does it look good if the problem is to read in 100 or more related data items and print
them in reverse order? Of course, the solution is the use of the regular variable names
a1, a2 and so on. But to remember each and every variable and perform the operations
on the variables is not only tedious a job and disadvantageous too. One common
organizing technique is to use arrays in such situations. An array is a collection of
similar kind of data elements stored in adjacent memory locations and are referred to
by a single array-name. In the case of C, you have to declare and define array before
it can be used. Declaration and definition tell the compiler the name of the array, the
type of each element, and the size or number of elements.To explain it, let us consider
to store marks of five students. They can be stored using five variables as follows:
int ar1, ar2, ar3, ar4, ar5;

26
Now, if we want to do the same thing for 100 students in a class then one will find it Arrays
difficult to handle 100 variables. This can be obtained by using an array. An array
declaration uses its size in [ ] brackets. For above example, we can define an array as:
int ar [100];

where ar is defined as an array of size 100 to store marks of integer data-type. Each
element of this collection is called an array-element and an integer value called the
subscript is used to denote individual elements of the array. An ar array is the
collection of 200 consecutive memory locations referred as below:

2001 2003 2200

Figure 6.1: Representation of an array

In the above figure, as each integer value occupies 2 bytes, 200 bytes were allocated
in the memory.
o m
. c
This unit explains the use of arrays, types of arrays, declaration and initialization with
the help of examples.
o t
6.1 OBJECTIVES s p
o g
After going through this unit you will be able to:

b l


declare and use arrays of one dimension;
initialize arrays;
6 .


use subscripts to access individual array elements;
0 0

write programs involving arrays;
do searching and sorting; and
i a

n
handle multi-dimensional arrays.
o
6.2
p
ARRAY DECLARATIONo
i l
Before discussing how to declare an array, first of all let us look at the characteristic

u n
features of an array.

s• Array is a data structure storing a group of elements, all of which are of the same
data type.
• All the elements of an array share the same name, and they are distinguished
from one another with the help of an index.
• Random access to every element using a numeric index (subscript).
• A simple data structure, used for decades, which is extremely useful.
• Abstract Data type (ADT) list is frequently associated with the array data
structure.

The declaration of an array is just like any variable declaration with additional size
part, indicating the number of elements of the array. Like other variables, arrays must
be declared at the beginning of a function.

The declaration specifies the base type of the array, its name, and its size or
dimension. In the following section we will see how an array is declared:

27
Control Statements,
Arrays and
Functions
6.2.1 Syntax of Array Declaration
Syntax of array declaration is as follows:

data-type array_name [constant-size];


Data-type refers to the type of elements you want to store
Constant-size is the number of elements

The following are some of declarations for arrays:


int char [80];
float farr [500];
static int iarr [80];
char charray [40];

There are two restrictions for using arrays in C:

• The amount of storage for a declared array has to be specified at compile time
o m
before execution. This means that an array has a fixed size.
. c

t
The data type of an array applies uniformly to all the elements; for this reason, an
array is called a homogeneous data structure.
o
6.2.2 Size Specification
s p
o g
The size of an array should be declared using symbolic constant rather a fixed integer
l
quantity (The subscript used for the individual element is of are integer quantity). The
b
6 .
use of a symbolic constant makes it easier to modify a program that uses an array. All
reference to maximize the array size can be altered simply by changing the value of

constants).
0 0
the symbolic constant. (Please refer to Unit – 3 for details regarding symbolic

i a
To declare size as 50 use the following symbolic constant, SIZE, defined:

o n
#define SIZE 50
o
i lp The following example shows how to declare and read values in an array to store
marks of the students of a class.

u n Example 6.1
s Write a program to declare and read values in an array and display them.

/* Program to read values in an array*/

# include < stdio.h >


# define SIZE 5 /* SIZE is a symbolic constant */

main ( )
{
int i = 0; /* Loop variable */
int stud_marks[SIZE]; /* array declaration */

/* enter the values of the elements */


for( i = 0;i<SIZE;i++)
{
printf (“Element no. =%d”,i+1);
28 printf(“ Enter the value of the element:”);
scanf(“%d”,&stud_marks[i]); Arrays
}
printf(“\nFollowing are the values stored in the corresponding array elements: \n\n”);
for( i = 0; i<SIZE;i++)
{
printf(“Value stored in a[%d] is %d\n”i, stud_marks[i]);
}
}

OUTPUT:

Element no. =1 Enter the value of the element = 11


Element no. =2 Enter the value of the element = 12
Element no. =3 Enter the value of the element = 13
Element no. =4 Enter the value of the element = 14
Element no. =5 Enter the value of the element = 15

Following are the values stored in the corresponding array elements:

Value stored in a[0] is 11


o m
Value stored in a[1] is 12
. c
Value stored in a[2] is 13
Value stored in a[3] is 14
o t
Value stored in a[4] is 15

s p
6.3 ARRAY INITIALIZATION
o g
b l
Arrays can be initialized at the time of declaration. The initial values must appear in

6 .
the order in which they will be assigned to the individual array elements, enclosed

can be done.
0 0
within the braces and separated by commas. In the following section, we see how this

6.3.1
i a
Initialization of Array Elements in the Declaration

o n
The values are assigned to individual array elements enclosed within the braces and

p o
separated by comma. Syntax of array initialization is as follows:

i l
data type array-name [ size ] = {val 1, val 2, .......val n};

u n
val 1 is the value for the first array element, val 2 is the value for the second element,

sand val n is the value for the n array element. Note that when you are initializing the
values at the time of declaration, then there is no need to specify the size. Let us see
some of the examples given below:

int digits [10] = {1,2,3,4,5,6,7,8,9,10};

int digits[ ] = {1,2,3,4,5,6,7,8,9,10};

int vector[5] = {12,-2,33,21,13};

float temperature[10] ={ 31.2, 22.3, 41.4, 33.2, 23.3, 32.3, 41.1, 10.8, 11.3, 42.3};

double width[ ] = { 17.33333456, -1.212121213, 222.191345 };

int height[ 10 ] = { 60, 70, 68, 72, 68 };

29
Control Statements, 6.3.2 Character Array Initialisation
Arrays and
Functions
The array of characters is implemented as strings in C. Strings are handled differently
as far as initialization is concerned. A special character called null character ‘ \0 ’,
implicitly suffixes every string. When the external or static string character array is
assigned a string constant, the size specification is usually omitted and is
automatically assigned; it will include the ‘\0’character, added at end. For example,
consider the following two assignment statements:
char thing [ 3 ] = “TIN”;
char thing [ ] = “TIN”;

In the above two statements the assignments are done differently. The first statement
is not a string but simply an array storing three characters ‘T’, ‘I’ and ‘N’ and is same
as writing:
char thing [ 3 ] = {‘T’, ‘I’, ‘N’};

whereas, the second one is a four character string TIN\0. The change in the first
assignment, as given below, can make it a string.
o m
char thing [ 4 ] = “TIN”;
. c
Check Your Progress 1
o t
1.
s p
What happens if I use a subscript on an array that is larger than the number of
elements in the array?
……………………………………………………………………………………
o g
……………………………………………………………………………………
b l
2.
6 .
Give sizes of following arrays.

a.
0 0
char carray [ ]= “HELLO”;
b.
i
c.achar carray [ 5]= “HELLO”;
char carray [ ]={ ‘H’, ‘E’, ‘L’, ‘L’, ‘O’ };

o n ……………………………………………………………………………………

p o ……………………………………………………………………………………

i l 3. What happens if an array is used without initializing it?


……………………………………………………………………………………

u n ……………………………………………………………………………………

s 4. Is there an easy way to initialize an entire array at once?


……………………………………………………………………………………
……………………………………………………………………………………
5. Use a for loop to total the contents of an integer array called numbers with five
elements. Store the result in an integer called TOTAL.
……………………………………………………………………………………
……………………………………………………………………………………

6.4 SUBSCRIPT
To refer to the individual element in an array, a subscript is used. Refer to the
statement we used in the Example 6.1,

scanf (“ % d”, &stud_marks[ i]);


30
Subscript is an integer type constant or variable name whose value ranges from 0 to Arrays
SIZE - 1 where SIZE is the total number of elements in the array. Let us now see how
we can refer to individual elements of an array of size 5:
Consider the following declarations:

char country[ ] = “India”;


int stud[ ] = {1, 2, 3, 4, 5};

Here both arrays are of size 5. This is because the country is a char array and
initialized by a string constant “India” and every string constant is terminated by a
null character ‘\0’. And stud is an integer array. country array occupies 5 bytes of
memory space whereas stud occupies size of 10 bytes of memory space. The
following table: 6.1 shows how individual array elements of country and stud arrays
can be referred:

Table 6.1: Reference of individual elements

Element
no.
Subscript country array stud array

o m
1 0
Reference
country [0] ‘I’
Value Reference
stud [0] 1
Value
. c
2 1 country [1] ‘n’ stud [1] 2
o t
3
4
2
3
country [2]
country [3]
‘d’
‘i’
stud [2]
stud [3]
3
4
s p
5 4 country [4] ‘a’ stud [4] 5
o g
Example 6.2
b l
6 .
Write a program to illustrate how the marks of 10 students are read in an array and

0
then used to find the maximum marks obtained by a student in the class.

0
a
/* Program to find the maximum marks among the marks of 10 students*/
i
# include < stdio.h >
o n
# define SIZE 10

p o /* SIZE is a symbolic constant */

main ( )
{
i l
u n
sint i = 0;
int max = 0;
int stud_marks[SIZE]; /* array declaration */

/* enter the values of the elements */


for( i = 0;i<SIZE;i++)
{
printf (“Student no. =%d”,i+1);
printf(“ Enter the marks out of 50:”);
scanf(“%d”,&stud_marks[i]);
}

/* find maximum */
for (i=0;i<SIZE;i ++)
{
if (stud_marks[i]>max)
max = stud_marks[ i ];
}
31
Control Statements,
Arrays and
Functions
printf(“\n\nThe maximum of the marks obtained among all the 10 students is: %d
”,max);
}

OUTPUT

Student no. = 1 Enter the marks out of 50: 10


Student no. = 2 Enter the marks out of 50: 17
Student no. = 3 Enter the marks out of 50: 23
Student no. = 4 Enter the marks out of 50: 40
Student no. = 5 Enter the marks out of 50: 49
Student no. = 6 Enter the marks out of 50: 34
Student no. = 7 Enter the marks out of 50: 37
Student no. = 8 Enter the marks out of 50: 16
Student no. = 9 Enter the marks out of 50: 08
Student no. = 10 Enter the marks out of 50: 37

The maximum of the marks obtained among all the 10 students is: 49
o m
. c
6.5 PROCESSING THE ARRAYS
o t
s p
For certain applications the assignment of initial values to elements of an array is
required. This means that the array be defined globally (extern) or locally as a static
array.
o g
b l
Let us now see in the following example how the marks in two subjects, stored in two

the below example.


6 .
different arrays, can be added to give another array and display the average marks in

0 0
a
Example 6.3:

i
o n
Write a program to display the average marks of each student, given the marks in 2
subjects for 3 students.

o
lp /* Program to display the average marks of 3 students */

ni # include < stdio.h >

su
# define SIZE 3
main()
{
int i = 0;
float stud_marks1[SIZE]; /* subject 1array declaration */
float stud_marks2[SIZE]; /*subject 2 array declaration */
float total_marks[SIZE];
float avg[SIZE];

printf(“\n Enter the marks in subject-1 out of 50 marks: \n”);


for( i = 0;i<SIZE;i++)
{
printf(“Student no. =%d”,i+1);
printf(“ Enter the marks= “);
scanf(“%f”,&stud_marks1[i]);
}
printf(“\n Enter the marks in subject-2 out of 50 marks \n”);
for(i=0;i<SIZE;i++)
{
32
printf(“Student no. =%d”,i+1); Arrays
printf(“ Please enter the marks= “);
scanf(“%f”,&stud_marks2[i]);
}

for(i=0;i<SIZE;i++)
{
total_marks[i]=stud_marks1[i]+ stud_marks2[i];
avg[i]=total_marks[i]/2;
printf(“Student no.=%d, Average= %f\n”,i+1, avg[i]);
}
}

OUTPUT

Enter the marks in subject-1out of 50 marks:


Student no. = 1 Enter the marks= 23
Student no. = 2 Enter the marks= 35
Student no. = 3 Enter the marks= 42

o m
Enter the marks in subject-2 out of 50 marks:
. c
Student no. = 1 Enter the marks= 31
Student no. = 2 Enter the marks= 35
o t
Student no. = 3 Enter the marks= 40

s p
Student no. = 1 Average= 27.000000
Student no. = 2 Average= 35.000000
o g
Student no. = 3 Average= 41.000000
b l
6 .
Let us now write another program to search an element using the linear search.

Example 6.4
0 0
i a
Write a program to search an element in a given list of elements using Linear Search.

o n
/* Linear Search.*/

p o
i l
# include<stdio.h>
# define SIZE 05
n
main()
u
s{
int i = 0;
int j;
int num_list[SIZE]; /* array declaration */

/* enter elements in the following loop */

printf(“Enter any 5 numbers: \n”);


for(i = 0;i<SIZE;i ++)
{
printf(“Element no.=%d Value of the element=”,i+1);
scanf(“%d”,&num_list[i]);
}
printf (“Enter the element to be searched:”);
scanf (“%d”,&j);

/* search using linear search */


for(i=0;i<SIZE;i++)
33
Control Statements, {
Arrays and
Functions
if(j == num_list[i])
{
printf(“The number exists in the list at position: %d\n”,i+1);
break;
}
}
}

OUTPUT

Enter any 5 numbers:


Element no.=1 Value of the element=23
Element no.=2 Value of the element=43
Element no.=3 Value of the element=12
Element no.=4 Value of the element=8
Element no.=5 Value of the element=5
Enter the element to be searched: 8
The number exists in the list at position: 4

o m
Example 6.5
. c
o t
Write a program to sort a list of elements using the selection sort method

/* Sorting list of numbers using selection sort method*/


s p
#include <stdio.h>
o g
#define SIZE 5
b l
main()
6 .
{

0 0
int i;
i a
int j,min_pos,tmp;
/* Loop variable */

o n
int a[SIZE]; /* array declaration */

o
lp
/* enter the elements */

ni for(i=0;i<SIZE;i++)
{

su printf(“Element no.=%d”,i+1);
printf(“Value of the element: “);
scanf(“%d”,&a[i]);
}

/* Sorting by descending order*/

for (i=0;i<SIZE;i++)
{
min_pos = i;
for (j=i+1;j<SIZE;j++)
if (a[j] < a[min_pos])
min_pos = j;
tmp = a[i];
a[i] = a[min_pos];
a[min_pos] = tmp;
}

34
/* print the result */ Arrays

printf(“The array after sorting:\n”);


for(i=0;i<SIZE;i++)
printf("% d\n",a[i]);
}

OUTPUT

Element no. = 1 Value of the element: 23


Element no. =2 Value of the element: 11
Element no. = 3 Value of the element: 100
Element no. = 4 Value of the element: 42
Element no. = 5 Value of the element: 50

The array after sorting:


11
23
42
50
o m
100
. c
Check Your Progress 2
o t
1. Name the technique used to pass an array to a function.
s p
……………………………………………………………………………………

o g
2.
……………………………………………………………………………………
Is it possible to pass the whole array to a function?
b l
……………………………………………………………………………………
6 .
……………………………………………………………………………………
0 0
3.
a
List any two applications of arrays.
……………………………………………………………………………………
i
o n
……………………………………………………………………………………

p o
6.6
l
MULTI-DIMENSIONAL ARRAYS
i
u n
Suppose that you are writing a chess-playing program. A chessboard is an 8-by-8

sgrid. What data structure would you use to represent it? You could use an array that
has a chessboard-like structure, i.e. a two-dimensional array, to store the positions of
the chess pieces. Two-dimensional arrays use two indices to pinpoint an individual
element of the array. This is very similar to what is called "algebraic notation",
commonly used in chess circles to record games and chess problems.

In principle, there is no limit to the number of subscripts (or dimensions) an array can
have. Arrays with more than one dimension are called multi- dimensional arrays.
While humans cannot easily visualize objects with more than three dimensions,
representing multi-dimensional arrays presents no problem to computers. In practice,
however, the amount of memory in a computer tends to place limits on the size of an
array . A simple four-dimensional array of double-precision numbers, merely twenty
elements wide in each dimension, takes up 20^4 * 8, or 1,280,000 bytes of memory -
about a megabyte.

For exmaple, you have ten rows and ten columns, for a total of 100 elements. It’s
really no big deal. The first number in brackets is the number of rows, the second
number in brackets is the number of columns. So, the upper left corner of any grid 35
Control Statements, would be element [0][0]. The element to its right would be [0][1], and so on. Here is a
Arrays and
Functions
little illustration to help.

Three-dimensional arrays (and higher) are stored in the same way as the two-
dimensional ones. They are kept in computer memory as a linear sequence of
variables, and the last index is always the one that varies fastest (then the next-to-last,
and so on).

o m
6.6.1 Multi - Dimensional Array Declaration

. c
You can declare an array of two dimensions as follows:

o t
datatype array_name[size1][size2];

s p
o g
In the above example, variable_type is the name of some type of variable, such as int.
Also, size1 and size2 are the sizes of the array’s first and second dimensions,
l
respectively. Here is an example of defining an 8-by-8 array of integers, similar to a
b
6 .
chessboard. Remember, because C arrays are zero-based, the indices on each side of
the chessboard array run 0 through 7, rather than 1 through 8. The effect is the same: a

0
two-dimensional array of 64 elements.

0
a
int chessboard [8][8];
i
o n
To pinpoint an element in this grid, simply supply the indices in both dimensions.

o
lp
6.6.2 Initialisation of Two - Dimensional Arrays

ni
If you have an m x n array, it will have m * n elements and will require m*n*element-
size bytes of storage. To allocate storage for an array you must reserve this amount of

su
memory. The elements of a two-dimensional array are stored row wise. If table is
declared as:
int table [ 2 ] [ 3 ] = { 1,2,3,4,5,6 };

It means that element


table [ 0][0] = 1;
table [ 0][1] = 2;
table [ 0][2] = 3;
table [ 1][0] = 4;
table [ 1][1] = 5;
table [ 1][2] = 6;

The neutral order in which the initial values are assigned can be altered by including
the groups in { } inside main enclosing brackets, like the following initialization as
above:
int table [ 2 ] [ 3 ] = { {1,2,3},
{4,5,6} };
36
The value within innermost braces will be assigned to those array elements whose last Arrays
subscript changes most rapidly. If there are few remaining values in the row, they will
be assigned zeros. The number of values cannot exceed the defined row size.

int table [ 2 ] [ 3 ] = { { 1, 2, 3},{ 4}};

It assigns values as
table [0][0] = 1;
table [0][1] = 2;
table [0][2] = 3;
table [1][0] = 4;
table [1][1] = 0;
table [1][2] = 0

Remember that, C language performs no error checking on array bounds. If you define
an array with 50 elements and you attempt to access element 50 (the 51st element), or
any out of bounds index, the compiler issues no warnings. It is the programmer’s task
to check that all attempts to access or write to arrays are done only at valid array
indexes. Writing or reading past the end of arrays is a common programming bug and
is hard to isolate.
o m
. c
Check Your Progress 3

o t
1. Declare a multi-dimensioned array of floats called balances having three rows
and five columns.
s p
……………………………………………………………………………………

o g
2.
……………………………………………………………………………………

b l
Write a for loop to total the contents of the multi-dimensioned float array
balances.
6 .
……………………………………………………………………………………
……………………………………………………………………………………
0 0
3.
i a
Write a for loop which will read five characters (use scanf) and deposit them

o n
into the character based array words, beginning at element 0.
……………………………………………………………………………………

p o
……………………………………………………………………………………

i l
sun
6.7 SUMMARY
Like other languages, C uses arrays as a way of describing a collection of variables
with identical properties. The group has a single name for all its members, with the
individual member being selected by an index. We have learnt in this unit, the basic
purpose of using an array in the program, declaration of array and assigning values to
the arrays. All elements of the arrays are stored in the consecutive memory locations.
Without exception, all arrays in C are indexed from 0 up to one less than the bound
given in the declaration. This is very puzzling for a beginner. Watch out for it in the
examples provided in this unit. One important point about array declarations is that
they don't permit the use of varying subscripts. The numbers given must be constant
expressions which can be evaluated at compile time, not run time. As with other
variables, global and static array elements are initialized to 0 by default, and automatic
array elements are filled with garbage values. In C, an array of type char is used to
represent a character string, the end of which is marked by a byte set to 0 (also known
as a NULL character).

Whenever the arrays are passed to function their starting address is used to access rest
of the elements. This is called – Call by reference. Whatever changes are made to the
37
Control Statements, elements of an array in the function, they are also made available in the calling part.
Arrays and
Functions
The formal argument contains no size specification except for the rightmost
dimension. Arrays and pointers are closely linked in C. Multi-dimensional arrays are
simply arrays of arrays. To use arrays effectively it is a good idea to know how to use
pointers with them. More about the pointers can be learnt from Unit -10 (Block -3).

6.8 SOLUTIONS / ANSWERS


Check Your Progress 1
1. If you use a subscript that is out of bounds of the array declaration, the program
will probably compile and even run. However, the results of such a mistake can
be unpredictable. This can be a difficult error to find once it starts causing
problems. So, make sure you’re careful when initializing and accessing the
array elements.

2.
a) 6
b) 5
o m
c) 5

. c
3.
t
This mistake doesn’t produce a compiler error. If you don’t initialize an array,

o
there can be any value in the array elements. You might get unpredictable

know their content.


s p
results. You should always initialize the variables and the arrays so that you

o g
4.
l
Each element of an array must be initialized. The safest way for a beginner is to

b
initialize an array, either with a declaration, as shown in this chapter, or with a

6 .
for statement. There are other ways to initialize an array, but they are beyond
the scope of this Unit.

5.
0 0
Use a for loop to total the contents of an integer array which has five elements.

i a
Store the result in an integer called total.

o n for ( loop = 0, total = 0; loop < 5; loop++ )

p o total = total + numbers[loop];

i l Check Your Progress 2

sun 1.

2.
Call by reference.

It is possible to pass the whole array to a function. In this case, only the address
of the array will be passed. When this happens, the function can change the
value of the elements in the array.

3. Two common statistical applications that use arrays are:

• Frequency distributions: A frequency array show the number of elements


with an identical value found in a series of numbers. For example, suppose
we have taken a sample of 50 values ranging from 0 to 10. We want to
know how many of the values are 0, how many are 1, how many are 2 and
so forth up to 10. Using the arrays we can solve the problem easily .
Histogram is a pictorial representation of the frequency array. Instead of
printing the values of the elements to show the frequency of each number,
we print a histogram in the form of a bar chart.
• Random Number Permutations: It is a set of random numbers in which
no numbers are repeated. For example, given a random number permutation
of 5 numbers, the values of 0 to 5 would all be included with no duplicates.
38
Check Your Progress 3 Arrays

1. float balances[3][5];

2. for(row = 0, total = 0; row < 3; row++)


for(column = 0; column < 5; column++)
total = total + balances[row][column];

3. for(loop = 0; loop < 5; loop++)


scanf ("%c", &words[loop] );

6.9 FURTHER READINGS


1. The C Programming Language, Brain W. Kernighan, Dennis M. Ritchie, PHI.
2. C, The Complete Reference, Fourth Edition, Herbert Schildt, TMGH, 2002.

3. Computer Science – A Structured Programming Approach Using C, Behrouz A.


Forouzan, Richard F. Gilberg, Thomas Learning, Second edition, 2001.

o m
4. Programming with ANSI and TURBO C, Ashok N. Kamthane, Pearson
Education, 2002.
. c
o t
s p
o g
b l
6 .
0 0
i a
o n
p o
i l
u n
s

39
Control Statements,
Arrays and UNIT 7 STRINGS
Functions
Structure
7.0 Introduction
7.1 Objectives
7.2 Declaration and Initialization of Strings
7.3 Display of Strings Using Different Formatting Techniques
7.4 Array of Strings
7.5 Built-in String Functions and Applications
7.5.1 Strlen Function
7.5.2 Strcpy Function
7.5.3 Strcmp Function
7.5.4 Strcat Function
7.5.5 Strlwr Function
7.5.6 Strrev Function
7.5.7 Strspn Function
7.6
7.7
Other String Functions
Summary
o m
7.8
7.9
Solutions / Answers
Further Readings
. c
o t
7.0 INTRODUCTION
s p
o g
In the previous unit, we have discussed numeric arrays, a powerful data storage
method that lets you group a number of same-type data items under the same group

b l
name. Individual items, or elements, in an array are identified using a subscript after

6 .
the array name. Computer programming tasks that involve repetitive data processing
lend themselves to array storage. Like non-array variables, arrays must be declared

0 0
before they can be used. Optionally, array elements can be initialized when the array
is declared. In the earlier unit, we had just known the concept of character arrays
a
which are also called strings.
i
o n
String can be represented as a single-dimensional character type array. C language

p o does not provide the intrinsic string types. Some problems require that the characters
within a string be processed individually. However, there are many problems which

i l require that strings be processed as complete entities. Such problems can be


manipulated considerably through the use of special string oriented library functions.

u n Most of the C compilers include string library functions that allow string comparison,

s string copy, concatenation of strings etc. The string functions operate on null-
terminated arrays of characters and require the header <string.h>.The use of the some
of the string library functions are given as examples in this unit.

7.1 OBJECTIVES

After going through this unit, you will be able to:


• define, declare and initialize a string;
• discuss various formatting techniques to display the strings; and
• discuss various built-in string functions and their use in manipulation of strings.

7.2 DECLARATION AND INITIALIZATION OF STRINGS


Strings in C are group of characters, digits, and symbols enclosed in quotation marks
or simply we can say the string is declared as a “character array”. The end of the
string is marked with a special character, the ‘\0’ (Null character), which has the
40 decimal value 0. There is a difference between a character stored in memory and a
single character string stored in a memory. The character requires only one byte Strings
whereas the single character string requires two bytes (one byte for the character and
other byte for the delimiter).

Declaration of strings
A string in C is simply a sequence of characters. To declare a string, specify the data
type as char and place the number of characters in the array in square brackets after
the string name. The syntax is shown as below:
char string-name[size];

For example,

char name[20];
char address[25];
char city[15];

Initialization of strings

o m
The string can be initialized as follows:

. c
char name[ 8] = {‘P’, ‘R’, ‘O’, ‘G’, ‘R’, ‘A’, ‘M’, ‘\0’};

o t
Each character of string occupies 1 byte of memory (on 16 bit computing). The size of
character is machine dependent, and varies from 16 bit computers to 64 bit computers.
s p
The characters of strings are stored in the contiguous (adjacent) memory locations.
o g
1 byte 1 byte 1 byte 1 byte 1 byte 1 byte
b
1 byte
l 1 byte
P
1001
R
1002
O
1003
G
1004
R
1005
A

6
1006 . M
1007
\0
1008

0 0
The C compiler inserts the NULL (\0) character automatically at the end of the string.

i a
So initialization of the NULL character is not essential.

o n
You can set the initial value of a character array when you declare it by specifying a

p o
string literal. If the array is too small for the literal, the literal will be truncated. If the
literal (including its null terminator) is smaller than the array, then the final characters
l
in the array will be undefined. If you don’t specify the size of the array, but do specify
i
u n
a literal, then C will set the array to the size of the literal, including the null
terminator.

s char str[4] = {‘u’, ‘n’, ‘i’, ‘x’};


char str[5] = {‘u’, ‘n’, ‘i’, ‘x’, ‘\0’};
char str[3];
char str[ ] = “UNIX”;
char str[4] = “unix”;
char str[9] = “unix”;

All of the above declarations are legal. But which ones don’t work? The first one is a
valid declaration, but will cause major problems because it is not null-terminated. The
second example shows a correct null-terminated string. The special escape character
\0 denotes string termination. The fifth example suffers the size problem, the character
array ‘str’ is of size 4 bytes, but it requires an additional space to store ‘\0’. The
fourth example however does not. This is because the compiler will determine the
length of the string and automatically initialize the last character to a null-terminator.
The strings not terminated by a ‘\0’ are merely a collection of characters and are
called as character arrays.

41
Control Statements, String Constants
Arrays and
Functions
String constants have double quote marks around them, and can be assigned to char
pointers. Alternatively, you can assign a string constant to a char array - either with no
size specified, or you can specify a size, but don’t forget to leave a space for the null
character! Suppose you create the following two code fragments and run them:
/* Fragment 1 */
{
char *s;
s=hello”;
printf(“%s\n”,s);
}

/* Fragment 2 */

{
char s[100];
strcpy(s, “ hello”);
printf(“%s\n”,s);
o m
}

. c
o t
These two fragments produce the same output, but their internal behaviour is quite
different. In fragment 2, you cannot say s = "hello";. To understand the differences,
p
you have to understand how the string constant table works in C. When your program
s
o g
is compiled, the compiler forms the object code file, which contains your machine
code and a table of all the string constants declared in the program. In fragment 1, the

b l
statement s = "hello"; causes s to point to the address of the string hello in the string
constant table. Since this string is in the string constant table, and therefore technically

6 .
a part of the executable code, you cannot modify it. You can only point to it and use it
in a read-only manner. In fragment 2, the string hello also exists in the constant table,

0 0
so you can copy it into the array of characters named s. Since s is not an address, the

a
statement s="hello"; will not work in fragment 2. It will not even compile.

i
n
Example 7.1

o
p o Write a program to read a name from the keyboard and display message Hello onto
the monitor

i l
u n Program 7.1

s /*Program that reads the name and display the hello along with your name*/
#include <stdio.h>
main()
{
char name[10];
printf(“\nEnter Your Name : “);
scanf(“%s”, name);
printf(“Hello %s\n”, name);
}

OUTPUT

Enter Your Name : Alex


Hello Alex

In the above example declaration char name [10] allocates 10 bytes of memory space
(on 16 bit computing) to array name [ ]. We are passing the base address to scanf
function and scanf() function fills the characters typed at the keyboard into array until
enter is pressed. The scanf() places ‘\0’ into array at the end of the input. The printf()
42
function prints the characters from the array on to monitor, leaving the end of the Strings
string ‘\0’. The %s used in the scanf() and printf() functions is a format specification
for strings.

7.3 DISPLAY OF STRINGS USING DIFFERENT


FORMATTING TECHNIQUES
The printf function with %s format is used to display the strings on the screen. For
example, the below statement displays entire string:
printf(“%s”, name);

We can also specify the accuracy with which character array (string) is displayed. For
example, if you want to display first 5 characters from a field width of 15 characters,
you have to write as:
printf(“%15.5s”, name);

If you include minus sign in the format (e.g. % –10.5s), the string will be printed left
justified.
o m
. c
printf(“% -10.5s”, name);

o t
Example 7.2
s p
Write a program to display the string “UNIX” in the following format.
o g
U
b l
UN
UNI
6 .
UNIX
UNIX
0 0
UNI
UN
i a
U
o n
p o
/* Program to display the string in the above shown format*/

i l
# include <stdio.h>
n
main()
u
s{
int x, y;
static char string[ ] = “UNIX”;
printf(“\n”);
for( x=0; x<4; x++)
{
y = x + 1;
/* reserves 4 character of space on to the monitor and minus sign is for left
justified*/
printf(“%-4.*s \n”, y, string);

/* and for every loop the * is replaced by value of y */


/* y value starts with 1 and for every time it is incremented by 1 until it reaches to 4*/
}

for( x=3; x>=0; x- -)


{
y = x + 1;
printf(“%-4.*s \n”, y, string);
43
Control Statements, /* y value starts with 4 and for every time it is decrements by 1 until it reaches to 1*/
Arrays and
Functions
}
}

OUTPUT

U
UN
UNI
UNIX
UNIX
UNI
UN
U

7.4 ARRAY OF STRINGS


Array of strings are multiple strings, stored in the form of table. Declaring array of

o
strings is same as strings, except it will have additional dimension to store the numberm
of strings. Syntax is as follows:
char array-name[size][size];
. c
For example,
o t
char names[5][10];
s p
o g
b l
where names is the name of the character array and the constant in first square
brackets will gives number of string we are going to store, and the value in second

.
square bracket will gives the maximum length of the string.

6
Example 7.3
0 0
char
i a
names [3][10] = {“martin”, “phil”, “collins”};

o n
It can be represented by a two-dimensional array of size[3][10] as shown below:

o
lp 0 1 2 3 4 5 6 7 8 9

ni
m a r t i n \0

su
p h i l \0
c o l l i n s \0

Example 7.4

Write a program to initializes 3 names in an array of strings and display them on to


monitor
/* Program that initializes 3 names in an array of strings and display them on to
monitor.*/

#include <stdio.h>
main()
{
int n;
char names[3][10] = {“Alex”, “Phillip”, “Collins” };
for(n=0; n<3; n++)
printf(“%s \n”,names[n] ); }
44
Strings
OUTPUT

Alex
Phillip
Collins

Check Your Progress 1

1. Which of the following is a static string?

A. Static String;
B. “Static String”;
C. ‘Static String’;
D. char string[100];
…………………………………………………………………………………
…………………………………………………………………………………

o m
…………………………………………………………………………………

. c
2. Which character ends all strings?

o t
A. ‘.’
B. ‘ ‘
s p
C. ‘0’
D. ‘n’
og
…………………………………………………………………………………
b l
…………………………………………………………………………………
6 .
…………………………………………………………………………………
0 0
3.
a
What is the Output of the following programs?
i
(a) main()
o n
{

p o
char name[10] = “IGNOU”;

i l printf(“\n %c”, name[0]);


printf(“\n %s”, name);

u n }

s (b) main()
{
char s[ ] = “hello”;
int j = 0;
while ( s[j] != ‘\0’ )
printf(“ %c”,s[j++]);
}

(c) main()
{
char str[ ] = “hello”;
printf(“%10.2s”, str);
printf(“%-10.2s”, str);
}
…………………………………………………………………………………
…………………………………………………………………………………
………………………………………………………………………………… 45
Control Statements,
Arrays and
Functions

4 Write a program to read 'n' number of lines from the keyboard using a two-
dimensional character array (ie., strings).
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………

7.5 BUILT IN STRING FUNCTIONS AND


APPLICATIONS
The header file <string.h> contains some string manipulation functions. The following
is a list of the common string managing functions in C.

7.5.1 Strlen Function


o m
The syntax is as follows:
. c
The strlen function returns the length of a string. It takes the string name as argument.

n = strlen (str);
o t
s p
where str is name of the string and n is the length of the string, returned by strlen
function.
o g
Example 7. 5
b l
6 .
Write a program to read a string from the keyboard and to display the length of the

0
string on to the monitor by using strlen( ) function.

0
a
/* Program to illustrate the strlen function to determine the length of a string */

i
o n
#include <stdio.h>
#include <string.h>

p o main()

i l {
char name[80];

u n int length;
printf(“Enter your name: ”);
s gets(name);
length = strlen(name);
printf(“Your name has %d characters\n”, length);
}

OUTPUT

Enter your name: TYRAN


Your name has 5 characters

7.5.2 Strcpy Function


In C, you cannot simply assign one character array to another. You have to copy
element by element. The string library <string.h> contains a function called strcpy for
this purpose. The strcpy function is used to copy one string to another. The syntax is
as follows:
strcpy(str1, str2);
46 where str1, str2 are two strings. The content of string str2 is copied on to string str1.
Strings

Example 7.6
Write a program to read a string from the keyboard and copy the string onto the
second string and display the strings on to the monitor by using strcpy( ) function.
/* Program to illustrate strcpy function*/

#include <stdio.h>
#include <string.h>
main()
{
char first[80], second[80];
printf(“Enter a string: ”);
gets(first);
strcpy(second, first);
printf(“\n First string is : %s, and second string is: %s\n”, first, second);
}

o m
OUTPUT

. c
Enter a string: ADAMS
First string is: ADAMS, and second string is: ADAMS
o t
s p
7.5.3 Strcmp Function

o g
b l
The strcmp function in the string library function which compares two strings,
character by character and stops comparison when there is a difference in the ASCII

6 .
value or the end of any one string and returns ASCII difference of the characters that
is integer. If the return value zero means the two strings are equal, a negative value

second. The syntax is as follows:


0 0
means that first is less than second, and a positive value means first is greater than

n = strcmp(str1, str2);
i a
o n
where str1 and str2 are two strings to be compared and n is returned value of differed
characters.

p o
Example 7.7

i l
n
Write a program to compare two strings using string compare function.
u
s/* The following program uses the strcmp function to compare two strings. */

#include <stdio.h>
#include <string.h>
main()
{
char first[80], second[80];
int value;
printf(“Enter a string: ”);
gets(first);
printf(“Enter another string: ”);
gets(second);
value = strcmp(first,second);
if(value == 0)
puts(“The two strings are equal”);
else if(value < 0)
puts(“The first string is smaller ”);
else if(value > 0)
47
Control Statements, puts(“the first string is bigger”);
Arrays and
Functions
}

OUTPUT

Enter a string: MOND


Enter another string: MOHANT
The first string is smaller

7.5.4 Strcat Function

The strcat function is used to join one string to another. It takes two strings as
arguments; the characters of the second string will be appended to the first string. The
syntax is as follows:

strcat(str1, str2);
where str1 and str2 are two string arguments, string str2 is appended to string str1.

Example 7.8
o m
c
Write a program to read two strings and append the second string to the first string.
.
/* Program for string concatenation*/
o t
#include <stdio.h>
s p
#include <string.h>
main()
o g
{
b l
char first[80], second[80];

6
printf(“Enter a string:”); .
gets(first);
0 0
printf(“Enter another string: ”);
a
gets(second);
i
o n
strcat(first, second);
printf(“\nThe two strings joined together: %s\n”, first);

p o }

i l OUTPUT

sun Enter a string: BOREX


Enter another string: BANKS
The two strings joined together: BOREX BANKS

7.5.5 Strlwr Function


The strlwr function converts upper case characters of string to lower case characters.
The syntax is as follows:
strlwr(str1);
where str1 is string to be converted into lower case characters.

Example 7.9
Write a program to convert the string into lower case characters using in-built
function.
/* Program that converts input string to lower case characters */

#include <stdio.h>
48 #include <string.h>
main() Strings
{
char first[80];
printf("Enter a string: ");
gets(first);
printf("Lower case of the string is %s”, strlwr(first));
}

OUTPUT

Enter a string: BROOKES


Lower case of the string is brookes

7.5.6 Strrev Function


The strrev funtion reverses the given string. The syntax is as follows:
strrev(str);
where string str will be reversed.

o m
Example 7.9

. c
Write a program to reverse a given string.

o t
/* Program to reverse a given string */

s p
#include <stdio.h>
#include <string.h>
o g
main()
b l
{
char first[80];
6 .
printf(“Enter a string:”);
gets(first);
0 0
}
i a
printf(“\n Reverse of the given string is : %s ”, strrev(first));

o n
OUTPUT

p o
i l
Enter a string: ADANY
Reverse of the given string is: YNADA

u n
s7.5.7 Strspn Function
The strspn function returns the position of the string, where first string mismatches
with second string. The syntax is as follows:
n = strspn (first, second);
where first and second are two strings to be compared, n is the number of character
from which first string does not match with second string.

Example 7.10
Write a program, which returns the position of the string from where first string does
not match with second string.
/*Program which returns the position of the string from where first string does not
match with second string*/

#include <stdio.h>
#include <string.h>
main()
49
Control Statements, {
Arrays and
Functions
char first[80], second[80];
printf("Enter first string: “);
gets(first);
printf(“\n Enter second string: “);
gets(second);
printf(“\n After %d characters there is no match”,strspn(first, second));
}

OUTPUT

Enter first string: ALEXANDER


Enter second string: ALEXSMITH
After 4 characters there is no match

7.6 OTHER STRING FUNCTIONS


strncpy function

o m
The strncpy function same as strcpy. It copies characters of one string to another
string up to the specified length. The syntax is as follows:
. c
strncpy(str1, str2, 10);
o t
string str1.
s p
where str1 and str2 are two strings. The 10 characters of string str2 are copied onto

o g
stricmp function

b l
6 .
The stricmp function is same as strcmp, except it compares two strings ignoring the
case (lower and upper case). The syntax is as follows:

0 0
n = stricmp(str1, str2);

i a
strncmp function

o n
The strncmp function is same as strcmp, except it compares two strings up to a

p o specified length. The syntax is as follows:

i l n = strncmp(str1, str2, 10);

u n where 10 characters of str1 and str2 are compared and n is returned value of differed
characters.

s strchr function
The strchr funtion takes two arguments (the string and the character whose address is
to be specified) and returns the address of first occurrence of the character in the given
string. The syntax is as follows:
cp = strchr (str, c);
where str is string and c is character and cp is character pointer.

strset function
The strset funtion replaces the string with the given character. It takes two arguments
the string and the character. The syntax is as follows:
strset (first, ch);
where string first will be replaced by character ch.

strchr function
50
The strchr function takes two arguments (the string and the character whose address Strings
is to be specified) and returns the address of first occurrence of the character in the
given string. The syntax is as follows:
cp = strchr (str, c);
where str is string and c is character and cp is character pointer.

strncat function
The strncat function is the same as strcat, except that it appends upto specified
length. The syntax is as follows:
strncat(str1, str2,10);
where 10 character of the str2 string is added into str1 string.

strupr function
The strupr function converts lower case characters of the string to upper case
characters. The syntax is as follows:
strupr(str1);
o m
where str1 is string to be converted into upper case characters.

. c
strstr function
o t
The strstr function takes two arguments address of the string and second string as
inputs. And returns the address from where the second string starts in the first string.
s p
The syntax is as follows:
o g
cp = strstr (first, second);
b l
.
where first and second are two strings, cp is character pointer.

6
Check Your Progress 2

0 0
1.
a
Which of the following functions compares two strings?
A. compare();
i
B. stringcompare();
C. cmp();
o n
p
D. strcmp(); o
l
…………………………………………………………………………………

i
n
…………………………………………………………………………………

u
s…………………………………………………………………………………
2. Which of the following appends one string to the end of another?
A. append();
B. stringadd();
C. strcat();
D. stradd();
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
3. Write a program to concatenate two strings without using the strcat() function.
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………
4. Write a program to find string length without using the strlen() function.
51
Control Statements, …………………………………………………………………………………
Arrays and
Functions …………………………………………………………………………………
…………………………………………………………………………………
5. Write a program to convert lower case letters to upper case letters in a given
string without using strupp().
…………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………

7.7 SUMMARY
Strings are sequence of characters. Strings are to be null-terminated if you want to use
them properly. Remember to take into account null-terminators when using dynamic
memory allocation. The string.h library has many useful functions. Losing the ‘ \0’
character can lead to some very considerable bugs. Make sure you copy \0 when you
o m
copy strings. If you create a new string, make sure you put \0 in it. And if you copy

.
one string to another, make sure the receiving string is big enough to hold the sourcec
sure they end with \0.
o t
string, including \0. Finally, if you point a character pointer to some characters, make

String Functions Its Use


s p
strlen
o g
Returns number of characters in string.
strlwr
b l
Converts all the characters in the string into lower case characters
strcat
6 .
Adds one string at the end of another string
strcpy
0 0
Copies a string into another
strcmp
i a Compares two strings and returns zero if both are equal.

on
strdup Duplicates a string

p o strchr
strstr
Finds the first occurrence of given character in a string
Finds the first occurrence of given string in another string
l
ni
strset Sets all the characters of string to given character or symbol

su
strrev Reverse a string

7.8 SOLUTIONS / ANSWERS


Check Your Progress 1

1. B

2. C

3. (a) I
IGNOU

(b) hello

(c) hehe

52
Check Your Progress 2 Strings

1. D

2. C

3. /* Program to concatenate two strings without using the strcat() function*/

# include<string.h>
# include <stdio.h>
main()
{
char str1[10];
char str2[10];
char output_str[20];
int i=0, j=0, k=0;
printf(" Input the first string: ");
o m
gets(str1);
printf("\nInput the second string: ");
. c
gets(str2);
while(str1[i] != ‘\0’)
o t
output_str[k++] = str1[i++];
s p
while(str2[j] != '\0')
output_str[k++] = str2[j++];
o g
output_str[k] = ‘\0’;
puts(output_str);
b l
}
6 .
0 0
4.
a
/* Program to find the string length without using the strlen() funtion */

i
# include<stdio.h>
# include<string.h>
o n
main()
p o
{

i l
char string[60];

u n int len=0, i=0;


printf(“ Input the string : ”);
s gets(string);
while(string[i++] != ‘\0’)
len ++;
printf(“Length of Input String = %d”, len);
getchar();
}

5. /* Program to convert the lower case letters to upper case in a given string
without using strupp() function*/

#include<stdio.h>
main()
{
int i= 0; char source[10], destination[10];
gets(source);
while( source[i] != ‘\0’)
{
if((source[i]>=97) && (source[i]<=122))
53
Control Statements,
Arrays and
Functions
destination[i]=source[i]-32;
else
destination[i]=source[i];
i++;
}
destination[i]= ‘ \0 ’;
puts(destination);
}

7.9 FURTHER READINGS


1. The C programming language, Brain W. Kernighan, Dennis M. Ritchie, PHI.
2. Programming with ANSI and Turbo C, Ashok N. Kamthane, Pearson
Education, 2002.
3.
4.
Computer Programming in C, Raja Raman. V, 2002, PHI.

o m
C,The Complete Reference, Fourth Edition, Herbert Schildt, Tata McGraw Hill,

5.
2002.
. c
Computer Science A structured Programming Approach Using C, Behrouz A.

o t
Forouzan, Richard F. Gilberg, Brooks/Cole Thomas Learning, Second Edition,
2001.

s p
o g
b l
6 .
0 0
i a
o n
p o
i l
u n
s

54
Functions
UNIT 8 FUNCTIONS
Structure
8.0 Introduction
8.1 Objectives
8.2 Definition of a Function
8.3 Declaration of a Function
8.4 Function Prototypes
8.5 The Return Statement
8.6 Types of Variables and Storage Classes
8.6.1 Automatic Variables
8.6.2 External Variables
8.6.3 Static Variables
8.6.4 Register Variables
8.7 Types of Function Invoking
8.8 Call by Value
8.9 Recursion
o m
8.10
8.11
Summary
Solutions / Answers
. c
8.12 Further Readings

o t
8.0 INTRODUCTION
s p
o
To make programming simple and easy to debug, we break a larger program into g
l
smaller subprograms which perform ‘well defined tasks’. These subprograms are
b
.
called functions. So far we have defined a single function main ( ).

6
0 0
After reading this unit you will be able to define many other functions and the main( )
function can call up these functions from several different places within the program,
to carry out the required processing.

i a
o n
Functions are very important tools for Modular Programming, where we break large
programs into small subprograms or modules (functions in case of C). The use of

p o
functions reduces complexity and makes programming simple and easy to understand.

i l
In this unit, we will discuss how functions are defined and how are they accessed from

u n
the main program? We will also discuss various types of functions and how to invoke
them. And finally you will learn an interesting and important programming technique
sknown as Recursion, in which a function calls within itself.

8.1 OBJECTIVES
After going through this unit, you will learn:

• the need of functions in the programming;


• how to define and declare functions in ‘C’ Language;
• different types of functions and their purpose;
• how the functions are called from other functions;
• how data is transferred through parameter passing, to functions and the Return
statement;
• recursive functions; and
• the concept of ‘Call by Value’ and its drawbacks.

55
Control Statements,
Arrays and
Functions

8.2 DEFINTION OF A FUNCTION


A function is a self- contained block of executable code that can be called from any
other function .In many programs, a set of statements are to be executed repeatedly at
various places in the program and may with different sets of data, the idea of functions
comes in mind. You keep those repeating statements in a function and call them as
and when required. When a function is called, the control transfers to the called
function, which will be executed, and then transfers the control back to the calling
function (to the statement following the function call). Let us see an example as
shown below:

Example 8.1

/* Program to illustrate a function*/

#include <stdio.h>
o m
main ()
{
. c
void sample( );
printf(“\n You are in main”);
o t
}
s p
void sample( )
o g
{
printf(“\n You are in sample”);
b l
}

6 .
OUTPUT
0 0
i a
You are in sample

n
You are in main

o
p o Here we are calling a function sample ( ) through main( ) i.e. control of execution
transfers from main( ) to sample( ) , which means main( ) is suspended for some time

i l and sample( ) is executed. After its execution the control returns back to main( ), at

u n the statement following function call and the execution of main( ) is resumed.

s The syntax of a function is:


return data type function_name (list of arguments)
{
datatype declaration of the arguments;
executable statements;
return (expression);
}

where,
• return data type is the same as the data type of the variable that is returned by the
function using return statement.
• a function_name is formed in the same way as variable names / identifiers are
formed.
• the list of arguments or parameters are valid variable names as shown below,
separated by commas: (data type1 var1,data type2 var2,…….. data type n var n)
for example (int x, float y, char z).
• arguments give the values which are passed from the calling function.
56
• the body of function contains executable statements. Functions
• the return statement returns a single value to the calling function.

Example 8.2

Let us write a simple function that calculates the square of an integer.

/*Program to calculate the square of a given integer*/

/* square( ) function */
{
int square (int no) /*passing of argument */
int result ; /* local variable to function square */
result = no*no;
return (result); /* returns an integer value */
}

/*It will be called from main()as follows */


main( )
{
o m
int n ,sq; /* local variable to function main */
. c
printf (“Enter a number to calculate square value”);
scanf(“%d”,&n);
o t
sq=square(n); /* function call with parameter passing */
printf (”\nSquare of the number is : %d”, sq);
s p
} /* program ends */
o g
b l
OUTPUT

6 .
Enter a number to calculate square value : 5
Square of the number is : 25
0 0
i a
8.3
o n
DECLARATION OF A FUNCTION

p o
l
As we have mentioned in the previous section, every function has its declaration and
i
u n
function definition. When we talk of declaration only, it means only the function
name, its argument list and return type are specified and the function body or

sdefinition is not attached to it. The syntax of a function declaration is:

return data type function_name(list of arguments);

For example,

int square(int no);


float temperature(float c, float f);

We will discuss the use of function declaration in the next section.

8.4 FUNCTION PROTOTYPES

In Example 8.1 for calculating square of a given number, we have declared function
square( ) before main( ) function; this means before coming to main( ), the compiler
knows about square( ), as the compilation process starts with the first statement of
57
Control Statements, any program. Now suppose, we reverse the sequence of functions in this program i.e.,
Arrays and
Functions
writing the main( ) function and later on writing the square( ) function, what
happens ? The “C” compiler will give an error. Here the introduction of concept of
“function prototypes” solves the above problem.

Function Prototypes require that every function which is to be accessed should be


declared in the calling function. The function declaration, that will be discussed
earlier, will be included for every function in its calling function . Example 8.2 may
be modified using the function prototype as follows:

Example 8.3

/*Program to calculate the square of a given integer using the function prototype*/
#include <stdio.h>
main ( )
{
int n , sq ;
int square (int ) ; /* function prototype */
printf (“Enter a number to calculate square value”);
scanf(“%d”,&n);
o m
sq = square(n); /* function call with parameter passing */
. c
printf (“\nSsquare of the number is : %d”, sq);
}
o t
/* square function */
s p
int square (int no)
{ g
/*passing of argument */
o
int result ;
b l
/* local variable to function square */

6.
result = no*no;
return (result); /* returns an integer value */
}

0 0
OUTPUT
i a
o n
Enter a number to calculate square value : 5

p o Square of the number is: 25

i l Points to remember:

u n • Function prototype requires that the function declaration must include the return
type of function as well as the type and number of arguments or parameters
s passed.
• The variable names of arguments need not be declared in prototype.
• The major reason to use this concept is that they enable the compiler to check if
there is any mismatch between function declaration and function call.

Check Your Progress 1


(1) Write a function to multiply two integers and display the product.
…………………………………………………………………………………
…………………………………………………………………………………
(2) Modify the above program, by introducing function prototype in the main
function.
…………………………………………………………………………………
…………………………………………………………………………………

58
Functions
8.5 THE return STATEMENT
If a function has to return a value to the calling function, it is done through the return
statement. It may be possible that a function does not return any value; only the
control is transferred to the calling function. The syntax for the return statement is:
return (expression);

We have seen in the square( ) function, the return statement, which returns an integer
value.

Points to remember:

• You can pass any number of arguments to a function but can return only one
value at a time.

For example, the following are the valid return statements

(a)
(b)
return (5);
return (x*y);
o m
. c
For example, the following are the invalid return statements
(c) return (2, 3);
o t
(d) return (x, y);

s p
• If a function does not return anything, void specifier is used in the function
declaration.
o g
b l
For example:

6 .
void square (int no)
{
0 0
int sq;
sq = no*no;
i a
n
printf (“square is %d”, sq);
o
}

p o

l
All the function’s return type is by default is “int”, i.e. a function returns an

i
integer value, if no type specifier is used in the function declaration.

u n Some examples are:


s (i) square (int no); /* will return an integer value */

(ii) int square (int no); /* will return an integer value */

(iii) void square (int no); /* will not return anything */

• What happens if a function has to return some value other than integer? The
answer is very simple: use the particular type specifier in the function
declaration.

For example consider the code fragments of function definitions below:


1) Code Fragment - 1

char func_char( …….. )


{
char c; 59
Control Statements, …………….
Arrays and
Functions
…………….
……………
}

2) Code Fragment - 2
float func_float (……..)
{
float f;
…………..
…………..
…………..
return(f);
}

Thus from the above examples, we see that you can return all the data types
from a function, the only condition being that the value returned using return
statement and the type specifier used in function declaration should match.

o m
• A function can have many return statements. This thing happens when some
. c
condition based returns are required.

o t
For example,

s p
/*Function to find greater of two numbers*/
int greater (int x, int y)
o g
{
b l
if (x>y)
return (x);
6 .
else

0 0
return (y);
}
i a
o on
• And finally, with the execution of return statement, the control is transferred to
the calling function with the value associated with it.

l p In the above example if we take x = 5 and y = 3, then the control will be

n i transferred to the calling function when the first return statement will be

su encountered, as the condition (x > y) will be satisfied. All the remaining


executable statements in the function will not be executed after this returning.

Check Your Progress 2


1. Which of the following are valid return statements?

a) return (a);
b) return (z,13);
c) return (22.44);
d) return;
e) return (x*x, y*y);

…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………

60
Functions

8.6 TYPES OF VARIABLES AND STORAGE CLASSES


In a program consisting of a number of functions a number of different types of
variables can be found.

Global vs. Static variables: Global variables are recognized through out the program
whereas local valuables are recognized only within the
function where they are defined.

Static vs. Dynamic variables: Retention of value by a local variable means, that in
static, retention of the variable value is lost once the
function is completely executed whereas in certain
conditions the value of the variable has to be retained
from the earlier execution and the execution retained.

The variables can be characterized by their data type and by their storage class. One
way to classify a variable is according to its data type and the other can be through its
storage class. Data type refers to the type of value represented by a variable whereas
o m
storage class refers to the permanence of a variable and its scope within the program
i.e. portion of the program over which variable is recognized.
. c
Storage Classes
o t
There are four different storage classes specified in C:
s p
1. Auto (matic) 2. Extern (al)
o g
3. Static 4. Register
b l
6 .
The storage class associated with a variable can sometimes be established by the

variables declarations.
0 0
location of the variable declaration within the program or by prefixing keywords to

For example: auto int a, b;


i a
static int
o n a, b;

p o
extern float f;

8.6.1
l
Automatic Variables
i
u n
The variables local to a function are automatic i.e., declared within the function. The
scope of lies within the function itself. The automatic defined in different functions,
seven if they have same name, are treated as different. It is the default storage class for
variables declared in a function.

Points to remember:

• The auto is optional therefore there is no need to write it.


• All the formal arguments also have the auto storage class.
• The initialization of the auto-variables can be done:

• in declarations
• using assignment expression in a function

• If not initialized the unpredictable value is defined.


• The value is not retained after exit from the program.

Let us study these variables by a sample program given below:

61
Control Statements, Example 8.4
Arrays and
Functions /* To print the value of automatic variables */

# include <stdio.h>
main ( int argc, char * argv[ ])
{
int a, b;
double d;
printf(“%d”, argc);
a = 10;
b = 5;
d = (b * b) – (a/2);
printf(“%d, %d, %f”, a, b, d);
}

All the variables a, b, d, argc and argv [ ] have automatic storage class.

8.6.2 External (Global) Variables

o m
These are not confined to a single function. Their scope ranges from the point of

.
declaration to the entire remaining program. Therefore, their scope may be the entirec
program or two or more functions depending upon where they are declared.
o t
Points to remember:
s p

o g
These are global and can be accessed by any function within its scope.


b l
Therefore value may be assigned in one and can be written in another.
There is difference in external variable definition and declaration.

.
External Definition is the same as any variable declaration:
6

0 0
Usually lies outside or before the function accessing it.


i a
It allocates storage space required.
Initial values can be assigned.

o
• n The external specifier is not required in external variable definition.
A declaration is required if the external variable definition comes after the
o function definition.

i lp •

A declaration begins with an external specifier.
Only when external variable is defined is the storage space allocated.

u n • External variables can be assigned initial values as a part of variable definitions,


but the values must be constants rather than expressions.
s • If initial value is not included then it is automatically assigned a value of zero.

Let us study these variables by a sample program given below:


Example 8.5
/* Program to illustrate the use of global variables*/
# include <stdio.h>
int gv; /*global variable*/
main ( )
{
void function1(); /*function declaration*/
gv = 10;
printf (“%d is the value of gv before function call\n”, gv);
function1( );
printf (“%d is the value of gv after function call\n”, gv);
}
62
Functions
void function1 ( )
{
gv = 15: }

OUTPUT

10 is the value of gv before function call


15 is the value of gv after function call

8.6.3 Static Variables


In case of single file programs static variables are defined within functions and
individually have the same scope as automatic variables. But static variables retain
their values throughout the execution of program within their previous values.

Points to remember:

• The specifier precedes the declaration. Static and the value cannot be accessed
outside of their defining function.
o m
• The static variables may have same name as that of external variables but the
local variables take precedence in the function. Therefore external variables
. c

maintain their independence with locally defined auto and static variables.
o t

Initial value is expressed as the constant and not expression.

s
Zeros are assigned to all variables whose declarations do not include explicit p

initial values. Hence they always have assigned values.
Initialization is done only is the first execution.
o g
Let us study this sample program to print value of a static variable:
b l
6 .
Example 8.6

0 0
a
/* Program to illustrate the use of static variable*/

i
#include <stdio.h>
o n
main()
p o
{

i l
int call_static();

u n
int i,j;
i=j=0;
sj = call_static();
printf(“%d\n”,j);
j = call_static ();
printf(“%d\n”,j);
j = call_static();
printf(“%d\n”,j);
}

int call_static()
{
static int i=1;
int j;
j = i;
i++;
return(j);
}

63
Control Statements,
Arrays and
Functions
OUTPUT
1
2
3

This is because i is a static variable and retains its previous value in next execution of
function call_static( ). To remind you j is having auto storage class. Both functions
main and call_static have the same local variable i and j but their values never get
mixed.

8.6.4 Register Variables


Besides three storage class specifications namely, Automatic, External and Static,
there is a register storage class. Registers are special storage areas within a
computer’s CPU. All the arithmetic and logical operations are carried out with these
registers.

For the same program, the execution time can be reduced if certain values can be
stored in registers rather than memory. These programs are smaller in size (as few
o m
c
instructions are required) and few data transfers are required. The reduction is there in
.
by register reserved word as follows:
o t
machine code and not in source code. They are declared by the proceeding declaration

register int m;
s p
Points to remember:
o g

b l
These variables are stored in registers of computers. If the registers are not

6 .
available they are put in memory.


0 0
Usually 2 or 3 register variables are there in the program.
Scope is same as automatic variable, local to a function in which they are


i a
declared.


o n Address operator ‘&’ cannot be applied to a register variable.
If the register is not available the variable is though to be like the automatic

o variable.

lp
• Usually associated integer variable but with other types it is allowed having
same size (short or unsigned).

ni •

Can be formal arguments in functions.

su
Pointers to register variables are not allowed.
• These variables can be used for loop indices also to increase efficiency.

8.7 TYPES OF FUNCTION INVOKING


We categorize a function’s invoking (calling) depending on arguments or parameters
and their returning a value. In simple words we can divide a function’s invoking into
four types depending on whether parameters are passed to a function or not and
whether a function returns some value or not.

The various types of invoking functions are:

• With no arguments and with no return value.


• With no arguments and with return value
• With arguments and with no return value
• With arguments and with return value.

Let us discuss each category with some examples:


64
TYPE 1: With no arguments and have no return value Functions

As the name suggests, any function which has no arguments and does not return any
values to the calling function, falls in this category. These type of functions are
confined to themselves i.e. neither do they receive any data from the calling function
nor do they transfer any data to the calling function. So there is no data
communication between the calling and the called function are only program control
will be transferred.

Example 8.7

/* Program for illustration of the function with no arguments and no return value*/

/* Function with no arguments and no return value*/

#include <stdio.h>
main()
{
void message();
printf(“Control is in main\n”);
o m
message(); /* Type 1 Function */
printf(“Control is again in main\n”);
. c
}
o t
void message()
s p
{
printf(“Control is in message function\n”);
o g
} /* does not return anything */
b l
OUTPUT
6 .
Control is in main
0 0
Control is in message function
Control is again in main
i a
o n
TYPE 2: With no arguments and with return value

p o
Suppose if a function does not receive any data from calling function but does send

i l
some value to the calling function, then it falls in this category.

u n
Example 8.8

sWrite a program to find the sum of the first ten natural numbers.

/* Program to find sum of first ten natural numbers */

#include <stdio.h>

int cal_sum()
{
int i, s=0;
for (i=0; i<=10; i++)
s=s + i;
return(s); /* function returning sum of first ten natural numbers */
}

main()
{
int sum;
65
Control Statements, sum = cal_sum();
Arrays and
Functions
printf(“Sum of first ten natural numbers is % d\n”, sum);
}

OUTPUT

Sum of first ten natural numbers is 55

TYPE 3: With Arguments and have no return value


If a function includes arguments but does not return anything, it falls in this
category. One way communication takes place between the calling and the called
function.

Before proceeding further, first we discuss the type of arguments or parameters here.
There are two types of arguments:

• Actual arguments
• Formal arguments

o m
Let us take an example to make this concept clear:

. c
Example 8.9

o t
p
Write a program to calculate sum of any three given numbers.

s
#include <stdio.h>
o g
main()
b l
{
int a1, a2, a3;
6 .
0 0
void sum(int, int, int);
printf(“Enter three numbers: “);

i a
scanf (“%d%d%d”,&a1,&a2,&a3);
sum (a1,a2,a3); /* Type 3 function */

o
} n
p o
i l /* function to calculate sum of three numbers */
void sum (int f1, int f2, int f3)

u n {

s int s;
s = f1+ f2+ f3;
printf (“\nThe sum of the three numbers is %d\n”,s);
}

OUTPUT

Enter three numbers: 23 34 45


The sum of the three numbers is 102

Here f1, f2, f3 are formal arguments and a1, a2, a3 are actual arguments.
Thus we see in the function declaration, the arguments are formal arguments, but
when values are passed to the function during function call, they are actual arguments.

Note: The actual and formal arguments should match in type, order and number

66
TYPE 4: With arguments function and with return value Functions

In this category two-way communication takes place between the calling and called
function i.e. a function returns a value and also arguments are passed to it. We modify
above Example according to this category.

Example 8.10
Write a program to calculate sum of three numbers.

/*Program to calculate the sum of three numbers*/

#include <stdio.h>
main ( )
{
int a1, a2, a3, result;
int sum(int, int, int);
printf(“Please enter any 3 numbers:\n”);
scanf (“%d %d %d”, & a1, &a2, &a3);
result = sum (a1,a2,a3); /* function call */
o m
printf (“Sum of the given numbers is : %d\n”, result);
}
. c
/* Function to calculate the sum of three numbers */
o t
int sum (int f1, int f2, int f3)
s p
{
return(f1+ f2 + f3); /* function returns a value */
o g
}

b l
OUTPUT
Please enter any 3 numbers:
6 .
345
0 0
Sum of the given numbers is: 12

i a
8.8 CALL BY VALUE
o n
p o
So far we have seen many functions and also passed arguments to them, but if we

i l
observe carefully, we will see that we have always created new variables for
arguments in the function and then passed the values of actual arguments to them.
n
Such function calls are called “call by value”.
u
sLet us illustrate the above concept in more detail by taking a simple function of
multiplying two numbers:
Example 8.11
Write a program to multiply the two given numbers

#include <stdio.h>
main()
{
int x, y, z;
int mul(int, int);
printf (“Enter two numbers: \n”);
scanf (“%d %d”,&x,&y);
z= mul(x, y); /* function call by value */
printf (“\n The product of the two numbers is : %d”, z);
}
67
Control Statements, /* Function to multiply two numbers */
Arrays and
Functions
int mul(int a, int b)
{
int c;
c =a*b;
return(c); }

OUTPUT

Enter two numbers:


23 2
The product of two numbers is: 46

Now let us see what happens to the actual and formal arguments in memory.
main( ) function mul( ) function

x a

2 2
o m
y b
. c
The variables are local to the

3 3 t
mul ( ) function which are

o
created in memory with the

s p function call and are


destroyed with the return to
z c

o g the called function

6 6

b l
6 .
0 0
i a
Variables local to main( ) function Variables local to mul( ) function

o n
What are meant by local variables? The answer is local variables are those which can

p o be used only by that function.

i l Advantages of Call by value:

u n The only advantage is that this mechanism is simple and it reduces confusion

s and complexity.

Disadvantages of Call by value:


As you have seen in the above example, there is separate memory allocation for
each of the variable, so unnecessary utilization of memory takes place.

The second disadvantage, which is very important from programming point of


view, is that any changes made in the arguments are not reflected to the calling
function, as these arguments are local to the called function and are destroyed
with function return.

Let us discuss the second disadvantage more clearly using one example:
Example 8.12
Write a program to swap two values.

68
/*Program to swap two values*/ Functions

#include <stdio.h>
main ( )
{
int x = 2, y = 3;
void swap(int, int);

printf (“\n Values before swapping are %d %d”, x, y);


swap (x, y);
printf (“\n Values after swapping are %d %d”, x, y);
}

/* Function to swap(interchange) two values */


void swap( int a, int b )
{
int t;
t = a;
a = b;
b = t;
o m
}
. c
OUTPUT
o t
Values before swap are 2 3
s p
Values after swap are 2 3
o g
But the output should have been 3 2. So what happened?
b l
x a
6. a
2 2
00 3

ia t 2
y b b t

o n
3

p o 3 2

i l
Values passing from main ( ) to swap() function Variables in swap ( ) function

n
Here we observe that the changes which takes place in argument variables are not
u
sreflected in the main() function; as these variables namely a, b and t will be destroyed
with function return.

• All these disadvantages will be removed by using “call by reference”, which will
be discussed with the introduction of pointers in UNIT 11.

Check Your Progress 3

1. Write a function to print Fibonacci series upto ‘n’ terms 1,1,2,3,…..n


……………………………………………………………………………………
……………………………………………………………………………………
2. Write a function power (a, b) to calculate ab
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
69
Control Statements,
Arrays and 8.9 RECURSION
Functions
Within a function body, if the function calls itself, the mechanism is known as
‘Recursion’ and the function is known as ‘Recursive function’. Now let us study this
mechanism in detail and understand how it works.
• As we see in this mechanism, a chaining of function calls occurs, so it is
necessary for a recursive function to stop somewhere or it will result into
infinite callings. So the most important thing to remember in this mechanism is
that every “recursive function” should have a terminating condition.

• Let us take a very simple example of calculating factorial of a number, which


we all know is computed using this formula 5! = 5*4*3*2*1

• First we will write non – recursive or iterative function for this.

Example 8.13
Write a program to find factorial of a number

o m
#include <stdio.h>
main ()
. c
{
o t
int n, factorial;
int fact(int);
s p
printf (“Enter any number:\n” );
scanf ("%d", &n);
o g
b
printf (“Factorial is %d\n”, factorial);l
factorial = fact ( n); /* function call */

}
6 .
0 0
/* Non recursive function of factorial */

i a
int fact (int n)
{
o n
int res = 1, i;

p o for (i = n; i >= 1; i--)

i l res = res * i;
return (res);

u n }

s OUTPUT

Enter any number: 5


Factorial is 120

How it works?

Suppose we call this function with n = 5

Iterations:
1. i= 5 res = 1*5 = 5
2. i= 4 res = 5*4 = 20
3. i= 3 res = 20*4 = 60
4. i= 2 res = 60*2 = 120
5. i= 1 res = 120*1 = 120

70
Now let us write this function recursively. Before writing any function recursively, Functions
we first have to examine the problem, that it can be implemented through recursion.

For instance, we know n! = n* (n − 1)! (Mathematical formula)

Or fact (n) = n*fact (n-1)


Or fact (5) = 5*fact (4)

That means this function calls itself but with value of argument decreased by ‘1’.

Example 8.14
Modify the program 8 using recursion.

/*Program to find factorial using recursion*/


#include<stdio.h>
main()
{
int n, factorial;
int fact(int);
o m
printf("Enter any number: \n" );
scanf("%d",&n);
. c
factorial = fact(n); /*Function call */
o t
printf ("Factorial is %d\n", factorial); }

s p
/* Recursive function of factorial */
int fact(int n)
o g
{
int res;
b l
if(n == 1) /* Terminating condition */
6 .
else
return(1);

0 0
res = n*fact(n-1);
return(res); } a
/* Recursive call */

i
o n
OUTPUT

p
Enter any number: 5 o
l
Factorial is 120

i
n
How it works?
u
sSuppose we will call this function with n = 5

fact (5) 120

5*fact(4) 5*24
Returning Process

4*fact(3) 4*6(=24)

3*fact(2) 3*2*1(= 6)

2*fact(1) 2* 1(=2)

2*1 (It terminates here)


71
Control Statements,
Arrays and
Functions
Thus a recursive function first proceeds towards the innermost condition, which is the
termination condition, and then returns with the value to the outermost call and
produces result with the values from the previous return.

Note: This mechanism applies only to those problems, which repeats itself. These
types of problems can be implemented either through loops or recursive functions,
which one is better understood to you.

Check Your Progress 4

1. Write recursive functions for calculating power of a number ‘a’ raised by


another number ‘b’ i.e. ab
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………............
o m
8.10 SUMMARY
. c
o t
s p
In this unit, we learnt about “Functions”: definition, declaration, prototypes, types,
function calls datatypes and storage classes, types function invoking and lastly

o g
Recursion. All these subtopics must have given you a clear idea of how to create and
call functions from other functions, how to send values through arguments, and how

b l
to return values to the called function. We have seen that the functions, which do not
return any value, must be declared as “void”, return type. A function can return only

6 .
one value at a time, although it can have many return statements. A function can

0
return any of the data type specified in ‘C’.

0
i a
Any variable declared in functions are local to it and are created with function call
and destroyed with function return. The actual and formal arguments should match in
n
type, order and number. A recursive function should have a terminating condition i.e.
o
p o function should return a value instead of a repetitive function call.

i l 8.11 SOLUTIONS / ANSWERS

u n Check Your Progress 1

s 1. /* Function to multiply two integers */


int mul( int a, int b)
{
int c;
c = a*b;
return( c );
}

2. #include <stdio.h>
main ( )
{
int x, y, z;
int mul (int, int); /* function prototype */
printf (“Enter two numbers”);
scanf (“%d %d”, &x, &y);
z = mul (x, y); /* function call */
printf (“result is %d”, z); }
72
Functions
Check Your Progress 2
1. (a) Valid
(b) In valid
(c) Valid
(d) Valid
(e) Invalid

Check Your Progress 3


1. /* Function to print Fibonacci Series */

void fib(int n)
{
int curr_term, int count = 0;
int first = 1;
int second = 1;
print (“%d %d”, curr_term);
count = 2;
o m
while(count < = n)
{ curr_term = first + second;
. c
printf (“%d”, curr_term);
first = second;
o t
second = curr_term;
s p
}
count++;

og
}

b l
2. /* Non Recursive Power function i.e. pow(a, b) */
6 .
int pow( int a, int b)
0 0
{
int i, p = 1;
i a
p = p*a;
o n
for (i = 1; i < = b; i++)

return (p);
p
} o
i l
n
Check Your Progress 4

u
s1. /* Recursive Power Function */

int pow ( int a, int b )


{ if ( b = = 0 )
return (1);
else
return (a* pow (a, b-1 )); /* Recursive call */
}

/* Main Function */
main ( )
{
int a, b, p;
printf (“ Enter two numbers”);
scanf ( “%d %d”, &a, &b );
p = pow (a, b); /* Function call */
printf ( “ The result is %d”, p);
}
73
Control Statements,
Arrays and
Functions 8.12 FURTHER READINGS
1. The C programming language, Brain W. Kernighan, Dennis M. Ritchie, PHI
2. C,The Complete Reference, Fourth Edition, Herbert Schildt, Tata McGraw
Hill, 2002.
3. Computer Programming in C, Raja Raman. V, 2002, PHI.
5. C,The Complete Reference, Fourth Edition, Herbert Schildt, TMGH,2002.

o m
. c
o t
s p
o g
b l
6 .
0 0
i a
o n
p o
i l
u n
s

74
Structures and
Unions

UNIT 9 STRUCTURES AND UNIONS


Structure

9.0 Introduction
9.1 Objectives
9.2 Declaration of Structures
9.3 Accessing the Members of a Structure
9.4 Initializing Structures
9.5 Structures as Function Arguments
9.6 Structures and Arrays
9.7 Unions
9.8 Initializing an Union
9.9 Accessing the Members of an Union
9.10 Summary
9.11 Solutions / Answers
9.12 Further Readings

9.0 INTRODUCTION
o m
We have seen so far how to store numbers, characters, strings, and even large sets of
. c
these primitives using arrays, but what if we want to store collections of different
kinds of data that are somehow related. For example, a file about an employee will
o t
s p
probably have his/her name, age, the hours of work, salary, etc. Physically, all of that
is usually stored in someone’s filing cabinet. In programming, if you have lots of

o g
related information, you group it together in an organized fashion. Let’s say you have

b l
a group of employees, and you want to make a database! It just wouldn’t do to have
tons of loose variables hanging all over the place. Then we need to have a single data
.
entity where we will be able to store all the related information together. But this
6
0 0
can’t be achieved by using the arrays alone, as in the case of arrays, we can group
multiple data elements that are of the same data type, and is stored in consecutive

defined datatype Structures come in.


i a
memory locations, and is individually accessed by a subscript. That is where the user-

o n
Structure is commonly referred to as a user-defined data type. C’s structures allow

p o
you to store multiple variables of any type in one place (the structure). A structure
can contain any of C’s data types, including arrays and other structures. Each variable
l
within a structure is called a member of the structure. They can hold any number of
i
u n
variables, and you can make arrays of structures. This flexibility makes structures
ideally useful for creating databases in C. Similar to the structure there is another user

s defined data type called Union which allows the programmer to view a single storage
in more than one way i.e., a variable declared as union can store within its storage
space, the data of different types, at different times. In this unit, we will be discussing
the user-defined data type structures and unions.

9.1 OBJECTIVES
After going through this unit you should be able to:

• declare and initialize the members of the structures;


• access the members of the structures;
• pass the structures as function arguments;
• declare the array of structures;
• declare and define union; and
• perform all operations on the variables of type Union.

9.2 DECLARATION OF STRUCTURES

5
Structures, Pointers
and File Handling
To declare a structure you must start with the keyword struct followed by the
structure name or structure tag and within the braces the list of the structure’s
member variables. Note that the structure declaration does not actually create any
variables. The syntax for the structure declaration is as follows:

struct structure-tag {
datatype variable1;
datatype variable2;
dataype variable 3;
...
};

For example, consider the student database in which each student has a roll number,
name and course and the marks obtained. Hence to group this data with a structure-
tag as student, we can have the declaration of structure as:

struct student {
int roll_no;
char name[20];
char course[20];
int marks_obtained ;
o m
};
. c
o t
The point you need to remember is that, till this time no memory is allocated to the

s p
structure. This is only the definition of structure that tells us that there exists a user-
defined data type by the name of student which is composed of the following

g
members. Using this structure type, we have to create the structure variables:

o
struct student stud1, stud2 ;
b l
6 .
At this point, we have created two instances or structure variables of the user-defined

0 0
data type student. Now memory will be allocated. The amount of memory allocated
will be the sum of all the data members which form part of the structure template.

i a
The second method is as follows:

o n
ostruct {

lp
int roll_no;
char name[20];

n i char course[20];
int marks_obtained ;

su } stud1, stud2 ;

In this case, a tag name student is missing, but still it happens to be a valid
declaration of structure. In this case the two variables are allocated memory
equivalent to the members of the structure.

The advantage of having a tag name is that we can declare any number of variables of
the tagged named structure later in the program as per requirement.

If you have a small structure that you just want to define in the program, you can do
the definition and declaration together as shown below. This will define a structure of
type struct telephone and declare three instances of it.

Consider the example for declaring and defining a structure for the telephone billing
with three instances:

struct telephone{
int tele_no;
int cust_code;

6
Structures and
Unions
char cust_address[40];
int bill_amt;
} tele1, tele2, tele3;

The structure can also be declared by using the typedefinition or typedef. This can be
done as shown below:

typedef struct country{


char name[20];
int population;
char language[10];
} Country;

This defines a structure which can be referred to either as struct country or Country,
whichever you prefer. Strictly speaking, you don’t need a tag name both before and
after the braces if you are not going to use one or the other. But it is a standard
practice to put them both in and to give them the same name, but the one after the
braces starts with an uppercase letter.

The typedef statement doesn’t occupy storage: it simply defines a new type. Variables
that are declared with the typedef above will be of type struct country, just like
o m
population is of type integer. The structure variables can be now defined as below:
. c
Country Mexico, Canada, Brazil;
o t
9.3 ACCESSING THE MEMBERS OF A s p
STRUCTURE
o g
b l
6 .
Individual structure members can be used like other variables of the same type.
Structure members are accessed using the structure member operator (.), also called

0
accessing the member of the structure is: 0
the dot operator, between the structure name and the member name. The syntax for

i a
n
structurevariable. member-name;

o
o
Let us take the example of the coordinate structure.

p
i l
struct coordinate{
int x;

u n int y;

s };
Thus, to have the structure named first refer to a screen location that has coordinates
x=50, y=100, you could write as,
first.x = 50;
first.y = 100;
To display the screen locations stored in the structure second, you could write,

printf ("%d,%d", second.x, second.y);


The individual members of the structure behave like ordinary date elements and can
be accessed accordingly.

Now let us see the following program to clarify our concepts. For example, let us see,
how will we go about storing and retrieving values of the individual data members of
the student structure.

Example 9.1

/*Program to store and retrieve the values from the student structure*/

7
Structures, Pointers
and File Handling

#include<stdio.h>
struct student {
int roll_no;
char name[20];
char course[20];
int marks_obtained ;
};
main()
{
student s1 ;
printf (“Enter the student roll number:”);
scanf (“%d”,&s1.roll_no);
printf (“\nEnter the student name: “);
scanf (“%s”,s1.name);
printf (“\nEnter the student course”);
scanf (“%s”,s1.course);
printf (“Enter the student percentage\n”);
scanf (“%d”,&s1.marks_obtained);
printf (“\nData entry is complete”);
printf ( “\nThe data entered is as follows:\n”);
o m
printf (“\nThe student roll no is %d”,s1.roll_no);
printf (“\nThe student name is %s”,s1.name);
. c
printf (“\nThe student course is %s”,s1.course);
o t
}
s p
printf (“\nThe student percentage is %d”,s1.marks_obtained);

OUTPUT
o g
b l
6
Enter the student name: ARUN .
Enter the student roll number: 1234

0 0
Enter the student course: MCA
Enter the student percentage: 84

a
Date entry is complete
i
o n
The data entered is as follows:

p oThe student roll no is 1234


The student name is ARUN

i l The student course is MCA


The student percentage is 84

u n Another way of accessing the storing the values in the members of a structure is by
s initializing them to some values at the time when we create an instance of the data
type.

9.4 INITIALIZING STRUCTURES


Like other C variable types, structures can be initialized when they’re declared. This
procedure is similar to that for initializing arrays. The structure declaration is
followed by an equal sign and a list of initialization values is separated by commas
and enclosed in braces. For example, look at the following statements for initializing
the values of the members of the mysale structure variable.

Example 9.2

struct sale {
char customer[20];
char item[20];
float amt;
} mysale = { "XYZ Industries",

8
Structures and
Unions
“toolskit",
600.00
};

In a structure that contains structures as members, list the initialization values in


order. They are placed in the structure members in the order in which the members
are listed in the structure definition. Here’s an example that expands on the previous
one:

Example 9.3

struct customer {
char firm[20];
char contact[25];
}

struct sale {
struct customer buyer1;
char item [20];
float amt;
o m
} mysale = {
{ "XYZ Industries", "Tyran Adams"},
. c
"toolskit",
600.00
o t
};
s p
These statements perform the following initializations:
o
• the structure member mysale.buyer1.firm is initialized to the string “XYZ
g
Industries”.
b l

6 .
the structure member mysale.buyer1.contact is initialized to the string “Tyran


Adams”.

0 0
the structure member mysale.item is initialized to the string "toolskit".

a
the structure member mysale.amount is initialized to the amount 600.00.
i
n
For example let us consider the following program where the data members are
o
o
initialized to some value.

p
l
Example 9.4

i
u n
Write a program to access the values of the structure initialized with some initial
values.
s /* Program to illustrate to access the values of the structure initialized with some
initial values*/

#include<stdio.h>
struct telephone{
int tele_no;
int cust_code;
char cust_name[20];
char cust_address[40];
int bill_amt;
};
main()
{
struct telephone tele = {2314345,
5463,
"Ram",
"New Delhi",

9
Structures, Pointers
and File Handling
2435 };

printf("The values are initialized in this program.");


printf("\nThe telephone number is %d",tele.tele_no);
printf("\nThe customer code is %d",tele.cust_code);
printf("\nThe customer name is %s",tele.cust_name);
printf("\nThe customer address is %s",tele.cust_address);
printf("\nThe bill amount is %d",tele.bill_amt);
}

OUTPUT

The values are initialized in this program.


The telephone number is 2314345
The customer code is 5463
The customer name is Ram
The customer Address is New Delhi
The bill amount is 2435

Check Your Progress 1

o m
1. What is the difference between the following two declarations?
. c
struct x1{……….};
typedef struct{………}x2;
o t
…………………………………………………………………………………………
s p
…………………………………………………………………………………………
o g
b l
.
2. Why can’t you compare structures?

6
0
…………………………………………………………………………………………

0
a
…………………………………………………………………………………………..

i
o n
3. Why does size of report a larger size than, one expects, for a structure type, as if

p o there were padding at the end?

i l …………………………………………………………………………………………

u n …………………………………………………………………………………………

s 4. Declare a structure and instance together to display the date.

…………………………………………………………………………………………
…………………………………………………………………………………………

9.5 STRUCTURES AS FUNCTION ARGUMENTS


C is a structured programming language and the basic concept in it is the modularity
of the programs. This concept is supported by the functions in C language. Let us
look into the techniques of passing the structures to the functions. This can be
achieved in primarily two ways: Firstly, to pass them as simple parameter values by
passing the structure name and secondly, through pointers. We will be concentrating
on the first method in this unit and passing using pointers will be taken up in the next
unit. Like other data types, a structure can be passed as an argument to a function.
The program listing given below shows how to do this. It uses a function to display
data on the screen.

10
Structures and
Unions

Example 9.5

Write a program to demonstrate passing a structure to a function.

/*Program to demonstrate passing a structure to a function.*/

#include <stdio.h>

/*Declare and define a structure to hold the data.*/

struct data{
float amt;
char fname [30];
char lname [30];
} per;

main()
{
void print_per (struct data x);
printf(“Enter the donor’s first and last names separated by a space:”);
o m
scanf (“%s %s”, per.fname, per.lname);
printf (“\nEnter the amount donated in rupees:”);
. c
scanf (“%f”, &per.amt);
o t
print_per (per);
return 0;
s p
}

o g
void print_per(struct data x)
b l
{

6 .
printf ("\n %s %s gave donation of amount Rs.%.2f.\n", x.fname, x.lname, x.amt);
}

0 0
OUTPUT
i a
o n
Enter the donor’s first and last names separated by a space: RAVI KANT

p o
Enter the amount donated in rupees: 1000.00
RAVI KANT gave donation of the amount Rs. 1000.00.

i l
You can also pass a structure to a function by passing the structure’s address (that is,

u n
a pointer to the structure which we will be discussing in the next unit). In fact, in the
older versions of C, this was the only way to pass a structure as an argument. It is not
s necessary now, but you might see the older programs that still use this method. If you
pass a pointer to a structure as an argument, remember that you must use the indirect
membership operator (Æ) to access structure members in the function.

Please note the following points with respect to passing the structure as a parameter
to a function.
• The return value of the called function must be declared as the value that is being
returned from the function. If the function is returning the entire structure then
the return value should be declared as struct with appropriate tag name.
• The actual and formal parameters for the structure data type must be the same as
the struct type.
• The return statement is required only when the function is returning some data.
• When the return values of type is struct, then it must be assigned to the structure
of identical type in the calling function.

Let us consider another example as shown in the Example 9.6, where structure salary
has three fields related to an employee, namely - name, no_days_worked and
daily_wage. To accept the values from the user we first call the function get_data that

11
Structures, Pointers
and File Handling
gets the values of the members of the structure. Then using the wages function we
calculate the salary of the person and display it to the user.

Example 9.6

Write a program to accept the data from the user and calculate the salary of the
person using concept of functions.

/* Program to accept the data from the user and calculate the salary of the person*/

#include<stdio.h>
main()
{
struct sal {
char name[30];
int no_days_worked;
int daily_wage; };
struct sal salary;
struct sal get_dat(struct); /* function prototype*/
float wages(struct);
float amount_payable;
/*function prototype*/
/* variable declaration*/
o m
salary = get_data(salary);
printf(“The name of employee is %s”,salary.name);
. c
printf(“Number of days worked is %d”,salary.no_daya_worked);
o t
amount_payable = wages(salary);
s p
printf(“The daily wage of the employees is %d”,salary.daily_wage);

}
o g
printf(“The amount payable to %s is %f”,salary.name,amount_payable);

b l
{
6 .
struct sal get_data(struct sal income)

0 0
printf(“Please enter the employee name:\n”);
scanf(“%s”,income.name);

i a
printf(“Please enter the number of days worked:\n”);
scanf(“%d”,&income.no_days_worked);

o n printf(‘Please enter the employee daily wages:\n”);

p o scanf(“%d”,&income.daily_wages);
return(income);

i l }

sun float wages(struct)


{
struct sal amt;
int total_salary ;
total_salary = amt.no_days_worked * amt.daily_wages;
return(total_salary); }

Check Your Progress 2

1. How is structure passing and returning implemented?

…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
2. How can I pass constant values to functions which accept structure arguments?

12
Structures and
Unions
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………

3. What will be the output of the program?

#include<stdio.h>
main( )
{
struct pqr{
int x ;
};
struct pqr pqr ;
pqr.x =10 ;
printf (“%d”, pqr.x);
}

…………………………………………………………………………………………
…………………………………………………………………………………………
o m
…………………………………………………………………………………………
. c
o t
9.6 STRUCTURES AND ARRAYS
s p
o g
Thus far we have studied as to how the data of heterogeneous nature can be grouped

b l
together and be referenced as a single unit of structure. Now we come to the next step
in our real world problem. Let’s consider the example of students and their marks. In
.
this case, to avoid declaring various data variables, we grouped together all the data
6
0 0
concerning the student’s marks as one unit and call it student. The problem that arises
now is that the data related to students is not going to be of a single student only. We

i a
will be required to store data for a number of students. To solve this situation one
way is to declare a structure and then create sufficient number of variables of that

o n
structure type. But it gets very cumbersome to manage such a large number of data
variables, so a better option is to declare an array.

p o
i l
So, revising the array for a few moments we would refresh the fact that an array is
simply a collection of homogeneous data types. Hence, if we make a declaration as:

u n
int temp[20];
s It simply means that temp is an array of twenty elements where each element is of
type integer, indicating homogenous data type. Now in the same manner, to extend
the concept a bit further to the structure variables, we would say,

struct student stud[20] ;

It means that stud is an array of twenty elements where each element is of the type
struct student (which is a user-defined data type we had defined earlier). The various
members of the stud array can be accessed in the similar manner as that of any other
ordinary array.

For example,
struct student stud[20], we can access the roll_no of this array as

stud[0].roll_no;
stud[1].roll_no;
stud[2].roll_no;
stud[3].roll_no;

13
Structures, Pointers
and File Handling



stud[19].roll_no;

Please remember the fact that for an array of twenty elements the subscripts of the
array will be ranging from 0 to 19 (a total of twenty elements). So let us now start by
seeing how we will write a simple program using array of structures.

Example 9.7

Write a program to read and display data for 20 students.

/*Program to read and print the data for 20 students*/

#include <stdio.h>
struct student { int roll_no;
char name[20];
char course[20];

};
int marks_obtained ;

o m
main( )
{
. c
struct student stud [20];
o t
int i;
printf (“Enter the student data one by one\n”);
s p
for(i=0; i<=19; i++)
{
o g
l
printf (“Enter the roll number of %d student”,i+1);
b
6 .
scanf (“%d”,&stud[i].roll_no);
printf (“Enter the name of %d student”,i+1);

0 0
scanf (“%s”,stud[i].name);
printf (“Enter the course of %d student”,i+1);

i a
scanf (“%d”,stud[i].course);
printf (“Enter the marks obtained of %d student”,i+1);

o n scanf (“%d”,&stud[i].marks_obtained);

p o }
printf (“the data entered is as follows\n”);

i l {
for (i=0;i<=19;i++)

u n printf (“The roll number of %d student is %d\n”,i+1,stud[i].roll_no);


printf (“The name of %d student is %s\n”,i+1,stud[i].name);
s printf (“The course of %d student is %s\n”,i+1,stud[i].course);
printf (“The marks of %d student is %d\n”,i+1,stud[i].marks_obtained);
}
}

The above program explains to us clearly that the array of structure behaves as any
other normal array of any data type. Just by making use of the subscript we can
access all the elements of the structure individually.

Extending the above concept where we can have arrays as the members of the
structure. For example, let’s see the above example where we have taken a structure
for the student record. Hence in this case it is a real world requirement that each
student will be having marks of more than one subject. Hence one way to declare the
structure, if we consider that each student has 3 subjects, will be as follows:

struct student {
int roll_no;
char name [20];

14
Structures and
Unions
char course [20];
int subject1 ;
int subject2;
int subject3;
};

The above described method is rather a bit cumbersome, so to make it more efficient
we can have an array inside the structure, that is, we have an array as the member of
the structure.
struct student {
int roll_no;
char name [20];
char course [20];
int subject [3] ;
};

Hence to access the various elements of this array we can the program logic as
follows:

Example 9.8

o m
/*Program to read and print data related to five students having marks of three
subjects each using the concept of arrays */
. c
o t
#include<stdio.h>
struct student {
s p
int roll_no;
char name [20];
o g
char course [20];
b l
int subject [3] ;
};
6 .
main( )
{
0 0
struct student stud[5];
int i,j;
i a
o n
printf (“Enter the data for all the students:\n”);
for (i=0;i<=4;i++)
{
p o
i l
printf (“Enter the roll number of %d student”,i+1);
scanf (“%d”,&stud[i].roll_no);

u n
printf(“Enter the name of %d student”,i+1);
scanf (“%s”,stud[i].name);
s printf (“Enter the course of %d student”,i+1);
scanf (“%s”,stud[i].course);
for (j=0;j<=2;j++)
{
printf (“Enter the marks of the %d subject of the student %d:\n”,j+1,i+1);
scanf (“%d”,&stud[i].subject[j]);
}
}
printf (“The data you have entered is as follows:\n”);
for (i=0;i<=4;i++)
{
printf (“The %d th student's roll number is %d\n”,i+1,stud[i].roll_no);
printf (“The %d the student's name is %s\n”,i+1,stud[i].name);
printf (“The %d the student's course is %s\n”,i+1,stud[i].course);
for (j=0;j<=2;j++)
{
printf (“The %d the student's marks of %d I subject are %d\n”,i+1, j+1,
stud[i].subject[j]);

15
Structures, Pointers
and File Handling
}
}
printf (“End of the program\n”);
}

Hence as described in the example above, the array as well as the arrays of structures
can be used with efficiency to resolve the major hurdles faced in the real world
programming environment.

9.7 UNIONS
Structures are a way of grouping homogeneous data together. But it often happens
that at any time we require only one of the member’s data. For example, in case of
the support price of shares you require only the latest quotations. And only the ones
that have changed need to be stored. So if we declare a structure for all the scripts, it
will only lead to crowding of the memory space. Hence it is beneficial if we allocate
space to only one of the members. This is achieved with the concepts of the UNIONS.
UNIONS are similar to STRUCTURES in all respects but differ in the concept of
storage space.

o m
A UNION is declared and used in the same way as the structures. Yet another

.
difference is that only one of its members can be used at any given time. Since all c
o t
members of a Union occupy the same memory and storage space, the space allocated
is equal to the largest data member of the Union. Hence, the member which has been
updated last is available at any given time.
s p
o g
For example a union can be declared using the syntax shown below:

union union-tag {
b l
.
datatype variable1;
6
0 0
datatype variable2;
...

i a };

o n
For example,
union temp{

p o int x;

i l char y;
float z;

u n };

s In this case a float is the member which requires the largest space to store its value
hence the space required for float (4 bytes) is allocated to the union. All members
share the same space. Let us see how to access the members of the union.

Example 9.9

Write a program to illustrate the concept of union.

/* Declare a union template called tag */


union tag {
int nbr;
char character;
}
/* Use the union template */
union tag mixed_variable;
/* Declare a union and instance together */
union generic_type_tag {
char c;
int i;

16
Structures and
Unions
float f;
double d;
} generic;

9.8 INITIALIZING AN UNION


Let us see, how to initialize a Union with the help of the following example:

Example 9.10
union date_tag {
char complete_date [9];
struct part_date_tag {
char month[2];
char break_value1;
char day[2];
char break_value2;
char year[2];
} parrt_date;
}date = {“01/01/05”};
o m
9.9 ACCESSING THE MEMBERS OF AN UNION . c
o t
Individual union members can be used in the same way as the structure members, by
using the member operator or dot operator (.). However, there is an important
s p
difference in accessing the union members. Only one union member should be
o g
accessed at a time. Because a union stores its members on top of each other, it’s

b l
important to access only one member at a time. Trying to access the previously stored
values will result in erroneous output.

6 .
Check Your Progress 3

0 0
1. What will be the output?
i a
#include<stdio.h>
o n
main()
{
p o
i l
union{
struct{

u n char x;

s char y;
char z;
char w;
}xyz;

struct{
int p;
int q ;
}pq;
long a ;
float b;
double d;
}prq;
printf (“%d”,sizeof(prq));
}

9.10 SUMMARY

17
Structures, Pointers
and File Handling
In this unit, we have learnt how to use structures, a data type that you design to meet
the needs of a program. A structure can contain any of C’s data types, including other
structures, pointers, and arrays. Each data item within a structure, called a member, is
accessed using the structure member operator (.) between the structure name and the
member name. Structures can be used individually, and can also be used in arrays.

Unions were presented as being similar to structures. The main difference between a
union and a structure is that the union stores all its members in the same area. This
means that only one member of a union can be used at a time.

9.11 SOLUTIONS / ANSWERS


Check Your Progress 1

1. The first form declares a structure tag; the second declares a typedef. The main
difference is that the second declaration is of a slightly more abstract type - users
do not necessarily know that it is a structure, and the keyword struct is not used
while declaring an instance.

o m
2. There is no single correct way for a compiler to implement a structure
comparison consistent with C’s low-level flavor. A simple byte-by-byte
. c
o t
comparison could detect the random bits present in the unused “holes” in the
structure (such padding is used to keep the alignment of later fields correct). A

repetitive code.
s p
field-by-field comparison for a large structure might require an inordinate

o g
b l
3. Structures may have this padding (as well as internal padding), to ensure that
alignment properties will be preserved when an array of contiguous structures is

6 .
allocated. Even when the structure is not part of an array, the end padding remains,
so that sizeof can always return a consistent size.

0 0
i a
4. struct date {
char month[2];

o n char day[2];
char year[4];

p o } current_date;

i l Check Your Progress 2

u n 1. When structures are passed as arguments to functions, the entire structure is

s typically pushed on the stack, using as many words. (Programmers often choose
to use pointers instead, to avoid this overhead). Some compilers merely pass a
pointer to the structure, though they may have to make a local copy to preserve
pass-by value semantics.

Structures are often returned from functions in a pointed location by an extra,


compiler-supplied “hidden” argument to the function. Some older compilers used
a special, static location for structure returns, although this made structure -
valued functions non-reentrant, which ANSI C disallows.

2. C has no way of generating anonymous structure values. You will have to use a
temporary structure variable or a little structure - building function.

3. 10

Check Your Progress 3

1. 8

18
Structures and
Unions

9.12 FURTHER READINGS


1. The C Programming Language, Kernighan & Richie, PHI Publication, 2002.
2. Computer Science A structured programming approach using C,
Behrouza .Forouzan, Richard F. Gilberg, Second Edition, Brooks/Cole,
Thomson Learning, 2001.
3. Programming with C, Schaum Outlines, Second Edition, Gottfried, Tata McGraw
Hill, 2003.

o m
. c
o t
s p
o g
b l
6 .
0 0
i a
o n
p o
i l
u n
s

19
Structures, Pointers
and File Handling UNIT 10 POINTERS

Structure

10.0 Introduction
10.1 Objectives
10.2 Pointers and their Characteristics
10.3 Address and Indirection Operators
10.4 Pointer Type Declaration and Assignment
10.4.1 Pointer to a Pointer
10.4.2 Null Pointer Assignment
10.5 Pointer Arithmetic
10.6 Passing Pointers to Functions
10.6.1 A Function Returning More than One Value
10.6.2 Function Returning a Pointer
10.7 Arrays and Pointers
10.8 Array of Pointers
10.9 Pointers and Strings
o m
10.10 Summary
10.11 Solutions / Answers
. c
10.12 Further Readings

o t
10.0 INTRODUCTION
s p
o g
b l
If you want to be proficient in the writing of code in the C programming language,
you must have a thorough working knowledge of how to use pointers. One of those

6 .
things, beginners in C find difficult is the concept of pointers. The purpose of this
unit is to provide an introduction to pointers and their efficient use in the C

0 0
programming. Actually, the main difficulty lies with the C’s pointer terminology than

a
the actual concept.

i
o n
C uses pointers in three main ways. First, they are used to create dynamic data
structures: data structures built up from blocks of memory allocated from the heap at

p o run-time. Second, C uses pointers to handle variable parameters passed to functions.


And third, pointers in C provide an alternative means of accessing information stored

i l in arrays, which is especially valuable when you work with strings.

u n A normal variable is a location in memory that can hold a value. For example, when

s you declare a variable i as an integer, four bytes of memory is set aside for it. In your
program, you refer to that location in memory by the name i. At the machine level,
that location has a memory address, at which the four bytes can hold one integer value.
A pointer is a variable that points to another variable. This means that it holds the
memory address of another variable. Put another way, the pointer does not hold a
value in the traditional sense; instead, it holds the address of another variable. It points
to that other variable by holding its address.

Because a pointer holds an address rather than a value, it has two parts. The pointer
itself holds the address. That addresses points to a value. There is the pointer and the
value pointed to. As long as you’re careful to ensure that the pointers in your
programs always point to valid memory locations, pointers can be useful, powerful,
and relatively trouble-free tools.

We will start this unit with a basic introduction to pointers and the concepts
surrounding pointers, and then move on to the three techniques described above.
Thorough knowledge of the pointers is very much essential for your future courses
like the datastructures, design and analysis of algorithms etc..
20
Pointers

10.1 OBJECTIVES

After going through this unit you should be able to:

• understand the concept and use pointers;


• address and use of indirection operators;
• make pointer type declaration, assignment and initialization;
• use null pointer assignment;
• use the pointer arithmetic;
• handle pointers to functions;
• see the underlying unit of arrays and pointers; and
• understand the concept of dynamic memory allocation.

10.2 POINTERS AND THEIR CHARACTERISTICS

Computer’s memory is made up of a sequential collection of storage cells called


bytes. Each byte has a number called an address associated with it. When we declare
o m
a variable in our program, the compiler immediately assigns a specific block of
. c
memory to hold the value of that variable. Since every cell has a unique address, this
block of memory will have a unique starting address. The size of this block depends
o t
on the range over which the variable is allowed to vary. For example, on 32 bit PC’s
the size of an integer variable is 4 bytes. On older 16 bit PC’s integers were 2 bytes.
s p
g
In C the size of a variable type such as an integer need not be the same on all types of
o
b l
machines. If you want to know the size of the various data types on your system,
running the following code given in the Example 10.1 will give you the information.

Example 10.1
6 .
0 0
Write a program to know the size of the various data types on your system.

i a
# include <stdio.h>
main( )
o n
{

p o
printf (“n Size of a int = %d bytes”, sizeof (int));

i l
printf (“\n Size of a float = %d bytes”, sizeof (float));
printf (“\n Size of a char = %d bytes”, sizeof (char));
}
u n
sOUTPUT

Size of int = 2 bytes


Size of float = 4 bytes
Size of char = 1 byte

An ordinary variable is a location in memory that can hold a value. For example,
when you declare a variable num as an integer, the compiler sets aside 2 bytes of
memory (depends up the PC) to hold the value of the integer. In your program, you
refer to that location in memory by the name num. At the machine level that location
has a memory address.

int num = 100;

We can access the value 100 either by the name num or by its memory address. Since
addresses are simply digits, they can be stored in any other variable. Such variables
that hold addresses of other variables are called Pointers. In other words, a pointer is
21
Structures, Pointers simply a variable that contains an address, which is a location of another variable in
and File Handling
memory. A pointer variable “points to” another variable by holding its address.
Since a pointer holds an address rather than a value, it has two parts. The pointer
itself holds the address. That addresses points to a value. There is a pointer and the
value pointed to. This fact can be a little confusing until you get comfortable with it,
but once you get familiar with it, then it is extremely easy and very powerful. One
good way to visualize this concept is to examine the figure 10.1 given below:

num

ch

temp

o m
ptr1

. c
o t
ptr2
s p
o g
b l
Figure 10.1: Concept of pointer variables

6 .
Let us see the important features of the pointers as follows:

0 0
a
Characteristic features of Pointers:
i
n
With the use of pointers in programming,
o
p o i. The program execution time will be faster as the data is manipulated with the

i l help of addresses directly.


ii. Will save the memory space.

u n iii. The memory access will be very efficient.


iv. Dynamic memory is allocated.
s 10.3 THE ADDRESS AND INDIRECTION OPERATORS

Now we will consider how to determine the address of a variable. The operator that is
available in C for this purpose is “&” (address of ) operator. The operator & and the
immediately preceding variable returns the address of the variable associated with it.
C’s other unary pointer operator is the “*”, also called as value at address or
indirection operator. It returns a value stored at that address. Let us look into the
illustrative example given below to understand how they are useful.

Example 10.2

Write a program to print the address associated with a variable and value
stored at that address.

/* Program to print the address associated with a variable and value stored at that
22 address*/
Pointers
# include <stdio.h>
main( )
{
int qty = 5;
printf ("Address of qty = %u\n",&qty);
printf ("Value of qty = %d \n",qty);
printf("Value of qty = %d",*(&qty));
}

OUTPUT

Address of qty = 65524


Value of qty = 5
Value of qty = 5

Look at the printf statement carefully. The format specifier %u is taken to increase
the range of values the address can possibly cover. The system-generated address of
the variable is not fixed, as this can be different the next time you execute the same
program. Remember unary operator operates on single operands. When & is
o m
preceded by the variable qty, has returned its address. Note that the & operator can
. c
be used only with simple variables or array elements. It cannot be applied to
expressions, constants, or register variables.
o t
Observe the third line of the above program. *(&qty) returns the value stored at
s p
address 65524 i.e. 5 in this case. Therefore, qty and *(&qty) will both evaluate to 5.
o g
10.4 POINTER TYPE DECLARATION AND
b l
ASSIGNMENT
6 .
0 0
We have seen in the previous section that &qty returns the address of qty and this
a
address can be stored in a variable as shown below:
i
ptr = &qty;
o n
p o
In C, every variable must be declared for its data type before it is used. Even this

i l
holds good for the pointers too. We know that ptr is not an ordinary variable like any
integer variable. We declare the data type of the pointer variable as that of the type of

u n
the data that will be stored at the address to which it is pointing to. Since ptr is a

svariable, which contains the address of an integer variable qty, it can be declared as:

int *ptr;

where ptr is called a pointer variable. In C, we define a pointer variable by preceding


its name with an asterisk(*). The “*” informs the compiler that we want a pointer
variable, i.e. to set aside the bytes that are required to store the address in memory.
The int says that we intend to use our pointer variable to store the address of an
integer. Consider the following memory map:

ptr qty Variable

65524 100 Value

65522 65524 Address

Let us look into an example given below:


23
Structures, Pointers
and File Handling
Example 10.3

/* Program below demonstrates the relationships we have discussed so far */

# include <stdio.h>
main( )
{
int qty = 5;
int *ptr; /* declares ptr as a pointer variable that points to an integer variable
*/
ptr = &qty; /* assigning qty’s address to ptr -> Pointer Assignment */

printf ("Address of qty = %u \n", &qty);


printf ("Address of qty = %u \n", ptr);
printf ("Address of ptr = %u \n", &ptr);
printf ("Value of ptr = %d \n", ptr);
printf ("Value of qty = %d \n", qty);
printf ("Value of qty = %d \n", *(&qty));
printf ("Value of qty = %d", *ptr);
o m
}
. c
OUTPUT
o t
Address of qty = 65524
s p
Address of ptr = 65522
Value of ptr = 65524
o g
Value of qty = 5
b l
Value of qty = 5
Value of qty = 5
6 .
0
Try this as well: 0
i
Example 10.4a
o n
p o /* Program that tries to reference the value of a pointer even though the pointer is
uninitialized */

i l # include <stdio.h>

u n main()
{
s int *p; /* a pointer to an integer */
*p = 10;
printf(“the value is %d”, *p);
printf(“the value is %u”,p);
}

This gives you an error. The pointer p is uninitialized and points to a random location
in memory when you declare it. It could be pointing into the system stack, or the
global variables, or into the program’s code space, or into the operating system.
When you say *p=10; the program will simply try to write a 10 to whatever random
location p points to. The program may explode immediately. It may subtly corrupt
data in another part of your program and you may never realize it. Almost always, an
uninitialized pointer or a bad pointer address causes the fault.

This can make it difficult to track down the error. Make sure you initialize all
pointers to a valid address before dereferencing them.

24
Within a variable declaration, a pointer variable can be initialized by assigning it the Pointers
address of another variable. Remember the variable whose address is assigned to the
pointer variable must be declared earlier in the program. In the example given below,
let us assign the pointer p with an address and also a value 10 through the *p.

Example 10.5

Let us say,

int x; /* x is initialized to a value 10*/


p = &x; /* Pointer declaration & Assignment */
*p=10;

Let us write the complete program as shown below:

# include <stdio.h>
main( )
{
int *p; /* a pointer to an integer */
o m
int x;
. c
p = &x;
*p=10;
o t
printf("The value of x is %d",*p);
printf("\nThe address in which the x is stored is %d",p);
s p
}
o g
OUTPUT
b l
The value of x is 10
6 .
The address in which the x is stored is 52004

0 0
i a
This statement puts the value of 20 at the memory location whose address is the value
of px. As we know that the value of px is the address of x and so the old value of x is

o n
replaced by 20. This is equivalent to assigning 20 to x. Thus we can change the value

o
of a variable indirectly using a pointer and the indirection operator.

p
l
10.4.1 Pointer to a Pointer
i
u n
The concept of pointer can be extended further. As we have seen earlier, a pointer
variable can be assigned the address of an ordinary variable. Now, this variable itself
scould be another pointer. This means that a pointer can contain address of another
pointer. The following program will makes you the concept clear.

Example 10.6

/* Program that declares a pointer to a pointer */

# include<stdio.h>
main( )
{
int i = 100;
int *pi;
int **pii;
pi = &i;
pii = &pi;

printf ("Address of i = %u \n", &i);


25
Structures, Pointers printf ("Address of i = %u \n", pi);
and File Handling
printf ("Address of i = %u \n", *pii);
printf ("Address of pi = %u \n", &pi);
printf ("Address of pi = %u \n", pii);
printf ("Address of pii = %u \n", &pii);
printf ("Value of i = %d \n", i);
printf ("Value of i = %d \n", *(&i));
printf ("Value of i = %d \n", *pi);
printf ("Value of i = %d", **pii);
}

OUTPUT

Address of i = 65524
Address of i = 65524
Address of i = 65524
Address of pi = 65522
Address of pi = 65522
Address of pii = 65520

o m
Value of i = 100
. c
Value of i = 100
Value of i = 100
o t
Value of i = 100

s p
o g
Consider the following memory map for the above shown example:

b l
pii
65522
6 . pi
65524
i
100
Variable

0 0 Value

i a
65520 65522 65524 Address

o n
p o 10.4.2 Null Pointer Assignment

i l It does make sense to assign an integer value to a pointer variable. An exception is an


assignment of 0, which is sometimes used to indicate some special condition. A

u n macro is used to represent a null pointer. That macro goes under the name NULL.

s Thus, setting the value of a pointer using the NULL, as with an assignment statement
such as ptr = NULL, tells that the pointer has become a null pointer. Similarly, as
one can test the condition for an integer value as zero or not, like if (i == 0), as well
we can test the condition for a null pointer using if (ptr == NULL) or you can even
set a pointer to NULL to indicate that it’s no longer in use. Let us see an example
given below.

Example 10.7

# include<stdio.h>
# define NULL 0
main()
{
int *pi = NULL;
printf(“The value of pi is %u”, pi);
}

26
OUTPUT Pointers

The value of pi is 0

Check Your Progress 1

1. How is a pointer variable being declared? What is the purpose of data type
included in the pointer declaration?

…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
2. What would be the output of following programs?

(i) void main( )


{
int i = 5;
printf ("Value of i = %d Address of i = %u", i, &i);
o m
&i = 65534;
printf ("\n New value of i = %d New Address of i = %u", i, &i);
. c
}
o t
(ii) void main( )
s p
{
int *i, *j;
o g
j = i * 2;
printf ("j = %u", j);
b l
}
6 .
…………………………………………………………………………………………
0 0
…………………………………………………………………………………………
i a
…………………………………………………………………………………………

o n
o
3. Explain the effect of the following statements:

p
(i)
l
int x = 10, *px = &x;
i
sun (ii)

(iii)
char *pc;

int x;
void *ptr = &x;
*(int *) ptr = 10;

…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………

10.5 POINTER ARITHMETIC

Pointer variables can also be used in arithmetic expressions. The following


operations can be carried out on pointers:

1. Pointers can be incremented or decremented to point to different locations like


27
Structures, Pointers ptr1 = ptr2 + 3;
and File Handling
ptr ++;
-- ptr;

However, ptr++ will cause the pointer ptr to point the next address value of its type.
For example, if ptr is a pointer to float with an initial value of 65526, then after the
operation ptr ++ or ptr = ptr+1, the value of ptr would be 65530. Therefore, if we
increment or decrement a pointer, its value is increased or decreased by the length of
the data type that it points to.

2. If ptr1 and ptr2 are properly declared and initialized pointers, the following
operations are valid:

res = res + *ptr1;


*ptr1 = *ptr2 + 5;
prod = *ptr1 * *ptr2;
quo = *ptr1 / *ptr2;

Note that there is a blank space between / and * in the last statement because if you
write /* together, then it will be considered as the beginning of a comment and the
o m
statement will fail.
. c
o t
3. Expressions like ptr1 == ptr2, ptr1 < ptr2, and ptr2 != ptr1 are permissible

comparisons are common in handling arrays.


s p
provided the pointers ptr1 and ptr2 refer to same and related variables. These

o g
Suppose p1 and p2 are pointers to related variables. The following operations cannot
work with respect to pointers:
b l
6 .
1. Pointer variables cannot be added. For example, p1 = p1 + p2 is not valid.

0 0
2. Multiplication or division of a pointer with a constant is not allowed. For

i a
example, p1 * p2 or p2 / 5 are invalid.

o n
3. An invalid pointer reference occurs when a pointer’s value is referenced even

p o though the pointer doesn’t point to a valid block. Suppose p and q are two
pointers. If we say, p = q; when q is uninitialized. The pointer p will then

i l become uninitialized as well, and any reference to *p is an invalid pointer


reference.

u n
s 10.6 PASSING POINTERS TO FUNCTIONS

As we have studied in the FUNCITONS that arguments can generally be passed to


functions in one of the two following ways:

1. Pass by value method


2. Pass by reference method

In the first method, when arguments are passed by value, a copy of the values of
actual arguments is passed to the calling function. Thus, any changes made to the
variables inside the function will have no effect on variables used in the actual
argument list.

However, when arguments are passed by reference (i.e. when a pointer is passed as
an argument to a function), the address of a variable is passed. The contents of that
address can be accessed freely, either in the called or calling function. Therefore, the
function called by reference can change the value of the variable used in the call.
28
Pointers
Here is a simple program that illustrates the difference.

Example 10.8

Write a program to swap the values using the pass by value and pass by reference
methods.

/* Program that illustrates the difference between ordinary arguments, which are
passed by value, and pointer arguments, which are passed by reference */

# include <stdio.h>
main()
{
int x = 10;
int y = 20;
void swapVal ( int, int ); /* function prototype */
void swapRef ( int *, int * );
printf("PASS BY VALUE METHOD\n");
/*function prototype*/

o m
printf ("Before calling function swapVal x=%d y=%d",x,y);
. c
swapVal (x, y); /* copy of the arguments are passed */
printf ("\nAfter calling function swapVal x=%d y=%d",x,y);
o t
printf("\n\nPASS BY REFERENCE METHOD");
printf ("\nBefore calling function swapRef x=%d y=%d",x,y);
s p
swapRef (&x,&y); /*address of arguments are passed */
printf("\nAfter calling function swapRef x=%d y=%d",x,y);
o g
}
b l
/* Function using the pass by value method*/
6 .
void swapVal (int x, int y)
{
0 0
int temp;
temp = x;
i a
x = y;
o n
y = temp;

p o
printf ("\nWithin function swapVal x=%d y=%d",x,y);

}
i l
return;

u n
s/*Function using the pass by reference method*/
void swapRef (int *px, int *py)
{
int temp;
temp = *px;
*px = *py;
*py = temp;
printf ("\nWithin function swapRef *px=%d *py=%d",*px,*py);
return;
}

OUTPUT

PASS BY VALUE METHOD


Before calling function swapVal x=10 y=20
Within function swapVal x=20 y=10
After calling function swapVal x=10 y=20

29
Structures, Pointers PASS BY REFERENCE METHOD
and File Handling
Before calling function swapRef x=10 y=20
Within function swapRef *px=20 *py=10
After calling function swapRef x=20 y=10

This program contains two functions, swapVal and swapRef.

In the function swapVal, arguments x and y are passed by value. So, any changes to
the arguments are local to the function in which the changes occur. Note the values of
x and y remain unchanged even after exchanging the values of x and y inside the
function swapVal.

Now consider the function swapRef. This function receives two pointers to integer
variables as arguments identified as pointers by the indirection operators that appear
in argument declaration. This means that in the function swapRef, arguments x and y
are passed by reference. So, any changes made to the arguments inside the function
swapRef are reflected in the function main( ). Note the values of x and y is
interchanged after the function call swapRef.

10.6.1 A Function returning more than one value


o m
. c
Using call by reference method we can make a function return more than one value at
t
a time, which is not possible in the call by value method. The following program will
o
makes you the concept very clear.

s p
Example 10.9

o g
are given by the user.
b l
Write a program to find the perimeter and area of a rectangle, if length and breadth

6 .
0
/* Program to find the perimeter and area of a rectangle*/

0
i a
#include <stdio.h>
void main()
{
o n
float len,br;

p o float peri, ar;

i l void periarea(float length, float breadth, float *, float *);


printf("\nEnter the length and breadth of a rectangle in metres: \n");

u n scanf("%f %f",&len,&br);
periarea(len,br,&peri,&ar);
s printf("\nPerimeter of the rectangle is %f metres", peri);
printf("\nArea of the rectangle is %f sq. metres", ar);
}

void periarea(float length, float breadth, float *perimeter, float *area)


{
*perimeter = 2 * (length +breadth);
*area = length * breadth;
}

OUTPUT

Enter the length and breadth of a rectangle in metres:


23.0 3.0
Perimeter of the rectangle is 52.000000 metres
Area of the rectangle is 69.000000 sq. metres

30
Here in the above program, we have seen that the function periarea is returning two Pointers
values. We are passing the values of len and br but, addresses of peri and ar. As we
are passing the addresses of peri and ar, any change that we make in values stored at
addresses contained in the variables *perimeter and *area, would make the change
effective even in main() also.

10.6.2 Function returning a pointer


A function can also return a pointer to the calling program, the way it returns an int, a
float or any other data type. To return a pointer, a function must explicitly mention in
the calling program as well as in the function prototype. Let’s illustrate this with an
example:

Example: 10.10

Write a program to illustrate a function returning a pointer.

/*Program that shows how a function returns a pointer */

# include<stdio.h>
o m
void main( )
. c
{
o t
float *a;
float *func( ); /* function prototype */
s p
a = func( );
printf ("Address = %u", a);
o g
}
b l
float *func( )
{
6 .
float r = 5.2;
return (&r);
0 0
}
i a
OUTPUT
o n
Address = 65516
p o
i l
This program only shows how a function can return a pointer. This concept will be
n
used later while handling arrays.
u
sCheck Your Progress 2

1. Tick mark ( √ )whether each of the following statements are true or false.

(i) An integer is subtracted from a pointer variable. True False

(ii) Pointer variables can be compared. True False

(iii) Pointer arguments are passed by value. True False

(iv) Value of a local variable in a function can be


changed by another function. True False

(v) A function can return more than one value. True False

(vi) A function can return a pointer. True False

31
Structures, Pointers
and File Handling
10.7 ARRAYS AND POINTERS

Pointers and arrays are so closely related. An array declaration such as int arr[ 5 ]
will lead the compiler to pick an address to store a sequence of 5 integers, and arr is a
name for that address. The array name in this case is the address where the sequence
of integers starts. Note that the value is not the first integer in the sequence, nor is it
the sequence in its entirety. The value is just an address.

Now, if arr is a one-dimensional array, then the address of the first array element can
be written as &arr[0] or simply arr. Moreover, the address of the second array
element can be written as &arr[1] or simply (arr+1). In general, address of array
element (i+1) can be expressed as either &arr[ i] or as (arr+ i). Thus, we have two
different ways for writing the address of an array element. In the latter case i.e,
expression (arr+ i) is a symbolic representation for an address rather than an
arithmetic expression. Since &arr[ i] and (ar+ i) both represent the address of the ith
element of arr, so arr[ i] and *(ar + i) both represent the contents of that address i.e.,
the value of ith element of arr.

o m
Note that it is not possible to assign an arbitrary address to an array name or to an
.
array element. Thus, expressions such as arr, (arr+ i) and arr[ i] cannot appear on c
o t
the left side of an assignment statement. Thus we cannot write a statement such as:

&arr[0] = &arr[1]; /* Invalid */


s p
o g
However, we can assign the value of one array element to another through a pointer,
for example,
b l
ptr = &arr[0];
6 .
/* ptr is a pointer to arr[ 0] */
arr[1] = *ptr;

0 0
/* Assigning the value stored at address to arr[1] */

a
Here is a simple program that will illustrate the above-explained concepts:
i
o n
Example 10.11

o
lp
/* Program that accesses array elements of a one-dimensional array using pointers */

ni
# include<stdio.h>
main()

su {

int arr[ 5 ] = {10, 20, 30, 40, 50};


int i;

for (i = 0; i < 5; i++)


{
printf ("i=%d\t arr[i]=%d\t *(arr+i)=%d\t", i, arr[i], *(arr+i));
printf ("&arr[i]=%u\t arr+i=%u\n", &arr[i], (arr+i)); }
}

OUTPUT:

i=0 arr[i]=10 *(arr+i)=10 &arr[i]=65516 arr+i=65516


i=1 arr[i]=20 *(arr+i)=20 &arr[i]=65518 arr+i=65518
i=2 arr[i]=30 *(arr+i)=30 &arr[i]=65520 arr+i=65520
i=3 arr[i]=40 *(arr+i)=40 &arr[i]=65522 arr+i=65522
i=4 arr[i]=50 *(arr+i)=50 &arr[i]=65524 arr+i=65524
32
Pointers
Note that i is added to a pointer value (address) pointing to integer data type (i.e., the
array name) the result is the pointer is increased by i times the size (in bytes) of
integer data type. Observe the addresses 65516, 65518 and so on. So if ptr is a char
pointer, containing addresses a, then ptr+1 is a+1. If ptr is a float pointer, then ptr+
1 is a+ 4.

Pointers and Multidimensional Arrays


C allows multidimensional arrays, lays them out in memory as contiguous locations,
and does more behind the scenes address arithmetic. Consider a 2-dimensional array.

int arr[ 3 ][ 3 ] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

The compiler treats a 2 dimensional array as an array of arrays. As you know, an


array name is a pointer to the first element within the array. So, arr points to the first
3-element array, which is actually the first row (i.e., row 0) of the two-dimensional
array. Similarly, (arr + 1) points to the second 3-element array (i.e., row 1) and so
on. The value of this pointer, *(arr + 1), refers to the entire row. Since row 1 is a one-
o m
dimensional array, (arr + 1) is actually a pointer to the first element in row 1. Now
add 2 to this pointer. Hence, (*(arr + 1) + 2) is a pointer to element 2 (i.e., the third
. c
element) in row 1. The value of this pointer, *(*(arr + 1) + 2), refers to the element
in column 2 of row 1. These relationships are illustrated below:
o t
s p
arr 1 2 3
o g
First 1-d array

4
y
5 6
b l
(arr+1)

6 . Second 1-d array

(arr + 2) 7
0 8 0 9 Third 1-d array

i a
o
*(arr + 2) n *(*(arr+2) + 2)

p o
l
10.8 ARRAY OF POINTERS
i
u n
The way there can be an array of integers, or an array of float numbers, similarly,

sthere can be array of pointers too. Since a pointer contains an address, an array of
pointers would be a collection of addresses. For example, a multidimensional array
can be expressed in terms of an array of pointers rather than a pointer to a group of
contiguous arrays.

Two-dimensional array can be defined as a one-dimensional array of integer pointers


by writing:

int *arr[3];

rather than the conventional array definition,

int arr[3][5];

Similarly, an n-dimensional array can be defined as (n-1)-dimensional array of


pointers by writing

data-type *arr[subscript 1] [subscript 2]…. [subscript n-1]; 33


Structures, Pointers
and File Handling
The subscript1, subscript2 indicate the maximum number of elements associated with
each subscript.

Example 10.12

Write a program in which a two-dimensional array is represented as an array of


integer pointers to a set of single-dimensional integer arrays.

/* Program calculates the difference of the corresponding elements of two table of


integers */

# include <stdio.h>
# include <stdlib.h>
# define MAXROWS 3
void main( )
{
int *ptr1[MAXROWS], *ptr2 [MAXROWS], *ptr3 [MAXROWS];
int rows, cols, i, j;
void inputmat (int *[ ], int, int);
o m
void dispmat (int *[ ], int, int);
. c
void calcdiff (int *[ ], int *[ ], int *[ ], int, int);

o t
printf ("Enter no. of rows & columns \n");
scanf ("%d%d", &rows, &cols);
s p
for (i = 0; i < rows; i++)
o g
{
b l
6 .
ptr1[ i ] = (int *) malloc (cols * sizeof (int));
ptr2[ i ] = (int *) malloc (cols * sizeof (int));

}
0 0
ptr3[ i ] = (int *) malloc (cols * sizeof (int));

i a
printf ("Enter values in first matrix \n");

o n inputmat (ptr1, rows, cols);

p o printf ("Enter values in second matrix \n");


inputmat (ptr2, rows, cols);

i l calcdiff (ptr1, ptr2, ptr3, rows, cols);


printf ("Display difference of the two matrices \n");

u n dispmat (ptr3, rows, cols);

s }

void inputmat (int *ptr1[MAXROWS], int m, int n)


{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf ("%d", (*(ptr1 + i) + j));
}
}
return;
}

void dispmat (int *ptr3[ MAXROWS ], int m, int n)


{
int i, j;
34
for (i = 0; i < m; i++) Pointers
{
for (j = 0; j < n; j++)
{
printf ("%d ", *(*(ptr3 + i) + j));
}
printf("\n");
}
return;
}

void calcdiff (int *ptr1[ MAXROWS ], int *ptr2 [ MAXROWS ],


int *ptr3[MAXROWS], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
*(*(ptr3 + i) + j) = *(*(ptr1 + i) + j) - *(*(ptr2 + i) + j);
o m
}
. c
}
return;
o t
}

s p
OUTPUT
o g
Enter no. of rows & columns
b l
3 3
Enter values in first matrix
6 .
2 6 3
5 9 3
0 0
1 0 2
Enter values in second matrix
i a
3 5 7
o n
2 8 2
1 0 1
p o
-1 1 -4
i l
Display difference of the two matrices

n
3 1 1
u
s 0 0 1

In this program, ptr1, ptr2, ptr3 are each defined as an array of pointers to integers.
Each array has a maximum of MAXROWS elements. Since each element of ptr1,
ptr2, ptr3 is a pointer, we must provide each pointer with enough memory for each
row of integers. This can be done using the library function malloc included in
stdlib.h header file as follows:

ptr1[ i ] = (int *) malloc (cols * sizeof (int));

This function reserves a block of memory whose size (in bytes) is equivalent to cols
* sizeof(int). Since cols = 3, so 3 * 2 (size of int data type) i.e., 6 is allocated to each
ptr1[ 1 ], ptr1[ 2 ] and ptr1[ 3 ]. This malloc function returns a pointer of type void.
This means that we can assign it to any type of pointer. In this case, the pointer is
type-casted to an integer type and assigned to the pointer ptr1[ 1 ], ptr1[ 2 ] and
ptr1[ 3 ]. Now, each of ptr1[ 1 ], ptr1[ 2 ] and ptr1[ 3 ] points to the first byte of the
memory allocated to the corresponding set of one-dimensional integer arrays of the
original two-dimensional array.
35
Structures, Pointers
and File Handling
The process of calculating and allocating memory at run time is known as dynamic
memory allocation. The library routine malloc can be used for this purpose.

Instead of using conventional array notation, pointer notation has been used for
accessing the address and value of corresponding array elements which has been
explained to you in the previous section. The difference of the array elements within
the function calcdiff is written as

*(*(ptr3 + i) + j) = *(*(ptr1 + i) + j) - *(*(ptr2 + i) + j);

10.9 POINTERS AND STRINGS

As we have seen in strings, a string in C is an array of characters ending in the null


character (written as '\0'), which specifies where the string terminates in memory.
Like in one-dimensional arrays, a string can be accessed via a pointer to the first
character in the string. The value of a string is the (constant) address of its first
character. Thus, it is appropriate to say that a string is a constant pointer.
A string can be declared as a character array or a variable of type char *. The
o m
declarations can be done as shown below:
. c
char country[ ] = "INDIA";
o t
char *country = "INDIA";

s p
o g
Each initialize a variable to the string “INDIA”. The second declaration creates a
pointer variable country that points to the letter I in the string "INDIA" somewhere in
memory.
b l
6 .
Once the base address is obtained in the pointer variable country, *country would

0
yield the value at this address, which gets printed through,

0
a
printf ("%s", *country);
i
o n
Here is a program that dynamically allocates memory to a character pointer using the

p o library function malloc at run-time. An advantage of doing this way is that a fixed
block of memory need not be reserved in advance, as is done when initializing a

i l conventional character array.

sun Example 10.13

Write a program to test whether the given string is a palindrome or not.

/* Program tests a string for a palindrome using pointer notation */

# include <stdio.h>
# include <conio.h>
# include <stdlib.h>

main()
{
char *palin, c;
int i, count;

short int palindrome(char,int); /*Function Prototype */


palin = (char *) malloc (20 * sizeof(char));
printf("\nEnter a word: ");
do
36
{ Pointers
c = getchar( );
palin[i] = c;
i++;
}while (c != '\n');

i = i-1;
palin[i] = '\0';
count = i;

if (palindrome(palin,count) == 1)
printf ("\nEntered word is not a palindrome.");
else
printf ("\nEntered word is a palindrome");
}

short int palindrome(char *palin, int len)


{
short int i = 0, j = 0;
for(i=0 , j=len-1; i < len/2;i++,j--)
o m
{
. c
if (palin[i] == palin[j])
continue;
o t
else
return(1);
s p
}
return(0);
o g
}
b l
OUTPUT
6 .
Enter a word: malayalam
0 0
Entered word is a palindrome.
i a
o n
Enter a word: abcdba

p o
l
Entered word is not a palindrome.
i
u n
Array of pointers to strings
sArrays may contain pointers. We can form an array of strings, referred to as a string
array. Each entry in the array is a string, but in C a string is essentially a pointer to its
first character, so each entry in an array of strings is actually a pointer to the first
character of a string. Consider the following declaration of a string array:

char *country[ ] = {
“INDIA”, “CHINA”, “BANGLADESH”, “PAKISTAN”, “U.S”
};

The *country[ ] of the declaration indicates an array of five elements. The char* of
the declaration indicates that each element of array country is of type “pointer to
char”. Thus, country [0] will point to INDIA, country[ 1] will point to CHINA, and
so on.

Thus, even though the array country is fixed in size, it provides access to character
strings of any length. However, a specified amount of memory will have to be
allocated for each string later in the program, for example, 37
Structures, Pointers
and File Handling
country[ i ] = (char *) malloc(15 * sizeof (char));

The country character strings could have been placed into a two-dimensional array
but such a data structure must have a fixed number of columns per row, and that
number must be as large as the largest string. Therefore, considerable memory is
wasted when a large number of strings are stored with most strings shorter than the
longest string.

As individual strings can be accessed by referring to the corresponding array element,


individual string elements be accessed through the use of the indirection operator.
For example, * ( * country + 3 ) + 2 ) refers to the third character in the fourth string
of the array country. Let us see an example below.

Example 10.14

Write a program to enter a list of strings and rearrange them in alphabetical order,
using a one-dimensional array of pointers, where each pointer indicates the beginning
of a string:

o m
c
/* Program to sort a list of strings in alphabetical order using an array of pointers */
.
# include <stdio.h>
o t
# include <conio.h>
# include <stdlib.h>
s p
# include <string.h>
o g
void readinput (char *[ ], int);
b l
6 .
void writeoutput (char *[ ], int);
void reorder (char *[ ], int);

main( )
0 0
{
i a
char *country[ 5 ];

o n int i;

p o for (i = 0; i < 5; i++)


{

i l }
country[ i ] = (char *) malloc (15 * sizeof (char));

u n printf ("Enter five countries on a separate line\n");

s readinput (country, 5);


reorder (country, 5);
printf ("\nReordered list\n");
writeoutput (country, 5);
getch( );
}

void readinput (char *country[ ], int n)


{
int i;
for (i = 0; i < n; i++)
{ scanf ("%s", country[ i ]); }
return;
}

void writeoutput (char *country[ ], int n)


{
int i;
38
for (i = 0; i < n; i++) Pointers
{ printf ("%s", country[ i ]);
printf ("\n"); }
return;
}

void reorder (char *country[ ], int n)


{
int i, j;
char *temp;
for (i = 0; i < n-1; i++)
{
for (j = i+1; j < n; j++)
{
if (strcmp (country[ i ], country[ j ]) > 0)
{
temp = country[ i ];
country[ i ] = country[ j ];

}
country[ j ] = temp;

o m
}
. c
}
return;
o t
}

s p
OUTPUT
o g
Enter five countries on a seperate line
b l
INDIA
BANGLADESH
6 .
PAKISTAN
CHINA
0 0
SRILANKA
i a
Reordered list
o n
BANGLADESH
CHINA
p o
INDIA
PAKISTAN
i l
n
SRILANKA
u
sThe limitation of the string array concept is that when we are using an array of
pointers to strings we can initialize the strings at the place where we are declaring the
array, but we cannot receive the strings from keyboard using scanf( ).

Check Your Progress 3

1. What is meant by array of pointers?

…………………………………………………………………………………………
…………………………………………………………………………………………
2. How the indirection operator can be used to access a multidimensional array
element.

…………………………………………………………………………………………
…………………………………………………………………………………………
39
Structures, Pointers 3. A C program contains the following declaration.
and File Handling
float temp[ 3 ][ 2 ] = {{13.4, 45.5}, {16.6, 47.8}, {20.2, 40.8}};

(i) What is the meaning of temp?


(ii) What is the meaning of (temp + 2)?
(iii) What is the meaning of *(temp + 1)?
(iv) What is the meaning of (*(temp + 2) + 1)?
(v) What is the meaning of *(*(temp) + 1) + 1)?
(vi) What is the meaning of *(*(temp + 2))?

……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………

10.10 SUMMARY

In this unit we have studied about pointers, pointer arithmetic, passing pointers to
o m
functions, relation to arrays and the concept of dynamic memory allocation. A
. c
pointer is simply a variable that contains an address which is a location of another
t
variable in memory. The unary operator &, when preceded by any variable returns its
o
returns a value stored at that address.
s p
address. C’s other unary pointer operator is *, when preceded by a pointer variable

o g
Pointers are often passed to a function as arguments by reference. This allows data

b l
items within the calling function to be accessed, altered by the called function, and
then returned to the calling function in the altered form. There is an intimate

6 .
relationship between pointers and arrays as an array name is really a pointer to the

0 0
first element in the array. Access to the elements of array using pointers is enabled
by adding the respective subscript to the pointer value (i.e. address of zeroth element)

a
and the expression preceded with an indirection operator.
i
n
As pointer declaration does not allocate memory to store the objects it points at,
o
p o therefore, memory is allocated at run time known as dynamic memory allocation. The
library routine malloc can be used for this purpose.

i l
sun 10.11 SOLUTIONS / ANSWERS

Check Your Progress 1

1. Refer to section 10.4. The data type included in the pointer declaration, refers to
the type of data stored at the address which we will be storing in our pointer.

2. (i) Compile-time Error : Lvalue Required. Means that the left side of an
assignment operator must be an addressable expression that include a variable
or an indirection through a pointer.

(ii) Multiplication of a pointer variable with a constant is invalid.

3. (i) Refer section 10.4


(ii) Refer section 10.4
(iii) This means pointers can be of type void but can’t be de-referenced without
explicit casting. This is because the compiler can’t determine the size of the
object the pointer points to.
40
Pointers

Check Your Progress 2

1 (i) True.
(ii) True.
(iii) False.
(iv) True.
(v) True.
(vi) True.

Check Your Progress 3

1. Refer section 10.4.

2. Refer section 10.4 to comprehend the convention followed.

3. (i) Refers to the base address of the array temp.

(ii) Address of the first element of the last row of array temp i.e. address of
o m
element 20.2.
. c
(iii) Will give you 0. To get the value of the last element of the first array i.e. the
o t
correct syntax would be *(*(temp+0)+1).

s p
(iv) Address of the last element of last row of array temp i.e. of 40.8.
o g
l
(v) Displays the value 47.8 i.e., second element of last row of array temp.
b
6 .
(vi) Displays the value 20.2 i.e., first element of last row of array temp.

0 0
10.12 FURTHER READINGS
i a
o n
1. Programming with C, Second Edition, Gottfried Byron S, Tata McGraw Hill,
India.

p o
2. The C Programming Language, Second Edition, Brian Kernighan and Dennis

i l
Richie, PHI, 2002.
3. Programming in ANSI C, Second Edition, Balaguruswamy E, Tata McGraw Hill,

u n
India, 2002.

s4. How to Solve it by Computer, R.G.Dromey, PHI, 2002.


5. C Programming in 12 easy lessons, Greg Perry, SAMS, 2002.
6. Teach Yourself C in 21 days, Fifth Edition, Peter G, Fifth edition,SAMS, 2002.

41
Structures, Pointers
and File Handling
UNIT 11 THE C PREPROCESSOR

Structure
11.0 Introduction
11.1 Objectives
11.2 # define to Implement Constants
11.3 # define to Create Functional Macros
11.4 Reading from Other Files using # include
11.5 Conditional Selection of Code using #ifdef
11.5.1 Using #ifdef for different computer types
11.5.2 Using #ifdef to temporarily remove program statements
11.6 Other Preprocessor Commands
11.7 Predefined Names Defined by Preprocessor
11.8 Macros Vs Functions
11.9 Summary
11.10 Solutions / Answers
11.11 Further Readings
o m
. c
11.0 INTRODUCTION
o t
s p
Theoretically, the “preprocessor” is a translation phase that is applied to the source
code before the compiler gets its hands on it. The C Preprocessor is not part of the

o g
compiler, but is a separate step in the compilation process. C Preprocessor is just a
text substitution tool, which filters your source code before it is compiled. The
l
preprocessor more or less provides its own language, which can be a very powerful
b
symbol #.
6 .
tool for the programmer. All preprocessor directives or commands begin with the

0 0
The preprocessor makes programs easier to develop, read and modify. The

i a
preprocessor makes C code portable between different machine architectures &
customizes the language.

o n
p o The preprocessor performs textual substitutions on your source code in three ways:

i l File inclusion: Inserting the contents of another file into your source file, as if you
had typed it all in there.

u n Macro substitution: Replacing instances of one piece of text with another.


Conditional compilation: Arranging that, depending on various circumstances,
s certain parts of your source code are seen or not seen by the compiler at all.

The next three sections will introduce these three preprocessing functions. The syntax
of the preprocessor is different from the syntax of the rest of C program in several
respects. The C preprocessor is not restricted to use with C programs, and
programmers who use other languages may also find it useful. However, it is tuned to
recognize features of the C language like comments and strings.

11.1 OBJECTIVES

After going through this unit, you will be able to:

• define, declare preprocessor directives;


• discuss various preprocessing directives, for example file inclusion, macro
substitution, and conditional compilation; and
• discuss various syntaxes of preprocessor directives and their applications.
42
The C Preprocessor

11.2 # define TO IMPLEMENT CONSTANTS


The preprocessor allows us to customize the language. For example to replace { and }
of C language to begin and end as block-statement delimiters (as like the case in
PASCAL) we can achieve this by writing:
# define begin {
# define end }

During compilation all occurrences of begin and end get replaced by corresponding
{ and }. So the subsequent C compilation stage does not know any difference!

#define is used to define constants.

The syntax is as follows:


# define <literal> <replacement-value>

literal is identifier which is replaced with replacement-value in the program.


o m
For Example,
. c
o t
#define MAXSIZE 256
#define PI 3.142857
s p
o
The C preprocessor simply searches through the C code before it is compiled and g
replaces every instance of MAXSIZE with 256.
b l
# define FALSE 0
6 .
# define TRUE !FALSE

0 0
i a
The literal TRUE is substituted by !FALSE and FALSE is substituted by the value 0 at
every occurrence, before compilation of the program. Since the values of the literal

o n
are constant throughout the program, they are called as constant.

p o
The syntax of above # define can be rewritten as:

i l
# define <constant-name> <replacement-value>

u n
sLet us consider some examples,

# define M 5
# define SUBJECTS 6
# define PI 3.142857
# define COUNTRY INDIA

Note that no semicolon (;) need to be placed as the delimiter at the end of a # define
line. This is just one of the ways that the syntax of the preprocessor is different from
the rest of C statements (commands). If you unintentionally place the semicolon at the
end as below:

#define MAXLINE 100; /* WRONG */

and if you declare as shown below in the declaration section,

char line[MAXLINE];

43
Structures, Pointers the preprocessor will expand it to:
and File Handling

char line[100;]; /* WRONG */

which gives you the syntax error. This shows that the preprocessor doesn’t know
much of anything about the syntax of C.

11.3 # define TO CREATE FUNCTIONAL MACROS

Macros are inline code, which are substituted at compile time. The definition of a
macro is that which accepts an argument when referenced. Let us consider an example
as shown below:

Example 11.1

Write a program to find the square of a given number using macro.

/* Program to find the square of a number using marco*/


#include <stdio.h>
o m
# define SQUARE(x) (x*x)
. c
main()
{
o t
int v,y;
printf("Enter any number to find its square: ");
s p
scanf("%d", &v);
y = SQUARE(v);
o g
b l
printf("\nThe square of %d is %d", v, y);
}

6 .
OUTPUT
0 0
a
Enter any number to find its square: 10
i
n
The square of 10 is 100

o
p o In this case, v is equated with x in the macro definition of square, so the variable y is
assigned the square of v. The brackets in the macro definition of square are necessary

i l for correct evaluation. The expansion of the macro becomes:

sun y =( v * v);

Macros can make long, ungainly pieces of code into short words. Macros can also
accept parameters and return values. Macros that do so are called macro functions. To
create a macro, simply define a macro with a parameter that has whatever name you
like, such as my_val. For example, one macro defined in the standard libraries is
“abs”, which returns the absolute value of its parameter. Let us define our own version
of ABS as shown below. Note that we are defining it in upper case not only to avoid
conflicting with the existing “abs”.

#define ABS(my_val) ((my_val) < 0) ? -(my_val) : (my_val)

#define can also be given arguments which are used in its replacement. The
definitions are then called macros. Macros work rather like functions, but with the
following minor differences:

• Since macros are implemented as a textual substitution, by this the performance of


program improves compared to functions.
• Recursive macros are generally not a good idea.
44
• Macros don’t care about the type of their arguments. Hence macros are a good The C Preprocessor
choice where we want to operate on reals, integers or a mixture of the two.
Programmers sometimes call such type flexibility polymorphism.
• Macros are generally fairly small.

Let us look more illustrative examples to understand the macros concept.

Example 11.2

Write a program to declare constants and macro functions using #define.

/* Program to illustrate the macros */


#include <stdio.h>
#include <string.h>
#define STR1 "A macro definition!\n"
#define STR2 "must be all on one line!\n"
#define EXPR1 1+2+3
#define EXPR2 EXPR1+5
#define ABS(x) (((x) < 0) ? – (x):(x))
#define MAX(p,q) ((p < q) ? (q):(p))
o m
#define BIGGEST(p,q,r) (MAX(p, q) < r)?(r):(MAX(p, q))
. c
main()
{
o t
printf(STR1);
printf(STR2);
s p
printf("Largest number among %d, %d and %d is %d\n",EXPR1, EXPR2, ABS (–3),
BIGGEST(1,2,3));
o g
}
b l
OUTPUT
6 .
A macro definition
0 0
must be all on one line!
i a
n
Largest number among 6, 11 and 3 is 3

o
p o
The macro STR1 is replaced with “A macro definition \n” in the first printf()
function. The macro STR2 is replaced with “must be all on one line! \n” in the

i l
second printf function. The macro EXPR1 is replaced with 1+2+3 in third printf
statement. The macro EXPR2 is replaced with EXPR1 +5 in fourth printf statement.

u n
The macro ABS(–3) is replaced with (– 3<0) ? – (– 3) : 3. And evaluation 3 is

sreplaced. The largest among the three numbers is diplayed.

Example 11.3

Write a program to find out square and cube of any given number using macros.

/* Program to find the square and cube of any given number using macro directive */
# include<stdio.h>
# define sqr(x) (x * x)
# define cub(x) (sqr(x) * x)
main()
{
int num;
printf(“Enter a number: ”);
scanf(“%d”, &num);
printf(“ \n Square of the number is %d”, sqr(num));
printf(“ \n Cube of the number is %d\n”, cub(num));
}
45
Structures, Pointers
and File Handling
OUTPUT

Enter a number: 5
Square of the number is 25
Cube of the number is 125

Note: Multi-line macros can be defined by placing a backward slash ( \ ) at end of


each line except the last line. This feature permits a single macro (i.e. a single
identifier) to represent a compound statement.

Example 11.4

Write a macro to display the string COBOL in the following fashion

C
CO
COB
COBO
COBOL
o m
COBOL
. c
COBO
COB
o t
CO
C
s p
o g
/* Program to display the string as given in the problem*/
# include<stdio.h>
b l
# define LOOP
{
6 .for(x=0; x<5; x++)
y=x+1;
\
\

0 0 printf(“%-5.*s\n”, y, string); } \
for(x=4; x>=0; x--) \

i a { y=x+1;
printf(“%-5.*s \n”, y, string); }
\

o n
p o main()
{

i l int x, y;
static char string[ ] = “COBOL”;

u n printf(“\n”);

s }
LOOP;

When the above program is executed the reference to macro (loop) is replaced by the
set of statements contained within the macro definition.

OUTPUT

C
CO
COB
COBO
COBOL
COBOL
COBO
COB
CO
C
46
The C Preprocessor
Recollect that CALL BY VALUE Vs CALL BY REFERENCE given in the previous
uint. By CALL BY VALUE, the swapping was not taking place, because the visibility
of the variables was restricted to with in the function in the case of local variables.
You can resolve this by using a macro. Here is swap in action when using a macro:

#define swap(x, y) {int tmp = x; x = y; y = tmp; }

Now we have swapping code that works. Why does this work? It is because the CPP
just simply replaces text. Wherever swap is called, the CPP will replace the macro call
with the macro meaning, (defined text).

Caution in using macros

You should be very careful in using Macros. In particular the textual substitution
means that arithmetic expressions are liable to be corrupted by the order of evaluation
rules (precedence rules). Here is an example of a macro, which won’t work.

#define DOUBLE(n) n+n


o m
Now if we have a statement,
. c
z = DOUBLE(p) * q;
o t
This will be expanded to
s p
z = p + p * q;
o g
b l
.
And since * has a higher priority than +, the compiler will treat it as.

6
z = p + (p * q);
0 0
a
The problem can be solved using a more robust definition of DOUBLE
i
#define DOUBLE(n)
o n (n + n)

p o
Here, the braces around the definition force the expression to be evaluated before any

l
surrounding operators are applied. This should make the macro more reliable.
i
u n
Check Your Progress 1

s1. Write a macro to evaluate the formula f(x) = x*x + 2*x + 4.


……………………………………………………………………………..……………
…………………………………………………………………………………………..
2. Define a preprocessor macro swap(t, x, y) that will swap two arguments x and y of
a given type t.

…………………………………………………………………………………………
…………………………………………………………………………………………
3. Define a macro called AREA, which will calculate the area of a circle in terms of
radius.

…………………………………………………………………………………………
…………………………………………………………………………………………
47
Structures, Pointers 4. Define a macro called CIRCUMFERENCE, which will calculate the circumference
and File Handling
of a circle in terms of radius.
…………………………………………………………………………………………
…………………………………………………………………………………………
5. Define a macro to display multiplication table.

…………………………………………………………………………………………
…………………………………………………………………………………………
6. Define a macro to find sum of n numbers.

…………………………………………………………………………………………
…………………………………………………………………………………………
………………………………………………………………………………………….

11.4 READING FROM OTHER FILES USING # include


o m
. c
The preprocessor directive #include is an instruction to read in the entire contents of

o t
another file at that point. This is generally used to read in header files for library
functions. Header files contain details of functions and types used within the library.

syntax is as follows:
s p
They must be included before the program can make use of the library functions. The

#include <filename.h>
o g
b l
or

6 .
#include “filename.h”
0 0
a
The above instruction causes the contents of the file “filename.h” to be read, parsed,
i
o n
and compiled at that point. The difference between the suing of # and “ ” is that,
where the preprocessor searches for the filename.h. For the files enclosed in < > (less

p o than and greater than symbols) the search will be done in standard directories (include
directory) where the libraries are stored. And in case of files enclosed in “ ” (double

i l quotes) search will be done in “current directory” or the directory containing the
source file. Therefore, “ ” is normally used for header files you’ve written, and # is

u n normally used for headers which are provided for you (which someone else has

s written).

Library header file names are enclosed in angle brackets, < >. These tell the
preprocessor to look for the header file in the standard location for library definitions.
This is /usr/include for most UNIX systems. And c:/tc/include for turbo compilers
on DOS / WINDOWS based systems.

Use of #include for the programmer in multi-file programs, where certain information
is required at the beginning of each program file. This can be put into a file by name
“globals.h” and included in each program file by the following line:

#include "globals.h"

If we want to make use of inbuilt functions related to input and output operations, no
need to write the prototype and definition of the functions. We can simply include the
file by writing:

#include <stdio.h>
48
The C Preprocessor
and call the functions by the function calls. The standard header file stdio.h is a
collection of function prototype (declarations) and definition related to input and
output operations.

The extension “.h”', simply stands for “header” and reflects the fact that #include
directives usually sit at the top (head) of your source files. “.h” extension is not
compulsory – you can name your own header files anything you wish to, but .h is
traditional, and is recommended.

Placing common declarations and definitions into header files means that if they
always change, they only have to be changed in one place, which is a much more
feasible system.

What should you put in header files?


• External declarations of global variables and functions.
• Structure definitions.
• Typedef declarations.

However, there are a few things not to put in header files:


o m
• Defining instances of global variables. If you put these in a header file, and
. c
include the header file in more than one source file, the variable will end up
multiply defined.
o t
• Function bodies (which are also defining instances), may not be put in header files.
Since these headers may end you up with multiple copies of the function and
s p
o
hence “multiply defined” errors. People sometimes put commonly-used functions
g
b l
in header files and then use #include to bring them (once) into each program
where they use that function, or use #include to bring together the several source

6 .
files making up a program, but both of these are not good practice. It’s much
better to learn how to use your compiler or linker to combine together separately-
compiled object files.
0 0
i a
n
11.5 CONDITIONAL SELECTION OF CODE USING # ifdef
o
p o
The preprocessor has a conditional statement similar to C’s if-else. It can be used to

i l
selectively include statements in a program. The commands for conditional selection
are; #ifdef, #else and #endif.

u n
#ifdef
sThe syntax is as follows:

#ifdef IDENTIFIER_NAME
{
statements;
}
This will accept a name as an argument, and returns true if the name has a current
definition. The name may be defined using a # define, the -d option of the compiler, or
certain names which are automatically defined by the UNIX environment. If the
identifier is defined then the statements below #ifdef will be executed

#else
The syntax is as follows:

#else
{
statements; 49
Structures, Pointers }
and File Handling

#else is optional and ends the block started with #ifdef. It is used to create a 2 way
optional selection. If the identifier is not defined then the statements below #else will
be executed.

#endif

Ends the block started by #ifdef or #else.


Where the #ifdef is true, statements between it and a following #else or #endif are
included in the program. Where it is false, and there is a following #else, statements
between the #else and the following #endif are included. Let us look into the
illustrative example given below to get an idea.

Example 11.5

Define a macro to find maximum of 3 or 2 numbers using #ifdef , #else

/* Program to find maximum of 2 numbers using #ifdef*/


o m
#include<stdio.h>
. c
#define TWO
main()
o t
{
int a, b, c;
s p
clrscr();
o g
#ifdef TWO
b l
{
6 .
printf("\n Enter two numbers: \n");
0
scanf("%d %d", &a,&b);
0
if(a>b)

i a
printf("\n Maximum of two numbers is %d", a);

o n else
printf("\n Maximum is of two numbers is %d", b);

p o }

i l #endif

u n } /* end of main*/

s OUTPUT

Enter two numbers:


33
22
Maximum of two numbers is 33

Explanation

The above program demonstrate preprocessor derivative #ifdef. By using #ifdef


TWO has been defined. The program finds out the maximum of two numbers.

11.5.1 Using #ifdef for Different Computer Types

Conditional selection is rarely performed using #define values. This is often used
where two different computer types implement a feature in different ways. It allows
the programmer to produce a program, which will run on either type.
50
The C Preprocessor
A simple application using machine dependent values is illustrated below.

#include <stdio.h>
main()
{
#ifdef HP
{
printf("This is a HP system \n");
…………………….
……………………. /* code for HP systems*/
}
#endif

#ifdef SUN
{
printf("This is a SUN system \n");
……………………. /* code for SUN Systems
}
#endif
o m
}
. c
If we want the program to run on HP systems, we include the directive
o t
#define HP at the top of the program.

s p
If we want the program to run on SUN systems, we include the directive
#define SUN at the top of the program.
o g
b l
6 .
Since all you’re using the macro HP or SUN to control the #ifdef, you don’t need to
give it any replacement text at all. Any definition for a macro, even if the replacement
text is empty, causes an #ifdef to succeed.

0 0
11.5.2 a
Using #ifdef to Temporarily Remove Program
i
Statements
o n
o
#ifdef also provides a useful means of temporarily “blanking out” lines of a program.
p
i l
The lines in the program are preceded by #ifdef NEVER and followed by #endif. Of
course, you should ensure that the name NEVER isn’t defined anywhere.

u n
#include <stdio.h>

smain()
{
…………….
#ifdef NEVER
{
…………………….
……………………. /* code is skipped */
#endif
}

11.6 OTHER PREPROCESSOR COMMANDS

Other preprocessor commands are:

• #ifndef - If this macro is not defined


• #if - Test if a compile time condition is true
51
Structures, Pointers • #else - The alternative for #if. This is part of an #if preprocessor
and File Handling
statement and works in the same way with #if that the regular
C else does with the regular if.
• #elif - enables us to establish an “if…else…if ..” sequence for
testing multiple conditions.

Example 11.6

#if processor == intel


#define FILE “intel.h”
#elif processor == amd
#define FILE “amd.h”
#if processor == motrola
#define FILE “motrola.h”
#endif
#include FILE

• # - Stringizing operator, to be used in the definition of macro. This operator


allows a formal parameter within macro definition to be converted to a string.
o m
Example 11.7
. c
#define multiply (p*q) printf(#pq “ = %f”, pq)
o t
main()
{
s p
………..
o g
}
multiply(m*n);

b l
6 .
The preprocessor converts the line multiply(m*n) into printf(“m*n” “ = %f”, m*n);

0
And then into printf(“m*n = %f”, m*n);
0
i a
## - Token merge, creates a single token from two adjacent ones within a macro

n
definition.

o
p o Example 11.8

i l #define merge(s1,s2) s1## s2


main()

u n {

s }
…………..
printf(“%f”, merge(total, sales);

The preprocessor transforms the statement merge(total, sales) into printf(“%f”,


totalsales);

#error - text of error message -- generates an appropriate compiler error message.

Example 11.9

#ifdef OS_MSDOS
#include <msdos.h>
#elifdef OS_UNIX
#include ``default.h''
#else
52
#error Wrong OS!! The C Preprocessor
#endif

# line
#line number "string" – informs the preprocessor that the number is the next number
of line of input. "string" is optional and names the next line of input. This is most
often used with programs that translate other languages to C. For example, error
messages produced by the C compiler can reference the file name and line numbers of
the original source files instead of the intermediate C (translated) source files.

#pragma

It is used to turn on or off certain features. Pragmas vary from compiler to compiler.
Pragmas available with Microsoft C compilers deals with formatting source listing
and placing comments in the object file generated by the compiler. Pragmas
available with Turbo C compilers allows to write assembly language statements in C
program.

A control line of the form


o m
#pragma token-sequence
. c
o t
This causes the processor to perform an implementation-dependent action. An
unrecognized pragma is ignored.
s p
o
Other preprocessor directives are # - Stringizing operator allows a formal parameterg
b l
within macro definition to be converted to a string. ## - Token merge, creates a single
token from two adjacent ones within a macro definition. #error - generates an
appropriate compiler error message.
6 .
Example 11.10
0 0
i a
Write a macro to demonstrate #define, #if, #else preprocessor commands.

o n
o
/* The following code displays 35 to the screen. */

p
i l
#include <stdio.h>
#define CHOICE 100

u n
int my_int = 0;
#if (CHOICE == 100)
s#else
void set_my_int()
{ my_int = 35; }

void set_my_int()
{
my_int = 27;
}
#endif
main ()
{
set_my_int();
printf("%d\n", my_int);
}

OUTPUT

35
53
Structures, Pointers The my_int is initialized to zero and CHOICE is defined as 100. #if derivative checks
and File Handling
whether CHOICE is equal to 100. Since CHOICE is defined as 100, void set_my_int
is called and int is set 35. And the same is displayed on to the screen.

Example 11.11

Write a macro to demonstrate #define, #if, #else preprocessor commands.

/* The following code displays 27 on the screen */

#include <stdio.h>
#define CHOICE 100
int my_int = 0;
#undef CHOICE
#ifdef CHOICE
void set_my_int()
{
my_int = 35;

#else
}

o m
void set_my_int()
. c
{
my_int = 27;
o t
#endif
}

s p
main ()
o g
{
b l
set_my_int();

6 .
printf("%d\n", my_int);
}

0 0
OUTPUT
i a
o
27n
o
lp
The my_int is initialized to 0 and CHOICE is defined as 100. #undef is used to
undefine CHOICE. #else is invoked , void set_my_int is called and int is set 27. And

n i the same is displayed on to the screen.

su 11.7 PREDEFINED NAMES DEFINED BY


PREPROCESSOR
These are identifiers defined by the preprocessor, and cannot be undefined or
redefined. They are:
_LINE_ an integer constant containing the current source line number.
_FILE_ a string containing the name of the file being complied.
_DATE_ a string literal containing the date of compilation, in the form “mm-dd-
yyyy”.
_TIME_ a string literal containing the time of compilation, in the form “hh:mm:ss”.
_STDC_ the constant 1. This identifier is defined to be 1 only in the implementations
conforming to the ANSI standard.

54
The C Preprocessor
11.8 MACROS Vs FUNCTIONS
Till now we have discussed about macros. Any computations that can be done on
macros can also be done on functions. But there is a difference in implementations
and in some cases it will be appropriate to use macros than function and vice versa.
We will see the difference between a macro and a function now.

Macros Functions
Macro calls are replaced with macro In function call, the control is passed to a
expansions (meaning). function definition along with arguments,
and definition is processed and value may
be returned to call
Macros run programs faster but Functions make program size smaller and
increase the program size. compact.
If macro is called 100 numbers of If function is called 100 numbers of times,
times, the size of the program will the program size will not increase.
increase.
It is better to use Macros, when the
definition is very small in size.
It is better to use functions, when the
definition is bigger in size.
o m
. c
Check Your Progress 2
o t
1. Write an instruction to the preprocessor to include the math library math.h.
s p
…………………………………………………………………………………………
o g
…………………………………………………………………………………………
b l
6 .
2. Write a macro to add user defined header file by name madan.h to your program.
…………………………………………………………………………………………
0 0
…………………………………………………………………………………………
i a
o n
3. What will be the output of the following program?
#include<stdio.h>
main()
p o
{
i l
u n float m=7;
#ifdef DEF

s i*=i;
#else
printf("\n%f", m);
#endif }
…………………………………………………………………………………………
…………………………………………………………………………………………

4. Write a macro to find out whether the given character is upper case or not.
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………..

55
Structures, Pointers
and File Handling 11.9 SUMMARY
The preprocessor makes programs easier to develop and modify. The preprocessor
makes C code more portable between different machine architectures and customize
the language. The C Preprocessor is not part of the compiler, but is a separate step in
the compilation process. All preprocessor lines begin with #. C Preprocessor is just a
text substitution tool on your source code in three ways: File inclusion, Macro
substitution, and Conditional compilation.

File inclusion - inserts the contents of another file into your source file.
Macro Substitution - replaces instances of one piece of text with another.
Conditional Compilation - arranges source code depending on various circumstances.

11.10 SOLUTIONS / ANSWERS


Check Your Progress 1

1. # include<stdio.h>
# define f(x) x*x + 2 * x + 4
o m
main()
. c
{
int num;
o t
printf("enter value x: ");
scanf("%d",&num);
s p
printf("\nvalue of f(num) is %d", f(num));
}
o g
b l
2. # include<stdio.h>

6 .
# define swap(t, x, y) { t tmp = x; x = y; y = tmp; }
main( )
{
0 0
int
i a
a, b;

o n float p, q;
printf("enter integer values for a, b: ");

p o scanf("%d %d", &a, &b);


printf("\n Enter float values for p, q: ");

i l scanf("%f %f", &p, &q);


swap(int, a, b);

u n printf(" \n After swap the values of a and b are %d %d", a, b);

s swap(float, p, q);
printf("\n After swap the values of p and q are %f %f", p, q);
}

3. # include<stdio.h>
# define AREA(radius) 3.1415 * radius * radius
main( )
{
int radius;
printf(“Enter value of radius: ”);
scanf(“%d ”, &radius );
printf(“\nArea is %d”, AREA(radius));
}

4. # include<stdio.h>
# define CIRCUMFERENCE(radius) 2 * 3.1415 * radius

main( )
56
{ The C Preprocessor
int radius;
printf(“Enter value for radius ”);
scanf(“%d ”, &radius );
printf(“Circumference is %d”, CIRCUMFERENCE(radius));
}

5. # include<stdio.h>
# define MUL_TABLE(num) for(n=1;n<=10;n++) \
printf("\n%d*%d=%d",num,n,num*n)
main()
{
int number; int n;
printf("enter number");
scanf("%d",&number);
MUL_TABLE(number);
}

6. # include<stdio.h>
o m
# define SUM(n) ( (n * (n+1)) / 2 )
. c
main()
{
o t
int number;
printf("enter number");
s p
scanf("%d", &number);
printf("\n sum of n numbers %d", sum(number)); }
og
b l
Check Your Progress 2

6 .
1. # include <math.h>

0 0
2. #include "madan.h"
i a
3. 7
o n
p
4. # include<stdio.h> o
main()
i l
# define isupper(c) (c>=65 && c<=90)

u n
{

s char c;
printf("Enter character:");
scanf("%c",&c);
if(isupper(c))
printf("\nUpper case");
else
printf("\nNo it is not an upper case character");
}

11.11 FURTHER READINGS


1. The C programming language, Brian W. Kernighan & Dennis Ritchie, Pearson
Education, 2002.
2. Programming with ANSI and Turbo C, Ashok N. Kamthane, Pearson Education,
2002.
3. Computer Programming in C, Raja Raman. V, PHI, 2002.

57
Structures, Pointers
and File Handling
UNIT 12 FILES

Structure

12.0 Introduction
12.1 Objectives
12.2 File Handling in C Using File Pointers
12.2.1 Open a file using the function fopen( )
12.2.2 Close a file using the function fclose( )
12.3 Input and Output using file pointers
12.3.1 Character Input and Output in Files
12.3.2 String Input / Output Functions
12.3.3 Formatted Input / Output Functions
12.3.4 Block Input / Output Functions
12.4 Sequential Vs Random Access Files
12.5 Positioning the File Pointer
12.6
12.7
The Unbufferred I/O - The UNIX like File Routines
Summary
o m
12.8 Solutions / Answers
. c
12.9 Further Readings

o t
12.0 INTRODUCTION
s p
o g
b l
The examples we have seen so far in the previous units deal with standard input and
output. When data is stored using variables, the data is lost when the program exits

6 .
unless something is done to save it. This unit discusses methods of working with files,
and a data structure to store data. C views file simply as a sequential stream of bytes.

0 0
Each file ends either with an end-of-file marker or at a specified byte number recorded

i a
in a system maintained, administrative data structure. C supports two types of files
called binary files and text files.

o n
The difference between these two files is in terms of storage. In text files, everything

p o is stored in terms of text i.e. even if we store an integer 54; it will be stored as a 3-byte

i l string - “54\0”. In a text file certain character translations may occur. For example a
newline(\n) character may be converted to a carriage return, linefeed pair. This is what

u n Turbo C does. Therefore, there may not be one to one relationship between the
characters that are read or written and those in the external device. A binary file

s contains data that was written in the same format used to store internally in main
memory.

For example, the integer value 1245 will be stored in 2 bytes depending on the
machine while it will require 5 bytes in a text file. The fact that a numeric value is in a
standard length makes binary files easier to handle. No special string to numeric
conversions is necessary.

The disk I/O in C is accomplished through the use of library functions. The ANSI
standard, which is followed by TURBO C, defines one complete set of I/O functions.
But since originally C was written for the UNIX operating system, UNIX standard
defines a second system of routines that handles I/O operations. The first method,
defined by both standards, is called a buffered file system. The second is the
unbuffered file system.

In this unit, we will first discuss buffered file functions and then the unbuffered file
functions in the following sections.
58
Files
12.1 OBJECTIVES

After going through this unit you will be able to:

• define the concept of file pointer and file storage in C;


• create text and binary files in C;
• read and write from text and binary files;
• deal with large set of Data such as File of Records; and
• perform operations on files such as count number of words in a file, search a word
in a file, compare two files etc.

12.2 FILE HANDLING IN C USING FILE POINTERS

As already mentioned in the above section, a sequential stream of bytes ending with
an end-of-file marker is what is called a file. When the file is opened the stream is
associated with the file. By default, three files and their streams are automatically
opened when program execution begins - the standard input, standard output, and
o m
the standard error. Streams provide communication channels between files and
programs.
.c
o t
For example, the standard input stream enables a program to read data from the
keyboard, and the standard output stream enables to write data on the screen.
s p
Opening a file returns a pointer to a FILE structure (defined in <stdio.h>) that

o g
contains information, such as size, current file pointer position, type of file etc., to
l
perform operations on the file. This structure also contains an integer called a file
b
6 .
descriptor which is an index into the table maintained by the operating system namely,
the open file table. Each element of this table contains a block called file control block

0
(FCB) used by the operating system to administer a particular file.

0
i a
The standard input, standard output and the standard error are manipulated using file
pointers stdin, stdout and stderr. The set of functions which we are now going to
n
discuss come under the category of buffered file system. This file system is referred to
o
p
writing automatically. o
as buffered because, the routines maintain all the disk buffers required for reading /

i l
To access any file, we need to declare a pointer to FILE structure and then associate it

u n
with the particular file. This pointer is referred as a file pointer and it is declared as
follows:
sFILE *fp;

12.2.1 Open A File Using The Function fopen()

Once a file pointer variables has been declared, the next step is to open a file. The
fopen() function opens a stream for use and links a file with that stream. This function
returns a file pointer, described in the previous section. The syntax is as follows:

FILE *fopen(char *filename,*mode);

where mode is a string, containing the desired open status. The filename must be a
string of characters that provide a valid file name for the operating system and may
include a path specification. The legal mode strings are shown below in the table 12.1:

59
Structures, Pointers
and File Handling
Table 12.1: Legal values to the fopen( ) mode parameter

MODE MEANING

“r” / “rt” opens a text file for read only access


“w” / “wt” creates a text file for write only access
“a” / “at” text file for appending to a file
“r+t” open a text file for read and write access
“w+t” creates a text file for read and write access,
“a+t” opens or creates a text file and read access
“rb” opens a binary file for read only access
“wb” create a binary file for write only access
“ab” binary file for appending to a file
“r+b” opens a binary or read and write access
“w+b” creates a binary or read and write access,
“a+b” open or binary file and read access
o m
. c
The following code fragment explains how to open a file for reading.

o t
Code Fragment 1
s p
#include <stdio.h>
o g
main ()
b l
{
6 .
FILE *fp;
0 0
{
i a
if ((fp=fopen(“file1.dat”, “r”))==NULL)

o n printf(“FILE DOES NOT EXIST\n”);


exit(0);

o }

lp }

ni The value returned by the fopen( ) function is a file pointer. If any error occurs while

su
opening the file, the value of this pointer is NULL, a constant declared in <stdio.h>.
Always check for this possibility as shown in the above example.

12.2.2 Close A File Using The Function Fclose( )


When the processing of the file is finished, the file should be closed using the fclose()
function, whose syntax is:

int fclose(FILE *fptr);

This function flushes any unwritten data for stream, discards any unread buffered
input, frees any automatically allocated buffer, and then closes the stream. The return
value is 0 if the file is closed successfully or a constant EOF, an end-of file marker, if
an error occurred. This constant is also defined in <stdio.h>. If the function fclose() is
not called explicitly, the operating system normally will close the file when the
program execution terminates.

The following code fragment explains how to close a file.


60
Files
Code Fragment 2

# include <stdio.h>
main ( )
{
FILE *fp;
if ((fp=fopen(“file1.dat”, “r”))==NULL)
{
printf(“FILE DOES NOT EXIST\n”);
exit(0);
}
……………..
……………..
……………..
…………….
/* close the file */
fclose(fp);
}

o m
Once the file is closed, it cannot be used further. If required it can be opened in same
.c
or another mode.

o t
Check Your Progress 1

s p
1. How does fopen( ) function links a file to a stream?
o g
…………………………………………………………………………………………
b l
…………………………………………………………………………………………
6 .
……………………………………………………………………………………..….
0 0
2.
a
Differentiate between text files and binary files.
i
n
…………………………………………………………………………………………
o
o
…………………………………………………………………………………………

p
l
………………………………………………………………………………………..

i
3.

u n What is EOF and what is its value?

s…………………………………………………………………………………………
…………………………………………………………………………………………
……………………………………………………………………………………..….

12.3 INPUT AND OUTPUT USING FILE POINTERS

After opening the file, the next thing needed is the way to read or write the file. There
are several functions and macros defined in <stdio.h> header file for reading and
writing the file. These functions can be categorized according to the form and type of
data read or written on to a file. These functions are classified as:

• Character input/output functions


• String input/output functions
• Formatted input/output functions
• Block input/output functions.
61
Structures, Pointers
and File Handling 12.3.1 Character Input and Output in Files
ANSI C provides a set of functions for reading and writing character by character or
one byte at a time. These functions are defined in the standard library. They are listed
and described below:

• getc()
• putc()

getc( ) is used to read a character from a file and putc( ) is used to write a character to
a file. Their syntax is as follows:

int putc(int ch, FILE *stream);


int getc(FILE *stream);

The file pointer indicates the file to read from or write to. The character ch is formally
called an integer in putc( ) function but only the low order byte is used. On success
putc( ) returns a character(in integer form) written or EOF on failure. Similarly getc( )
returns an integer but only the low order byte is used. It returns EOF when end-of-file
o m
is reached. getc( ) and putc( ) are defined in <stdio.h> as macros not functions.

. c
fgetc() and fputc()
o t
characters from / to a file. These are:
s p
Apart from the above two macros, C also defines equivalent functions to read / write

o g
int fgetc(FILE *stream);
int fputc(int c, FILE *stream);
b l
6 .
To check the end of file, C includes the function feof( ) whose prototype is:

0
int feof(FILE *fp); 0
i a
o n
It returns 1 if end of file has been reached or 0 if not. The following code fragment
explains the use of these functions.

o
lp Example 12.1

ni Write a program to copy one file to another.

su /*Program to copy one file to another */

#include <stdio.h>
main( )
{
FILE *fp1;
FILE *fp2;
int ch;
if((fp1=fopen(“f1.dat”,”r”)) == NULL)

{
printf(“Error opening input file\n”);
exit(0);
}
if((fp2=fopen(“f2.dat”,”w”)) == NULL)
{
printf(“Error opening output file\n”);
exit(0);
62
} Files

while (!feof(fp1))
{
ch=getc(fp1);
putc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
}

OUTPUT

If the file ”f1.dat” is not present, then the output would be:
Error opening input file
If the disk is full, then the output would be:
Error opening output file

If there is no error, then “f2.dat” would contain whatever is present in “f1.dat” after
the execution of the program, if “f2.dat” was not empty earlier, then its contents
o m
would be overwritten.
.c
12.3.2 String Input/Output Functions
o t
s p
o
input function, instead C provides some string input/output functions with the help ofg
If we want to read a whole line in the file then each time we will need to call character

standard library and are discussed below:


b l
which we can read/write a set of characters at one time. These are defined in the

• 6 .

fgets( )
fputs( )
0 0
i a
These functions are used to read and write strings. Their syntax is:

o n
p o
int fputs(char *str, FILE *stream);
char *fgets(char *str, int num, FILE *stream);

i l
The integer parameter in fgets( ) is used to indicate that at most num-1 characters are

u n
to be read, terminating at end-of-file or end-of-line. The end-of-line character will be
placed in the string str before the string terminator, if it is read. If end-of-file is
sencountered as the first character, EOF is returned, otherwise str is returned. The
fputs( ) function returns a non-negative number or EOF if unsuccessful.

Example 12.2
Write a program read a file and count the number of lines in the file, assuming that a
line can contain at most 80 characters.

/*Program to read a file and count the number of lines in the file */
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
FILE *fp;
int cnt=0;
char str[80];
63
Structures, Pointers
and File Handling
/* open a file in read mode */

if ((fp=fopen("lines.dat","r"))== NULL)
{ printf("File does not exist\n");
exit(0);
}
/* read the file till end of file is encountered */
while(!(feof(fp)))
{ fgets(str,80,fp); /*reads at most 80 characters in str */
cnt++; /* increment the counter after reading a line */
}
}/* print the number of lines */
printf(“The number of lines in the file is :%d\n”,cnt);
fclose(fp);
}

OUTPUT

Let us assume that the contents of the file “lines.dat” are as follows:
o m
. c
This is C programming.
I love C programming.
o t
s p
To be a good programmer one should have a good logic. This is a must.
C is a procedural programming language.
o g
After the execution the output would be:
b l
6 .
The number of lines in the file is: 4

0 0
a
12.3.3 Formatted Input/Output Functions
i
o n
If the file contains data in the form of digits, real numbers, characters and strings, then
character input/output functions are not enough as the values would be read in the

p o form of characters. Also if we want to write data in some specific format to a file, then
it is not possible with the above described functions. Hence C provides a set of

i l formatted input/output functions. These are defined in standard library and are

u n discussed below:

s fscanf() and fprintf()

These functions are used for formatted input and output. These are identical to scanf()
and printf() except that the first argument is a file pointer that specifies the file to be
read or written, the second argument is the format string. The syntax for these
functions is:
int fscanf(FILE *fp, char *format,. . .);
int fprintf(FILE *fp, char *format,. . .);

Both these functions return an integer indicating the number of bytes actually read or
written.

Example 12.3

Write a program to read formatted data (account number, name and balance) from a
file and print the information of clients with zero balance, in formatted manner on the
screen.
64
Files
/* Program to read formatted data from a file */

#include<stdio.h>
main()
{
int account;
char name[30];
double bal;
FILE *fp;

if((fp=fopen("bank.dat","r"))== NULL)
printf("FILE not present \n");
else
do{
fscanf(fp,"%d%s%lf",&account,name,&bal);
if(!feof(fp))
{
if(bal==0)
printf("%d %s %lf\n",account,name,bal);
o m
}
.c
}
}while(!feof(fp));

o t
OUTPUT
s p
o g
This program opens a file “bank.dat” in the read mode if it exists, reads the records
l
and prints the information (account number, name and balance) of the zero balance
b
records.

6 .
Let the file be as follows:

0 0
101
102
nuj
Raman
1200
1500
i a
103 Swathi 0
o n
104
105
Ajay
Udit
p
1600
0 o
i l
The output would be as follows:

u n
s103
105
Swathi 0
Udit 0

12.3.4 Block Input/Output Functions

Block Input / Output functions read/write a block (specific number of bytes from/to a
file. A block can be a record, a set of records or an array. These functions are also
defined in standard library and are described below.

• fread( )
• fwrite( )

These two functions allow reading and writing of blocks of data. Their syntax is:
int fread(void *buf, int num_bytes, int count, FILE *fp);
int fwrite(void *buf, int num_bytes, int count, FILE *fp);
65
Structures, Pointers In case of fread(), buf is the pointer to a memory area that receives the data from the
and File Handling
file and in fwrite(), it is the pointer to the information to be written to the file.
num_bytes specifies the number of bytes to be read or written. These functions are
quite helpful in case of binary files. Generally these functions are used to read or write
array of records from or to a file. The use of the above functions is shown in the
following program.

Example 12.4

Write a program using fread( ) and fwrite() to create a file of records and then read
and print the same file.

/* Program to illustrate the fread() and fwrite() functions*/


#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>

void main()
{
o m
struct stud
. c
{
char name[30];
o t
int age;
int roll_no;
s p
int i;
}s[30],st;
o g
FILE *fp;
b l
6 .
/*opening the file in write mode*/

0 0
if((fp=fopen("sud.dat","w"))== NULL)
{ printf("Error while creating a file\n");

i a exit(0); }

o n
/* reading an array of students */

p o for(i=0;i<30;i++)
scanf("%s %d %d",s[i].name,s[i].age,s[i].roll_no);

i l /* writing to a file*/

u n fwrite(s,sizeof(struct stud),30,fp);

s fclose(fp);

/* opening a file in read mode */


fp=fopen("stud.dat","r");

/* reading from a file and writing on the screen */


while(!feof(fp))
{
fread(&st,sizeof(struct stud),1,fp);
fprintf("%s %d %d",st.name,st.age,st.roll_no);
}
fclose(fp); }

OUTPUT

This program reads 30 records (name, age and roll_number) from the user, writes one
record at a time to a file. The file is closed and then reopened in read mode; the
records are again read from the file and written on to the screen.
66
Files
Check Your Progress 2
1. Give the output of the following code fragment:

#include<stdio.h>
#include<process.h>
#include<conio.h>
main()
{
FILE * fp1, * fp2;
double a,b,c;

fp1=fopen(“file1”, “w”);
fp2=fopen(“file2”, “w”);

fprintf(fp1,”1 5.34 –4E02”);


fprintf(fp2,”-2\n1.245\n3.234e02\n”);
fclose(fp1);
fclose(fp2);

o m
fp1=fopen(“file1”, “r”);
fp2=fopen(“file2”,“r”);
.c
fscanf(fp1,“%lf %lf %lf”,&a,&b,&c);
o t
printf(“%10lf %10lf %10lf”,a,b,c);
s p
fscanf(fp2,”%lf %lf %lf”,&a,&b,&c);
printf(“%10.1e %10lf %10lf”,a,b,c);
o g
fclose(fp1);
b l
fclose(fp2);
6 .
}
…………………………………………………………………………………………
0 0
…………………………………………………………………………………………
i a
n
…………………………………………………………………………………………
o
p o
2. What is the advantage of using fread/fwrite functions?
l
…………………………………………………………………………………………
i
n
…………………………………………………………………………………………

u
s…………………………………………………………………………………………
3. ________ and _______ functions are used for formatted input and output
from a file.

…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………

12.4 SEQUENTIAL Vs RANDOM ACCESS FILES

We have seen in section 12.0 that C supports two type of files – text and binary files,
also two types of file systems – buffered and unbuffered file system. We can also
differentiate in terms of the type of file access as Sequential access files and random
access files. Sequential access files allow reading the data from the file in sequential
manner which means that data can only be read in sequence. All the above examples 67
Structures, Pointers that we have considered till now in this unit are performing sequential access.
and File Handling
Random access files allow reading data from any location in the file. To achieve this
purpose, C defines a set of functions to manipulate the position of the file pointer. We
will discuss these functions in the following sections.

12.5 POSITIONING THE FILE POINTER

To support random access files, C requires a function with the help of which the file
pointer can be positioned at any random location in the file. Such a function defined in
the standard library is discussed below:

The function fseek( ) is used to set the file position. Its prototype is:

int fseek(FILE *fp, long offset, int pos);

The first argument is the pointer to a file. The second argument is the number of bytes
to move the file pointer, counting from zero. This argument can be positive, negative
or zero depending on the desired movement. The third parameter is a flag indicating
o m
from where in the file to compute the offset. It can have three values:

. c
SEEK_SET(or value 0)
SEEK_CUR(or value 1)
the beginning of the file,
the current position and
o t
SEEK_END(or value 2) the end of the file
s p
o g
These three constants are defined in <stdio.h>. If successful fseek( ) returns zero.

Its prototype is:


b l
Another function rewind() is used to reset the file position to the beginning of the file.

void rewind(FILE *fp);


6 .
0 0
a
A call to rewind is equivalent to the call

i
n
fseek(fp,0,SEEK_SET);
o
p o Another function ftell() is used to tell the position of the file pointer. Its prototype is:

i l long ftell(FILE *fp);

u n It returns –1 on error and the position of the file pointer if successful.


s Example 12.5

Write a program to search a record in an already created file and update it. Use the
same file as created in the previous example.

/*Program to search a record in an already created file*/

#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
void main()
{
int r,found;
struct stud
{
68
char name[30]; Files
int age;
int roll_no;
}st;
FILE *fp;
/* open the file in read/write mode */

if((fp=fopen("f1.dat","r+b"))==NULL)
{ printf("Error while opening the file \n");
exit(0);
}

/* Get the roll_no of the student */


printf("Enter the roll_no of the record to be updated\n");
found=0;
scanf("%d",&r);

/* check in the file for the existence of the roll_no */


while((!feof(fp)) && !(found))
{ fread(&st,sizeof(stud),1,fp);
o m
if(st.roll_no == r)
.c
/* if roll_no is found then move one record backward to update it */
o t
{ fseek(fp,- sizeof(stud),SEEK_CUR);
printf("Enter the new name\n");
s p
scanf("%s",st.name);
fwrite(fp,sizeof(stud),1,fp);
o g
found=1;
b l
}
}

6 .
if (!found)
printf("Record not present\n");
0 0
}
fclose(fp);
i a
o n
OUTPUT

p o
Geeta 18
i l
Let the input file be as follows:
101

u n
Leena 17 102

sMahesh 23
Lokesh 21
Amit 19
103
104
105

Let the roll_no of the record to be updated be 106. Now since this roll_no is not
present the output would be:

Record not present

If the roll_no to be searched is 103, then if the new name is Sham, the output would
be the file with the contents:

Geeta 18 101
Leena 17 102
Sham 23 103
Lokesh 21 104
Amit 19 105

69
Structures, Pointers
and File Handling 12.6 THE UNBUFFERED I/O – THE UNIX LIKE FILE
ROUTINES

The buffered I/O system uses buffered input and output, that is, the operating system
handles the details of data retrieval and storage, the system stores data temporarily
(buffers it) in order to optimize file system access. The buffered I/O functions are
handled directly as system calls without buffering by the operating system. That is
why they are also known as low level functions. This is referred to as unbuffered I/O
system because the programmer must provide and maintain all disk buffers, the
routines do not do it automatically.

The low level functions are defined in the header file <io.h>.

These functions do not use file pointer of type FILE to access a particular file, but
they use directly the file descriptors, as explained earlier, of type integer. They are
also called handles.

Opening and closing of files

o m
The function used to open a file is open( ). Its prototype is:
. c
int open(char *filename, int mode, int access);
o t
s p
Here mode indicates one of the following macros defined in <fcntl.h>.

Mode:
o g
b l
O_RDONLY
O_WRONLY
6 .
Read only
Write only
O_RDWR

0 0 Read / Write

i a
The access parameter is used in UNIX environment for providing the access to
particular users and is just included here for compatibility and can be set to zero.
n
open() function returns –1 on failure. It is used as:
o
o
lp
Code fragment 2

ni
int fd;

su if ((fd=open(filename,mode,0)) == -1)
{ printf(“cannot open file\n”);
exit(1); }

If the file does not exist, open() the function will not create it. For this, the function
creat() is used which will create new files and re-write old ones. The prototype is:

int creat(char *filename, int access);

It returns a file descriptor; if successful else it returns –1. It is not an error to create an
already existing file, the function will just truncate its length to zero. The access
parameter is used to provide permissions to the users in the UNIX environment.
The function close() is used to close a file. The prototype is:

int close(int fd);

It returns zero if successful and –1 if not.

70
Files
Reading, Writing and Positioning in File

The functions read() and write() are used to read from and write to a file. Their
prototypes are:

int read(int fd, void *buf, int size);


int write(int fd, void *buf, int size);

The first parameter is the file descriptor returned by open(), the second parameter
holds the data which must be typecast to the format needed by the program, the third
parameter indicates the number of bytes to transferred. The return value tells how
many bytes are actually transferred. If this value is –1, then an error must have
occurred.

Example 12.6

Write a program to copy one file to another to illustrate the use of the above functions.
The program should expect two command line arguments indicating the name of the
file to be copied and the name of the file to be created.
o m
/* Program to copy one file to another file to illustrate the functions*/
.c
# include<stdio.h>
# include<io.h>
o t
#include<process.h>

s p
typedef char arr[80];
typedef char name[30];
o g
b l
main()
{
6 .
arr buf;
name fname, sname;
0 0
int fd1,fd2,size;
i a
o n
/* check for the command line arguments */
if (argc!=3)

p o
{ printf("Invalid number of arguments\n");

}
i l
exit(0);

u n
if ((fd1=open(argv[1],O_RDONLY))<0)

s { printf("Error in opening file %s \n",argv[1]);

}
exit(0);

if ((fd2=creat(argv[2],0))<0)
{ printf("Error in creating file %s \n",argv[2]);
exit(0);}

open(argv[2],O_WRONLY);
size=read(fd1,buf,80); /* read till end of file */

while (size>0)
{ write(fd2,buf,80);
size=read(fd1,buf,80);
}
close(fd1);
close(fd2);
}

71
Structures, Pointers OUTPUT
and File Handling

If the number of arguments given on the command line is not correct then output
would be:

Invalid number of arguments

One file is opened in the read mode, and another file is opened in the write mode. The
output would be as follows is the file to be read is not present (let the file be f1.dat):

Error in opening file f1.dat

The output would be as follows if the disk is full and the file cannot be created (let the
output file be f2.dat):

Error in creating file f2.dat

If there is no error contents of f1.dat will be copied to f2.dat.

lseek()
o m
. c
is:
o t
The function lseek() is provided to move to the specific position in a file. Its prototype

long lseek(int fd, long offset, int pos);


s p
o g
This function is exactly the same as fseek() except that the file descriptor is used
instead of the file pointer.
b l
6 .
Using the above defined functions, it is possible to write any kind of program dealing
with files.

0 0
i a
Check Your Progress 3

o n
p o 1. Random access is possible in C files using function ____________.

i l 2. Write a proper C statement with proper arguments that would be called to move the
file pointer back by 2 bytes.

u n …………………………………………………………………………………………

s …………………………………………………………………………………………

3. Indicate the header files needed to use unbuffered I/O.


…………………………………………………………………………………………
…………………………………………………………………………………………
……………………………………………………………………………………..….

12.7 SUMMARY

In this unit, we have learnt about files and how C handles them. We have discussed
the buffered as well as unbuffered file systems. The available functions in the standard
library have been discussed. This unit provided you an ample set of programs to start
with. We have also tried to differentiate between sequential access as well as random
access file. The file pointers assigned to standard input, standard output and standard
72 error are stdin, stdout, and stderr respectively. The unit clearly explains the different
type of modes oof opening the file. As seen there are several functions available to Files
read/write from the file. The usage of a particular function depends on the application.
After reading this unit one must be able to handle large data bases in the form of files.

12.8 SOLUTIONS / ANSWERS

Check Your Progress 1

1. fopen() function links a file to a stream by returning a pointer to a FILE structure


defined in <stdio.h>. This structure contains an index called file descriptor to a
File Control Block, which is maintained by the operating system for
administrative purposes.

2. Text files and binary files differ in terms of storage. In text files everything is
stored in terms of text while binary files stores exact memory image of the data i.e.
in text files 154 would take 3 bytes of storage while in binary files it will take 2
bytes as required by an integer.

o m
3. EOF is an end-of-file marker. It is a macro defined in <stdio.h>. Its value is –1.

.c
Check Your progress 2
o t
1. The output would be:
s p
1.000000 5.340000 –400.000000 -2.0e+00 1.245000 323.400000
o g
b l
2. The advantage of using these functions is that they are used for block read/write,

6 .
which means we can read or write a large set of data at one time thus increasing
the speed.

0 0
3.
a
fscanf() and fprintf() functions are used for formatted input and output from a file.

i
Check Your progress 3
o n
p o
1. Random access is possible in C files using function fseek().

2.
i l
fseek(fp, -2L, SEEK_END);

u n
3. <io.h> and <fcntl.h>
s12.9 FURTHER READINGS

1. The C Programming Language, Kernighan & Richie, PHI Publication, 2002.


2. C How to Program, Deitel & Deitel, Pearson Education, 2002.
3. Practical C Programming, Steve Oualline, Oreilly Publication, 2003.

73
Structures, Pointers APPENDIX-A
and File Handling
THE ASCII SET

The ASCII (American Standard Code for Information Interchange) character set
defines 128 characters (0 to 127 decimal, 0 to FF hexadecimal, and 0 to 177 octal).
This character set is a subset of many other character sets with 256 characters,
including the ANSI character set of MS Windows, the Roman-8 character set of HP
systems, and the IBM PC Extended Character Set of DOS, and the ISO Latin-1
character set used by Web browsers. They are not the same as the EBCDIC character
set used on IBM mainframes. The first 32 values are non-printing control characters,
such as Return and Line feed. You generate these characters on the keyboard by
holding down the Control key while you strike another key. For example, Bell is value
7, Control plus G, often shown in documents as ^G. Notice that 7 is 64 less than the
value of G (71); the Control key subtracts 64 from the value of the keys that it
modifies. The table shown below gives the list of the control and printing characters.

The Control Characters

o m
Char Oct Dec Hex Control-Key Control Action

. c
NUL 0
SOH 1
0
1
0
1
^@
^A
Null character
Start of heading, = console interrupt
o t
STX 2 2 2 ^B
p
Start of text, maintenance mode on HP console

s
ETX 3
EOT 4
3
4
3
4
^C
^D
End of text

o g
End of transmission, not the same as ETB
ENQ 5 5 5 ^E
l
Enquiry, goes with ACK; old HP flow control

b
ACK 6
BEL 7
6
7
6
7
^F
^G
6 . Acknowledge, clears ENQ logon hand
Bell, rings the bell...
BS 10 8

0 8 ^H
0 Backspace, works on HP terminals/computers
HT
LF
11
12
i a
9
10
9
a
^I
^J
Horizontal tab, move to next tab stop
Line Feed

o on
VT
FF
13
14
11
12
b
c
^K
^L
Vertical tab
Form Feed, page eject

l p CR 15 13 d ^M Carriage Return

n i SO
SI
16
17
14
15
e
f
^N
^O
Shift Out, alternate character set
Shift In, resume defaultn character set

su DLE 20
DC1 21
16
17
10
11
^P
^Q
Data link escape
XON, with XOFF to pause listings; ":okay to send".
DC2 22 18 12 ^R Device control 2, block-mode flow control
DC3 23 19 13 ^S XOFF, with XON is TERM=18 flow control
DC4 24 20 14 ^T Device control 4
NAK 25 21 15 ^U Negative acknowledge
SYN 26 22 16 ^V Synchronous idle
ETB 27 23 17 ^W End transmission block, not the same as EOT
CAN 30 24 17 ^X Cancel line, MPE echoes !!!
EM 31 25 19 ^Y End of medium, Control-Y interrupt
SUB 32 26 1a ^Z Substitute
ESC 33 27 1b ^[ Escape, next character is not echoed
FS 34 28 1c ^\ File separator
GS 35 29 1d ^] Group separator
RS 36 30 1e ^^ Record separator, block-mode terminator
US 37 31 1f ^_ Unit separator
74
Printing Characters Files

Char Octal Dec Hex Description


SP 40 32 20 Space
! 41 33 21 Exclamation mark
" 42 34 22 Quotation mark (&quot; in HTML)
# 43 35 23 Cross hatch (number sign)
$ 44 36 24 Dollar sign
% 45 37 25 Percent sign
& 46 38 26 Ampersand
` 47 39 27 Closing single quote (apostrophe)
( 50 40 28 Opening parentheses
) 51 41 29 Closing parentheses
* 52 42 2a Asterisk (star, multiply)
+ 53 43 2b Plus
, 54 44 2c Comma
-
.
55
56
45
46
2d
2e
Hyphen, dash, minus
Period
o m
/ 57 47 2f Slant (forward slash, divide)
.c
0
1
60
61
48
49
30
31
Zero
One
o t
2 62 50 32 Two
s p
3
4
63
64
51
52
33
34
Three
Four
og
5 65 53 35 Five
b l
6 66 54 36 Six

6 .
7
8
67
70
55
56
37
38
Seven
Eight
0 0
9 71 57 39 Nine

i a
:
;
72
73
58
59
3a
3b
Colon
Semicolon
o n
< 74 60 3c
o
Less than sign (&lt; in HTML)
= 75 61
lp
3d Equals sign

ni
> 76 62 3e Greater than sign (&gt; in HTML)
? 77 63 3f Question mark

su
@
A
B
100
101
102
64
65
66
40
41
42
At-sign
Uppercase A
Uppercase B
C 103 67 43 Uppercase C
D 104 68 44 Uppercase D
E 105 69 45 Uppercase E
F 106 70 46 Uppercase F
G 107 71 47 Uppercase G
H 110 72 48 Uppercase H
I 111 73 49 Uppercase I
J 112 74 4a Uppercase J
K 113 75 4b Uppercase K
L 114 76 4c Uppercase L
M 115 77 4d Uppercase M
N 116 78 4e Uppercase N
75
Structures, Pointers O 117 79 4f Uppercase O
and File Handling
P 120 80 50 Uppercase P
Q 121 81 51 Uppercase Q
R 122 82 52 Uppercase R
S 123 83 53 Uppercase S
T 124 84 54 Uppercase T
U 125 85 55 Uppercase U
V 126 86 56 Uppercase V
W 127 87 57 Uppercase W
X 130 88 58 Uppercase X
Y 131 89 59 Uppercase Y
Z 132 90 5a Uppercase Z
[ 133 91 5b Opening square bracket
\ 134 92 5c Reverse slant (Backslash)
] 135 93 5d Closing square bracket
^
_
136
137
94
95
5e
5f
Caret (Circumflex)
Underscore
o m
` 140 96 60 Opening single quote

.c
a
b
141
142
97
98
61
62
Lowercase a
Lowercase b
o t
c 143 99 63 Lowercase c
s p
d
e
144
145
100 64
101 65
Lowercase d
Lowercase e
o g
f 146 102 66 Lowercase f
b l
g
h
147
150
103 67
104 68
6 .
Lowercase g
Lowercase h
i 151
0105 69 0
Lowercase i
j 152

i a 106 6a Lowercase j
k

o
l n 153
154
107 6b
108 6c
Lowercase k
Lowercase l

o m 155 109 6d Lowercase m

i lp n
o
156
157
110 6e
111 6f
Lowercase n
Lowercase o

u n p 160 112 70 Lowercase p

s q
r
s
161
162
163
113 71
114 72
115 73
Lowercase q
Lowercase r
Lowercase s
t 164 116 74 Lowercase t
u 165 117 75 Lowercase u
v 166 118 76 Lowercase v
w 167 119 77 Lowercase w
x 170 120 78 Lowercase x
y 171 121 79 Lowercase y
z 172 122 7a Lowercase z
{ 173 123 7b Opening curly brace
| 174 124 7c Vertical line
} 175 125 7d Cloing curly brace
~ 176 126 7e Tilde (approximate)
DEL 177 127 7f Delete (rubout), cross-hatch box

76

You might also like