Block 1 PDF
Block 1 PDF
PROBLEM SOLVING
Problem Solving
Structure
1.0
1.1
1.2
Introduction
Objectives
Problem - Solving Techniques
1.2.1
1.2.2
1.3
Design of Algorithms
1.3.1
1.3.2
1.3.3
1.3.4
1.4
Definition
Features of Algorithm
Criteria to be followed by an Algorithm
Top Down Design
1.5
Redundant Computations
Referencing Array Elements
Inefficiency Due to Late Termination
Early Detection of Desired Output Condition
Trading Storage for Efficient Gains
Computational Complexity
The Order of Notation
Rules for using the Big - O Notation
Worst and Average Case Behavior
1.6
Flowcharts
1.7
1.8
1.8
Summary
Solutions / Answers
Further Readings
1.6.1
1.0
INTRODUCTION
In our daily life, we routinely encounter and solve problems. We pose problems that
we need or want to solve. For this, we make use of available resources, and solve
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
quite simple. But some others may be very complex.
In this unit we introduce you to the concepts of problem-solving, especially as they
pertain to computer programming.
The problem-solving is a skill and there are no universal approaches one can take to
solving 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 ones 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
languages are also part of it. Physical computer equipment such as electronic circuitry,
input/output devices, storage media etc. comes under hardware. Software governs the
functioning of hardware. Operations performed by software may be built into the
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
software is made by the manufacturer and designer of the software and hardware.
Normal considerations for this are: cost, speed, memory required, adaptability and
reliability of the system. Set of instructions of the high level language used to code a
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
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
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
executed to perform calculations. This stage is the execution phase. Data, if required
by the program, are supplied now and the results are obtained on the output device.
Source
Program
Computer
System
Object
Program
Results
1.1
OBJECTIVES
Data,
if
required
1.2
Problem Solving
1.2.1
An Introduction to C
1.2.2
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
using good structured development techniques, your programs will be efficient, errorfree, and easy to maintain. The following are the steps in detail:
1. Develop an Algorithm and a Flowchart.
2. Write the program in a computer language (for example say C programming
language).
3. Enter the program using some editor.
4. Test and debug the program.
5. Run the program, input data, and get the results.
1.3
DESIGN OF ALGORITHMS
The first step in the program development is to devise and describe a precise plan of
what you want the computer to do. This plan, expressed as a sequence of operations,
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
within the programming language. It is expected that one could translate each pseudocode statement to a small number of lines of actual code, easily and mechanically.
1.3.1
Definition
Problem Solving
Example 1.1
Let us try to develop an algorithm to compute and display the sum of two numbers
1.
2.
3.
4.
5.
Start
Read two numbers a and b
Calculate the sum of a and b and store it in sum
Display the value of sum
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
Example 1.4
Write an algorithm to check that whether the given number is prime or not.
1. Start
2. Read the number num
3. [Initialize]
i 2 , flag 1
4. Repeat steps 4 through 6 until i < num or flag = 0
5. rem num mod i
6. if rem = 0 then
flag 0
else
ii+1
7. if flag = 0 then
Print Number is not prime
Else
Print Number is prime
8. Stop
1.3.4
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
itself provide the necessary starting point for the top-down design.
Problem Solving
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.
Figure 1.1:
1.4
Every algorithm uses some of the computers 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:
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
1.4.2
For using the array element, we require two memory references and an additional
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
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
14
Problem Solving
1.4.3
1.4.4
Sometimes the loops can be terminated early, if the desired output conditions are met.
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
list can be assumed to be sorted and the search can be terminated before running the
outer loop for n times.
1.4.5
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
do a lot of unnecessary testing and computation later on.
One strategy for speeding up the execution of an algorithm is to implement it using
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
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
is to be done while determining the approach for the same.
1.5
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
n
2
10
102
103
104
n log2 n
2
33.22
664.4
9966.0
132877
n2
4
102
104
106
108
n3
8
103
106
109
1012
2n
4
>103
>>1025
>>10250
>>102500
The above table shows that only very small problems can be solved with an algorithm
that exhibit exponential behaviour. An exponential problem with n=100 would take
immeasurably longer time. At the other extreme, for an algorithm with logarithmic
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
behave as a function of the problem size. Analysis of an algorithm also provides the
theoretical model of the inherent computational complexity of a particular problem.
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
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
made as n grows. For example, comparisons, exchanges, and moves count most in
sorting algorithm. The number of comparisons usually dominates so we use
comparisons in computational model for sorting algorithms.
1.5.2
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.
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 algorithms 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
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 doesnt have to take N2 steps (or a
constant multiple of N2). But it cant 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).
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).
= 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
Input/Output
Process, Instruction
Comments, Explanations,
Definitions.
Additional Symbols Related to more advanced programming
18
Problem Solving
Example 1.5
The flowchart for the Example 1.1 is shown below:
Start
Read a b
Sum = a + b
Print sum
Stop
Example 1.6
The flowchart for the Example 1.3 (to find factorial of a given number) is shown
below:
Start
Read n
i=1
fact = 1
No
Is i<= n ?
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
no
is
i<num?
yes
no
is
flag = 0?
yes
rem =
num mod i
no
is rem
!= 0?
yes
i=i+1
flag = 0
no
Print
number is
not prime
is flag
= 1?
yes
stop
20
Print
Number
is prime
Problem Solving
3. Write the following steps are suggested to facilitate the problem solving process
using computer.
1.7
SUMMARY
To solve a problem different problem - solving tools are available that help in finding
the solution to problem in an efficient and systematic way. Steps should be followed
to 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
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)
b)
c)
d)
e)
f)
22
UNIT 2
BASICS OF C
Basics of C
Structure
2.0
2.1
2.2
2.3
Introduction
Objectives
What is a Program and what is a Programming Language?
C Language
2.3.1
2.3.2
History of C
Salient Features of C
2.4
Structure of a C Program
2.5
2.6
Writing a C Program
Compiling a C Program
A simple C Program
2.6.1
2.6.2
2.7
2.8
2.9
2.10
2.11
2.0
The C Compiler
Syntax and Semantic Errors
Run the C Program through the Menu
Run from an Executable File
Linker Errors
Logical and Runtime Errors
INTRODUCTION
2.1
OBJECTIVES
23
An Introduction to C
2.2
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.
Programming languages can be divided into two categories:
(i)
(ii)
2.3
C LANGUAGE
2.3.1
History of C
Now let us see its historical development. The late 1960s were a turbulent era for
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 & Ts 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.
Basics of C
For many years the de facto standard for C was the version supplied with the UNIX
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
implementations were highly compatible. However, since no standard existed there
were discrepancies. To overcome this situation, ANSI established a committee in
1983 that defined an ANSI standard for the C language.
2.3.2
Salient features of C
25
An Introduction to C
2. What is the difference between high level language and low 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
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
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
in the keyword list,it cannot be used for naming a variable. The structure of a C
program is illustrated in Figure.2.1 where functions func1( ) through funcn( )
represent user defined functions.
Preprocessor directives
Global data declarations
main ( )
/* main function*/
{
Declaration part;
Program statements;
}
/*User defined functions*/
func1( )
{
}
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. Its 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
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
libraries provided.
#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
of this required information will be handled automatically by the compiler. You will
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
considered in the later units. Just remember, that this statement allows you to use
some predefined functions such as, printf(), in this case.
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
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 (;).
printf(This is a C program\n); prints the words on the screen. The text to be
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
statement, 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 programs
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.
2.
3.
4.
Start
Input the two numbers a and b
Calculate the sum as a+b
Store the result in sum
27
An Introduction to C
5.
6.
Flowchart
START
INPUT
a, b
Sum = a + b
PRINT
Sum
STOP
Figure 2.2: Flow chart to add two numbers
Program
#include <stdio.h>
main()
{
int a,b,sum;
/* variables declaration*/
/*output statement*/
}
OUTPUT
Enter the values of a and b:
23
The sum is 5
28
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
bit different from the first program; it includes a format specifier (%d). The format
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.
Basics of C
At this stage, dont 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
you will get the editor in front of you. For example:
C:> turboc\bin\tc
Here, tc.exe is stored in bin subdirectory of turboc directory. After you get the menu
just type the program and store it in a file using the menu provided. The file
automatically gets the extension of .c.
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
$ vi testprog.c
The editor is also used to make subsequent changes to the program.
2.6
COMPILING A C PROGRAM
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
understood by the computer. So, the next step is to convert the high-level language
program (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
Language
Program
Translator
(Compiler)
Source
Code
Machine
Language
Program
Object
Code
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.
2.6.2
Every language has an associated grammar, and the program written in that language
has to follow the rules of that grammar. For example in English a sentence such a
Shyam, is playing, with a ball. This sentence is syntactically incorrect because
commas should not come the way they are in the sentence.
Likewise, C also follows certain syntax rules. When a C program is compiled, the
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.
Let us consider the following program.
Example 2.3: Write a program to print a message on the screen.
/* Program to print a message on the screen*/
#include <stdio.h
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:
30
#include <stdio.h>
main( )
{
printf (Hello, how are you\n);
}
Apart from syntax errors, another type of errors that are shown while compilation are
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.
Basics of C
2.
3.
2.7
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
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
provides 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
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
2.7.3
Linker Errors
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
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;
the definition for this function is present in the header file that is why we do not get a
compiler error. Such kinds of errors are called linker errors. The executable file would
be created successfully only if these linker errors are corrected.
2.7.4
After the program is compiled and linked successfully we execute the program. Now
there are three possibilities:
1)
2)
3)
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( )
{
int a,b,c,sum,avg;
Basics of C
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,
therefore only the integer part is stored in avg. Such kinds of errors which are not
detected by the compiler or the linker are known as logical errors.
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
stops in between and the run time error message is flashed on the screen. Let us look
at the following example:
Example 2.5: Write a program to divide a sum of two numbers by their
difference
/* Program to divide a sum of two numbers by their difference*/
#include <stdio.h>
main( )
{
int a,b;
float c;
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
DEBUG SYNTAX
ERRORS (IF ANY),
SAVE AND RECOMPILE
2.
3.
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
advantage of high level language over low level language is discussed.
Basics of C
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
2.
3.
35
An Introduction to C
2.11
1.
2.
3.
4.
5.
36
FURTHER READINGS
UNIT 3
Variables and
Constants
Structure
3.0
3.1
3.2
3.3
Introduction
Objectives
Character Set
Identifiers and Keywords
3.3.1
3.3.2
3.4
3.5
3.6
3.7
3.8
3.9
Integer Constants
Floating Point Constants
Character Constants
String Constants
3.10
3.11
3.12
3.13
Symbolic Constants
Summary
Solutions / Answers
Further Readings
3.0
INTRODUCTION
As every natural language has a basic character set, computer languages also have a
character set, rules to define words. Words are used to form statements. These in turn
are used to write the programs.
Computer programs usually work with different types of data and need a way to store
the values being used. These values can be numbers or characters. C language has two
ways of storing number valuesvariables and constantswith many options for
each. Constants and variables are the fundamental elements of each program. Simply
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
execution. In contrast, a constant has a fixed value that cant change.
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
types, constants, variables and arrays, declaration and naming conventions of
variables.
3.1
OBJECTIVES
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
3.
3.3
0-9
:
>
]
(
&
{
<
!
)
.
,
/
$
-
\
?
%
~
*
#
|
_
+
^
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
distinct name but one cannot select any name unless it conforms to valid name in C
language. Let us study first the rules to define names or identifiers.
3.3.1
3.3.2
Keywords
Keywords are reserved words which have standard, predefined meaning in C. They
cannot be used as program-defined identifiers.
38
Variables and
Constants
while
if
double
enum
const
for
sizeof
do
else
struct
register
float
signed
volatile
typedef
switch
break
extern
short
void
auto
case
static
return
unsigned
default
Note: Generally all keywords are in lower case although uppercase of same names
can be used as identifiers.
3.4
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:
1.
2.
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
together and use different ways (to be discussed later) to store them as per
requirement. The four basic data types are described in the following table 3.1:
Table 3.1: Basic Data Types
DATA TYPE
int
char
float
TYPE OF DATA
Integer
character
Floating point number
MEMORY
2 Bytes
1 Byte
4 bytes
double
8 bytes
RANGE
32,768 to 32,767
128 to 128
3.4e 38 to 3.4e
+38
1.7e 308 to 1.7e
+ 308
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.
3.5
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
32768 to 32,767
Long int
2147483648 to 2147483647
39
An Introduction to C
Signed int
32768 to 32767
Unsigned int
0 to 65535
Signed char
128 to 127
Unsigned char
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 computers memory. By using a variables
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
accessed later in the program. If the variable is accessed before it is assigned a value,
it may give garbage value. The data type of a variable doesnt change whereas the
value assigned to can change. All variables have three essential attributes:
the name
the value
the memory, where the value is stored.
For example, in the following C program a, b, c, d are the variables but variable e is
not declared and is used before declaration. After compiling the source code and look
what gives?
main( )
{
int a, b, c;
char d;
a = 3;
b = 5;
c = a + b;
d = a;
e=d;
.
.
}
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
Variables and
Constants
int c, d;
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)
a = 10;
b = 0.4 e 5;
c = a;
Using Assignment statement
The values are assigned just after the declarations are made.
For example,
a = 10;
b = 0.4 e 5;
c = a;
Check Your Progress 1
1)
function
max_value
int
day-of-the-week
what
union
2)
3)
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.
2.
3.
4.
Integer constants
Floating point constants
Character constants
String constants
Integer and Floating Point constants are numeric constants and represent numbers.
Rules to form Integer and Floating Point Constants
3.9.1
Integer Constants
Further, these constant can be classified according to the base of the numbers as:
1.
2.
3.
, not allowed
Illegal char.
Blankspace not allowed
Illegal char
The first digit should not be a zero
01
0743
4.
0777
Variables and
Constants
OX1
OXF77
Oxabcd.
x is not included
illegal char (.)
illegal char G
Maximum value
Decimal integer
Octal integer
Hexadecimal integer
32767
77777
7FFF
decimal unsigned.
decimal long.
otal long.
otal unsigned.
A Floating Point number taking the value of 5 x 104 can be represented as:
5000.
5e+4
5.0e+4
5e4
5E4
.5e5
43
An Introduction to C
x,
3,
blank has 32
ESCAPE SEQUENCE
There are some non-printable characters that can be printed by preceding them with \
backslash character. Within character constants and string literals, you can write a
variety of escape sequences. Each escape sequence determines the code value for a
single character. You can use escape sequences to represent character codes:
Escape Sequence
"
'
?
\
BEL
BS
FF
NL
CR
HT
VT
\"
\'
\?
\\
\a
\b
\f
\n
\r
\t
\v
3.9.4
String Constants
3.10
44
Blue Sea
41213*(I+3) .
SYMBOLIC CONSTANTS
#define name
text
Variables and
Constants
printf print
MAX 100
TRUE 1
FALSE 0
SIZE 10
2)
3)
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
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
# define PI 3.14
2.
Integer constant:
Character constants:
String constants:
3.13
1.
2.
3.
46
0147
A, \r
A, EFH
FURTHER READINGS
Expressions and
Operators
Structure
4.0
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.9
4.10
4.11
4.12
4.13
4.0
Introduction
Objectives
Assignment Statements
Arithmetic Operators
Relational Operators
Logical Operators
Comma and Conditional Operators
Type Cast Operator
Size of Operator
C Shorthand
Priority of Operators
Summary
Solutions / Answers
Further Readings
INTRODUCTION
In the previous unit we have learnt variables, constants, datatypes and how to declare
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
sequence of operators and operands that does one or a combination of the following:
4.1
OBJECTIVES
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
an arithmetic expression;
a relational expression;
a logical expression;
a mixed expression.
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.
Arithmetic operators, relational operators and logical operators are discussed in the
following sections.
For example,
int a;
float b,c ,avg, t;
avg = (b+c) / 2;
a = b && c;
a = (b+c) && (b<c);
4.3
/*arithmetic expression */
/*logical expression*/
/* mixed expression*/
ARITHMETIC OPERATORS
The basic arithmetic operators in C are the same as in most other computer languages,
and correspond to our usual mathematical/algebraic symbolism. The following
arithmetic operators are present in C:
Operator
Meaning
Addition
Subtraction
Multiplication
Division
Modular Division
Some of the examples of algebraic expressions and their C notation are given below:
Expression
48
C notation
b* g
d
(b *g) / d
a3+cd
(a*a*a) + (c*d)
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.
Expressions and
Operators
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:
If the data type is double instead of float, then we get a result of double data type.
For example,
Operation
Result
5/3
5.0/3
1.3
5/3.0
1.3
5.0/3.0
1.3
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 )
2
3
3.
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
referred to as increment (++) and decrement (--) operators, which we are going to be
discussed below:
The two-unary arithmetic operators provided by C are:
Expressions and
Operators
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.
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
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
variable value is incremented (decrement) by 1.
The explanation is given in the table below:
Expression
Explanation
++a
a++
--b
b--
The precedence of these operators is right to left. Let us consider the following
examples:
int 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
- --c
step1
step2
final result
a*4c2 - d
m+n
ii)
ab - (e+f)4
c
2.
3.
4.4
RELATIONAL OPERATORS
Expressions and
Operators
Relational Operator
==
!=
<
<=
>
>=
Condition
x==y
x!=y
x<y
x<=y
x>y
x>=y
Meaning
x is equal to y
x is not equal to y
x is less than y
x is less than or equal to y
x is greater than 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)
4.5
LOGICAL OPERATORS
53
An Introduction to C
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:
if ((marks >=65) && (marks <= 75))
printf (Grade is B\n);
(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,
if ((PCM >=85) ||(aggregate >=90))
printf (Eligible for admission\n);
Logical negation (!) enables the programmer to reverse the meaning of the condition.
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
condition as an operand. Let us consider an example:
if !(grade==A)
printf (the next grade is %c\n, grade);
The parentheses around the condition grade==A are needed because the logical
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
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
54
x&&y
zero
zero
Non zero
zero
zero
Non zero
Non zero
Non zero
Expressions and
Operators
x || y
zero
zero
Non zero
zero
zero
Non zero
Non zero
Non zero
!x
zero
Non zero
The following table 4.5 shows the operator precedence and associativity
Table 4.5: (Logical operators precedence and associativity)
Operator
Associativity
Right to left
!
4.6
&&
Left to right
||
Left to right
Conditional Operator
C provides an called as the conditional operator (?:) which is closely related to the
if/else structure. The conditional operator is Cs 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
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
operand. The left side of comma operator is always evaluated to void. This means that
the expression on the right hand side becomes the value of the total comma-separated
expression. For example,
x = (y=2, y - 1);
first assigns y the value 2 and then x the value 1. Parenthesis is necessary since
comma operator has lower precedence than assignment operator.
Generally, comma operator (,) is used in the for loop (will be introduced in the next
unit)
For example,
for (i = 0,j = n;i<j; i++,j--)
{
printf (A);
}
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 jare separated by comma (,).
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.
56
x = (a<b)?++a:++b;
printf(x=%d a=%d b=%d\n,x,a,b);
Expressions and
Operators
3.
4.7
We have seen in the previous sections and last unit that when constants and variables
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
followed:
1.
All chars and short ints are converted to ints. All floats are converted to
doubles.
2.
In case of binary operators, if one of the two operands is a long double, the other
operand is converted to long double,
else if one operand is double, the other is converted to double,
else if one operand is long, the other is converted to long,
else if one operand is unsigned, the other is converted to unsigned,
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
sizeof(int) = 2 bytes
4.9
C SHORTHAND
Operators
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
&&=
||=
58
Examples
Examples
a&&=c
a||=c
Meaning
a=a&&c
a=a||c
4.10
Expressions and
Operators
PRIORITY OF 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
()
! ++ -- (type) sizeof
/%
+< <= > >=
== !=
&&
||
?:
= += -= *= /= %= &&= ||=
,
Associativity
Left to right
Right to left
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Right to left
Right to left
Left to right
2. What is the difference between && and &. Explain with an example.
3.
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 Cs 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.
4.12
SOLUTIONS / ANSWERS
1. C expression would be
i)
ii)
((a*4*c*c)-d)/(m+n)
a*b-(e+f)*4/c
2.
3.
2.
3.
60
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.
2.
&& operator is a logical and operator and & is a bit wise and operator.
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:
Expressions and
Operators
Use of Bit Wise operators makes the execution of the program faster.
4.13
FURTHER READINGS
61