0% found this document useful (0 votes)
14 views129 pages

Introduction to Computer Programming

Getting started with C programming

Uploaded by

Ann Kingori
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views129 pages

Introduction to Computer Programming

Getting started with C programming

Uploaded by

Ann Kingori
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INTRODUCTION TO COMPUTER PROGRAMMING

Definitions

Computer Program: A computer program is a set of coded instructions given to the


computer, and represents a logical solution to a problem. It directs a computer in
performing various operations/tasks on the data supplied to it.

Computer programs may be written by the hardware manufacturers, Software houses,


or a programmer to solve user problems on the computer.

Programming: Programming is the process of designing a set of instructions (computer


programs) which can be used to perform a particular task or solve a specific problem.

It involves use of special characters, signs and symbols found in a particular


programming language to create computer instructions. The programming process is
quite extensive. It includes analyzing of an application, designing of a solution, coding
for the processor, testing to produce an operating program, and development of other
procedures to make the system function.

The program created must specify in detail the logical steps to be taken & the method of
processing the data input into the computer in order to carry out the specified task.

A computer program performs the following:

1. Accepts data from outside the computer as its input.


2. Carries out a set of processes on the data within the computer memory.
3. Presents the results of this processing as its output, and
4. Stores the data for future use.

Programming Languages: A programming language is a set of symbols (a language)


which a computer programmer uses to solve a given problem using a computer.

1
The computer must be able to translate these instructions into machine-readable form
when arranged in a particular sequence or order.
TERMS USED IN COMPUTER PROGRAMMING

Source program (source code)


The term Source program refers to program statements that the programmer enters in
the program editor window, and which have not yet been translated into machine-
readable form.

Source code is the code understood by the programmer, and is usually written in high-
level language or Assembly language.

Object code (object program)


The term Object code refers to the program code that is in machine-readable (binary)
form. This is the code/language the computer can understand, and is produced by a
Compiler or Assembler after translating the Source program into a form that can be
readily loaded into the computer.

LANGUAGE TRANSLATORS

A computer uses & stores information in binary form, and therefore, it cannot
understand programs written in either high-level or low-level languages. This means
that, any program code written in Assembly language or high-level language must be
translated into Machine language, before the computer can recognize & run these
programs.

A Translator is special system software used to convert the Source codes (program
statements written in any of the computer programming languages) to their Object
codes (computer language equivalents).

2
The Translators reside in the main memory of the computer, and use the program code
of the high-level or Assembly language as input data, changes the codes, and gives the
output program in machine-readable code.

In addition, translators check for & identify some types of errors (e.g.,
Syntax/grammatical errors) that may be present in the program being translated. They
will produce error messages if there is a mistake in the code.

Each language needs its own translator. Generally, there are 3 types of language
translators:
i) Assembler.
ii) Interpreter.
iii) Compiler.

Note. Interpreters & Compilers translate source programs written in high-level


languages to their machine language equivalents.

Assembler
An assembler translates programs written in Assembly language into machine
language that the computer can understand and execute.

Functions of an Assembler.
1. It checks whether the instructions written are valid, and identifies any errors in
the program.
The Assembler will display these errors as well as the complete source and object
programs. If the program has no errors, the job control will let it run immediately,
or save the object program so that it may run it later without translating it again.

2. It assigns memory locations to the names the programmer uses.

E.g., the Assembler keeps a table of these names so that if an instruction refers to
it, the Assembler can easily tell the location to which it was assigned.

3
3. It generates the machine code equivalent of the Assembly instructions.

Usually, the Assembler generates a machine code only when no errors are detected.
Some of the errors include;

- Typing mistakes.
- Using the wrong format for an instruction.
- Specifying a memory location outside the range 0 – 2047.
Note. The Assembler cannot detect Logic errors. The programmer knows of these
errors only when the program is run & the results produced are incorrect (not what the
programmer expected). The programmer must therefore, go through the program &
try to discover why an incorrect result was being produced.

Interpreter

An interpreter translates a source program word by word or line by line. This allows
the CPU to execute one line at a time.

The Interpreter takes one line of the source program, translates it into a machine
instruction, and then it is immediately executed by the CPU. It then takes the next
instruction, translates it into a machine instruction, and then the CPU executes it, and so
on.

The translated line is not stored in the computer memory. Therefore, every time the
program is needed for execution, it has to be translated.

Compiler
A compiler translates the entire/whole source program into object code at once, and
then executes it in machine language code. These machine code instructions can then
be run on the computer to perform the particular task as specified in the high-level
program.

4
The process of translating a program written in a high-level source language into
machine language using a compiler is called Compilation.

For a given machine, each language requires its own Compiler. E.g., for a computer to
be able translate a program written in FORTRAN into machine language; the program
must pass through the FORTRAN compiler (which must ‘know’ FORTRAN as well as the
Machine language of the computer).

The object code file can be made into a fully executable program by carrying out a
Linking process, which joins the object code to all the other files that are needed for
the execution of the program. After the linking process, an executable file with an .EXE
extension is generated. This file is stored on a storage media.

Points to note.

The job of a Compiler is much more difficult than that of an Assembler in that, a single
statement in a high-level language is equivalent to many machine instructions.
The format of an Assembly instruction is fairly fixed, while high-level languages give
a lot of freedom in the way the programmer writes statements.

Functions of a compiler.
A Compiler performs the following tasks during the compilation process:

1). It identifies the proper order of processing, so as to execute the process as fast
as possible & minimize the storage space required in memory.

2). It allocates space in memory for the storage locations defined in the program to
be executed.

3). It reads each line of the source program & converts it into machine language.

5
4). It checks for Syntax errors in a program (i.e., statements which do not conform
to the grammatical rules of the language). If there are no syntax errors, it
generates machine code equivalent to the given program.
5). It combines the program (machine) code generated with the appropriate
subroutines from the library.
6). It produces a listing of the program, indicating errors, if any.

Differences between Compilers and Interpreters

Interpreter Compiler
1. Translates & executes each statement of 1. Translates all the source code statements
the source code one at a time. at once as a unit into their corresponding
object codes, before the computer can

The source code instruction is translated execute them.

& immediately obeyed by the computer A Compiler translates the entire source

hardware before the next instruction can program first to machine code, and then

be translated. the code is executed by the CPU.

(Translation & execution go together). (Translation & execution are separate


phases)
2. Translates the program each time it
is needed for execution; hence, it is
slower than compiling. 2. Compiled programs (object codes) can
be saved on a storage media and run
3. Interpreted object codes take less when required; hence executes faster
memory compared to compiled than interpreted programs.
programs.
3. Compiled programs require more
memory as their object files are larger.
4. For an Interpreter, the syntax
(grammatical) errors are reported &
corrected before the execution can 4. For a Compiler, the syntax errors are

continue. reported & corrected after the source

6
code has been translated to its object
5. An Interpreter can relate error code equivalent.
messages to the source program, which is
always available to the Interpreter. This 5. Once the source program has been
makes debugging of a program easier translated, it is no longer available to the
when using an Interpreter than a Compiler, so the error messages are
Compiler. usually less meaningful.

Linkers & Loaders


Computer programs are usually developed in Modules or Subroutines (i.e., program
segments meant to carry out the specific relevant tasks). During program translation,
these modules are translated separately into their object (machine) code equivalents.

The Linker is a utility software that accepts the separately translated program modules
as its input, and logically combines them into one logical module, known as the Load
Module that has got all the required bits and pieces for the translated program to be
obeyed by the computer hardware.

The Loader is a utility program that transfers the load module (i.e., the linker output)
into the computer memory, ready for it to be executed by the computer hardware.

Syntax
Each programming language has a special sequence or order of writing characters.
The term Syntax refers to the grammatical rules, which govern how words, symbols,
expressions and statements may be formed & combined.

Semantics
These are rules, which govern the meaning of syntax. They dictate what happens
(takes place) when a program is run or executed.

7
LEVELS OF PROGRAMMING LANGUAGES
There are many programming languages. The languages are classified into 2 major
categories:

i) Low-level programming languages.


ii) High-level programming languages.
Each programming language has its own grammatical (syntax) rules, which must be
obeyed in order to write valid programs, just as a natural language has its own rules for
forming sentences.

LOW-LEVEL LANGUAGES
These are the basic programming languages, which can easily be understood by the
computer directly, or which require little effort to be translated into computer
understandable form.
They include:
i) Machine languages.
ii) Assembly languages.

Features of low-level languages


They are machine hardware-oriented.
They are not portable, i.e., a program written for one computer cannot be installed
and used on another computer of a different family.
They use Mnemonic codes.
They frequently use symbolic addresses.

Machine languages (1st Generation languages)


Machine language is written using machine codes (binary digits) that consist of 0’s &
1’s.
The computer can readily understand Machine code (language) instructions without any
translation.

8
A programmer is required to write his program in strings of 0’s & 1’s, calculate &
allocate the core memory locations for his data and/or instructions.
Different CPU’s have different machine codes, e.g., codes written for the Intel Pentium
processors may differ from those written for Motorola or Cyrix processors. Therefore,
before interpreting the meaning of a particular code, a programmer must know for
which CPU the program was written.

Note. The computer can only execute instructions which are written in machine
language. This is because; it is the only language which the computer can understand.
Therefore, any program written in any other programming language must first be
translated into machine language (binary digits) before the computer can understand.

Assembly language (2nd Generation Languages).


Assembly languages were developed in order to speed up programming (i.e., to
overcome the difficulties of understanding and using machine languages).

The vocabulary of Assembly languages is close to that of machine language, and their
instructions are symbolic representations of the machine language instructions.

 Assembly language programs are easier to understand, use & modify compared
to Machine language programs.
 Assembly language programs have less error chances.

To write program statements in Assembly language, the programmer uses a set of


symbolic operation codes called Mnemonic codes.

The code could be a 2 or 3 shortened letter word that will cause the computer to
perform specific operation. E.g., MOV – move, ADD - addition, SUB – subtraction, RD -
read.

Example;

RD PAT, 15 (read the value 15 stored in the processor register named


PAT)
SUB PAT, 10 (subtract 10 from the value in register PAT)

9
A program written in an Assembly language cannot be executed/obeyed by the
computer hardware directly. To enable the CPU, understand Assembly language
instructions, an Assembler (which is stored in a ROM) is used to convert them into
Machine language. The Assembler accepts the source codes written in an Assembly
language as its input, and translates them into their corresponding computer language
(machine code/ object code) equivalent.
Comments are incorporated into the program statements to make them easier to be
understood by the human programmers.

Assembly languages are machine-dependent. Therefore, a program written in the


Assembly language for a particular computer cannot run on another make of computer.

Advantages of Low-level languages


1. The CPU can easily understand machine language without translation.
2. The program instructions can be executed by the hardware (processor) much
faster. This is because; complex instructions are already broken down into
smaller simpler ones.
3. Low-level languages have a closer control over the hardware, are highly efficient
& allow direct control of each operation.
They are therefore suitable for writing Operating system software & Game programs,
which require fast & efficient use of the CPU time.

4. They require less memory space.


5. Low-level languages are stable, i.e., they do not crash once written.

Disadvantages of Low-level languages


Very few computer programs are actually written in machine or Assembly language
because of the following reasons;

1. Low-level languages are difficult to learn, understand, and write programs in them.
2. Low-level language programs are difficult to debug (remove errors from).

10
3. Low-level languages have a collection of very detailed & complex instructions that
control the internal circuiting of the computer. Therefore, it requires one to
understand how the computer codes internally.
4. Relating the program & the problem structures is difficult, and therefore
cumbersome to work with.
5. The programs are very long; hence, writing a program in a low-level language is
usually tedious & time consuming.
6. The programs are difficult to develop, maintain, and are also prone to errors (i.e., it
requires highly trained experts to develop and maintain the programs).
7. Low level languages are machine-dependent (specific), hence non-portable.
This implies that, they are designed for a specific machine & specific processor, and
therefore, cannot be transferred between machines with different hardware or
software specifications.
8. It is not easy to revise the program, because this will mean re-writing the program
again.

High-level programming languages

High-level languages were developed to solve (overcome) the problems encountered


in low-level
programming languages. The grammar of High-level languages is very close to the
vocabulary of the natural languages used by human beings. Hence; they can be read
and understood easily even by people who are not experts in programming.

Most high-level languages are general-purpose & problem-oriented. They allow the
programmer
to concentrate on the functional details of a program rather than the details of the
hardware on
which the program will run.

11
High-level language programs are machine-independent, (i.e., they do not depend on a
particular
machine, and are able to run in any family of computers provided the relevant
translator software is installed). Programs written in a high-level language cannot be
obeyed by the computer hardware directly. Therefore, the source codes must be
translated into their corresponding machine language equivalent. The translation
process is carried out by a high-level language software translator such as a Compiler
or an Interpreter.

Features of high-level programming languages

 They contain statements that have an extensive vocabulary of words, symbols,


sentences &
mathematical expressions, which are very similar to the normal English
language.
 Allow modularization (sub-routines).
 They are ‘user-friendly’ and problem-oriented rather than machine-based. This
implies that,
during a programming session, the programmer concentrates on problem-
solving rather than how a machine operates.
 They require one to be obey a set of rules when writing the program.
 Programs written in high-level languages are shorter than their low-level
language
equivalents, since one statement translates into several machine code
instructions.
 The programs are portable between different computers.

Purpose of High-level languages

i) To improve the productivity of a programmer. This is because; the source


programs of high-level languages are shorter than the source programs of low-
level languages, since one statement translates into several machine code
instructions.

12
ii) To ease the training of new programmers, since there is no need to learn the
detailed layout
of a procession/sequence.
iii) To speed up testing & error correction.
iv) To make programs easy to understand & follow.

Advantages of High-level languages.

i) They are easily portable, i.e., they can be transferred between computers of
different families and run with little or no modification.
ii) High-level language programs are short, and take shorter time to be translated.
iii) They are easy to lean, understand and use.
iv) They are easy to debug (correct/remove errors), & maintain.
v) High level language programs are easy to modify, and also to incorporate
additional features thus enhancing its functional capabilities.
vi) They are ‘user-friendly’ & problem-oriented; hence, can be used to solve
problems arising from the real world.

Disadvantages of using High-level languages

i) High-level languages are not machine-oriented; hence, they do not use the CPU
and
hardware facilities efficiently.
ii) The languages are machine-independent, and cannot be used in programming
the hardware
directly.
iii) Each high-level language statement converts into several machine code
instructions. This
means that, they use more storage space, and it also takes more time to run the
program.

13
iv) Their program statements are too general; hence, they execute slowly than their
machine
code program equivalents.
v) They have to be interpreted or compiled to machine-readable form before the
computer can
execute them.
vi) The languages cannot be used on very small computers.
vii) The source program written in a high-level language needs a Compiler, which is
loaded into the main memory of the computer, and thus occupies much of
memory space. This greatly reduces the memory available for a source program.

Types of high-level languages

High-level languages are classified into five different groups:

1. Third generation languages (Structured / Procedural languages).


2. Fourth generation languages (4GLs).
3. Fifth generation languages (5GLs)
4. Object-oriented programming languages (OOPs).
5. Web scripting languages.

The various types of high-level languages differ in:

 The data structures they handle.


 The control structures they support.
 The assignment instructions they use.
 Application areas, e.g., educational, business, scientific, etc.

Structured Languages (3rd Generation Languages)

The third generation of programming language, 3GL, or procedural language uses a


series of English-like words, that are closer to human language, to write instructions. A
structured (procedural) language allows a large program to be broken into smaller

14
subprograms called modules, each performing a particular (single) task. This
Technique of program design is referred to as structured programming. Structured
programming also makes use of a few simple control structures in problem solving.
Examples of 3GLs are BASIC, PASCAL, COBOL, FORTRAN, Ada, C, LOGO, COROL,
RPG, SNOBOL
The 3 basic control structures are:

 Sequence
 Selection.
 Iteration (looping).

Advantages of structured programming.

i) It is flexible.
ii) Structured programs are easier to read.
iii) Programs are easy to modify because; a programmer can change the details of a
section
without affecting the rest of the program.
iv) It is easier to document specific tasks.
v) Use of modules that contain standard procedures throughout the program saves
development time.
vi) Modules can be named in such a way that they are consistent and easy to find in
documentation.
vii) Debugging is easier because; each module can be designed, coded & tested
independently.

Problem Oriented languages (Fourth Generation Languages - 4GL’s).

4GLs make programming even easier than the 3GLs because; they present the
programmer with more programming tools, such as command buttons, forms,
textboxes etc. The programmer simply selects graphical objects called controls on the
screen, and then uses them to create designs on a form by dragging a mouse pointer.
The languages also use application generators (which in the background) to generate

15
the necessary program codes; hence, the programmer is freed from the tedious work of
writing the code.
4GLs are used to enquire & access the data stored in database systems; hence, they are
described as the Query languages.

Examples of 4GLs are:

16
 Visual Basic, Delphi Pascal, Visual COBOL (Object COBOL), Access Basic,
SQL, NOMAD, FOCUS

Advantages of fourth generation languages.

1. They are user-based, and therefore, easy to learn & understand.


2. The grammar of 4GL’s is very close to the natural English language. It uses
menus &
prompts to guide a non-specialist to retrieve data with ease.
3. Very little training is required in order to develop & use 4GL programs.
4. They provide features for formatting of input, processing, & instant reporting.

Natural languages (Fifth Generation Languages - 5GL’s)

The 5GL’s are designed to make a computer solve a problem by portraying human-
like
intelligence. The languages are able to make a computer solve a problem for the
programmer; hence, he/she does not spend a lot of time in coming up with the
solution. The programmer only thinks about what problem needs to be solved and
what conditions need to be met without worrying about how to implement an
algorithm to solve the problem. Fifth generation programming allows people to
interact with computers without needing any specialized knowledge. People can
talk to computers and the voice recognition systems can convert spoken sounds into
written words.5GLs are mostly used in artificial intelligence.
Examples of 5GLs are:

 PROLOG
 LISP
 Mercury
 OCCAM

17
Factors to consider when choosing a Programming language

1. The availability of the relevant translator


2. Whether the programmer is familiar with the language
3. Ease of learning and use
4. Purpose of the program, i.e., application areas such as education, business,
scientific, etc.
5. Execution time; Applications that require quick response are best
programmed in machine code or assembly language. High-level languages
are not suitable for such application because, they take long to be translated
& executed.
6. Development time; Development time is the time a programmer takes to write
and run a program. High-level languages are easy to read, understand and
develop; hence, they require less
development time. Machine code & Assembly languages are relatively
difficult to read,
understand and develop; hence, they are time-consuming.
7. Documentation; It should have accompanying documentation (descriptions)
on how to use the language or maintain the programs written in the language.
8. Availability of skilled programmers; The language selected should have a
pool of readily available programmers to ease the programming activity, and
reduce development time.

PROGRAMING APPROACHES

a) Object-Oriented Programming Languages (OOP)

OOP is a new approach to software development in which data & procedures that
operate on data are combined into one object. OOP use objects. An Object is a
representation of a software entity such as a user-defined window or variable. Each
object has specific data values that are unique to it (called state) and a set of the
things it can accomplish called (functions or behavior). Several objects can be linked

18
together to form a complete program. Programs send messages to an object to
perform a procedure that is already embedded in it. Examples of Object-oriented
programming languages are: - Simula, C++, SmallTalk, Java.

b) Web Scripting Languages

Web scripting languages are mostly used to create or add functionalities on web
pages.
Web pages are used for creating Web sites on the Internet where all sorts of
advertising can be done. Web pages are hypertext (plain-text) documents written
using a language called HyperText Markup Language (HTML).

c) Event-driven Programming languages

It is a programming paradigm in which the flow of the program is determined by


events such as user actions (mouse clicks, movements, key presses), sensor outputs,
or messages from other programs/threads. Event-driven programming is the
dominant paradigm used in graphical user interfaces and other applications (e.g.,
JavaScript web applications) that are centered on performing certain actions in
response to user input. It is popularly used in graphical user interface systems and
web applications. Examples include Visual Basic, Visual Delphi, and Java etc.

d) Mobile application Programming Languages

Used to develop application software for handheld devices as personal digital


assistant and mobile phones. These applications can be pre-installed on phone
during manufacturing or downloaded by customers e.g., action script, Java, Visual
Blocks.

e) Procedure/ structured programming

Notes as given above.

19
f) Logic Programming

Logic (declarative) programming allows a program to model a problem by


declaring what outcome the program should accomplish, rather than how it should
be accomplished. Sometimes called rule-based languages, since the program’s
declarations look more like a set of rules, or constraints on the problem, rather than
a sequence of commands to be carried out.

g) Imperative programming

The oldest and the most well-developed, it emerged with the first computers in the
1940s and its elements directly mirror the architectural characteristics of modern
computers as well. The program and its variables are stored together and the
program contains a series of commands that perform calculations, assign values to
variables, retrieve input, produce out, or redirect control elsewhere in the series.

h) Modular programming

Modular programming is the concept that similar functions should be contained


within the same unit of programming code and that separate functions should be
developed as separate units of code so that the code can easily be maintained and
reused by different programs. Object-oriented programming is a newer idea that
inherently encompasses modular programming.

Modular programming can make the code more readable, maintainable, portable,
and reliable. In addition, carving up a design into separate modules makes it much
easier for individuals on a team to work independently on separate parts of the
implementation, without getting in each other's way.

i) Monolithic Programming

In these types of programming languages, the program is written with a single


function. A program is not divided into parts; hence it is named as monolithic

20
programming. It is also called single thread execution. There is no support of
subroutine concept and hence it is suitable for developing small and simple
applications. The program flow control is achieved through the use of jump
statements such as goto that transfers control to any statement as specified in it.
Since there is no concept of subroutine, the program code is duplicated at each
time. There is no data abstraction and hence difficult to maintain the program code.
When the program size increases it leads to difficulty. The global data can be
accessed from any portion of the program. Due to this reason the data is not fully
protected.

Ex: Assembly language, BASIC.

Comparisons between monolithic and modular programming

1. Monolith is better suited for Critical Apps like Blockchain, Automated Trade
and health care where we can lose money or life. Modular is better suited for
non-critical apps like social apps, educational app or games.
2. Monolith is less vulnerable to hacks than Modular, due to far less entry and
exit points.
3. Monolith is a nightmare to scale up, modular is a breeze to scale up and
experiment.
4. Monoliths are generally designed with immutability in mind, modulars are
designed for flexibility and quickness.
5. Good Monolith designs perform better in terms of resource consumption
when compared with equally good modular design.

Programming Qualities

Good programming principles and practice aim at producing a program with the
following characteristics;

 Reliability: the program can be depended upon always to do what it is


supposed to do

21
 Maintainability: the program will be easy to change or modify when the need
arises
 Portability: the program will be transferable to a different computer with a
minimum modification.
 Readability: the program will be easy for a programmer to read and
understand.
 Performance: the program causes the tasks to be done quickly and efficiently.
 Storage saving: the program is not allowed to be unnecessarily long

PROGRAMMING METHODOLOGY

A Methodology is a system of methods with its orderly and integrated collection of


various methods, tools and notations. Two most popular programming methodology
are:

• Top-down methodology
• Bottom-up methodology

Top-down methodology

A top-down approach (is also known as step-wise design) is essentially the breaking
down of a system to gain insight into its compositional sub-systems. Each subsystem
is then refined in yet greater detail, sometimes in many additional subsystem levels,
until the entire specification is reduced to base elements. Top-down-design starts
with a description of the overall system and usually consists of a hierarchical
structure which contains more detailed descriptions of the system at each lower
level. The lower-level design details continue until further subdivision is no longer
possible.

Top-down is a programming style, the mainstay of traditional procedural languages,


in which design begins by specifying complex pieces and then dividing them into
successively smaller pieces. The technique for writing a program using top-down

22
methods is to write a main procedure that names all the major functions it will need.
Later, the programming team looks at the requirements of each of those functions
and the process is repeated. These partitioned sub-routines eventually will perform
actions so simple they can be easily and concisely coded. When all the various sub-
routines have been coded the program is ready for testing. By defining how the
application comes together at a high level, lower-level work can be self-contained.
By defining how the lower-level abstractions are expected to integrate into higher
level ones, interfaces become clearly defined.

Top-down approaches emphasize planning and a complete understanding of the


system. It is inherent that no coding can begin until a sufficient level of detail has
been reached in the design of at least some part of the system. The Top-Down
Approach is done by attaching the stubs in place of the module. This, however,
delays testing of the ultimate functional units of a system until significant design is
complete.

Bottom-up methodology

In a bottom-up approach the individual base elements of the system are first
specified in great detail. These elements are then linked together to form larger
subsystems, which then in turn are linked, sometimes in many levels, until a
complete top-level system is formed. In this model, the beginnings are small but
eventually grow in complexity and completeness.

Bottom-up emphasizes coding and early testing, which can begin as soon as the first
module has been specified. This approach, however, runs the risk that modules may
be coded without having a clear idea of how they link to other parts of the system,
and that such linking may not be as easy as first thought. Re-usability of code is one
of the main benefits of the bottom-up approach

23
PROGRAM DEVELOPMENT
Stages involved in the program development cycle.

The process of program development can be broken down into the following stages:
1. Problem recognition (Identification of the problem).
2. Problem definition.
3. Program design.
4. Program coding.
5. Program testing & debugging.
6. Program Implementation and maintenance.
7. Program documentation.

Problem recognition.
Problem recognition refers to the understanding and interpretation of a particular
problem. The programmer must know what problem he/she is trying to solve.
He/she must also understand clearly the nature of the problem & the function of the
program.

In order to understand a problem, look for the keywords such as compute, evaluate,
compare, etc. Usually, a programmer identifies problems in the environment and
tries to solve them by writing a computer program. There are 3 situations that cause
the programmer to identify a problem that is worth solving:
1. Problems or undesirable situations that prevent an individual or
organizations from achieving their purpose.
2. Opportunity to improve the current program.
3. A new directive given by the management requiring a change in the current
system.

Problem definition (Problem Analysis).


In Problem definition, the programmer tries to define (determine) the:

24
1. Output expected from the program.
2. Inputs needed to generate the output
information.
3. Processing activities (requirements),
4. Kind of files which may be needed.
 The programmer should write a narrative on what the program will do, and
how it is meant to achieve the intended purpose. Within this narrative, he/she
is required to determine what data is to be input & what information is to be
output.
 At the end of the problem definition, the programmer is required to write a
requirements report/document for the new program. This document will
enable the programmer to come up with a program design that meets the
needs at hand.
Note. Problem definition should be done thoroughly to ensure user satisfaction, and
to facilitate the subsequent stages in the program development cycle. A failure at
this stage usually results in a system that will not work as intended, or that may not
work at all.

Program design

Program design is the actual development of the program’s process or problem-


solving logic called the Algorithm. It involves identifying the processing tasks
required to be carried out in order to solve the problem.

The design stage enables the programmer to come up with a model of the expected
program (or a general framework (outline) of how to solve the problem, and where
possible, break it into a sequence of small & simple steps.
The models show the flow of events throughout the entire program from the time
data is input to the time the program gives out the expected information.

25
√ The processing tasks must be in order & systematic. Therefore, the programmer
identifies the processing tasks required, and the exact order in which they are to
be carried out.
√ The design process does not take account of the programming language to be
used in the final product, since it only defines program logic.

√ Program design provides for easy maintenance.

Note. It is important to design programs before entering them into the computer.
The programmer should only attempt to covert a design into a program code after
ensuring that it is logically correct. If possible, check the logical order on the desk.

Some programmers produce rough & ready solutions at a Keyboard, and continue to
amend the programs until eventually the program appears to do what was expected.
This is not recommended in programming because of the following reasons:
1. The final code may not be easy to follow, since it was just cobbled together.
2. Variable names & specific items of code may not be documented.
3. Programs produced by continuous amendments & changing of codes mostly
lead to unforeseen side effects.
4. E.g., there may not have been plan for testing the program or
procedures, hence, the program may easily fail.
5. A programmer may be asked to modify the code at a later date. Without
sufficient documentation, the programmer will be forced to trace through the
program in order to gain an insight into how the program functions.

Program coding
Program coding is the actual process of converting a design model into its
equivalent program. Coding requires the programmer to convert the design
specification (algorithm) into actual computer instructions using a particular
programming language.

26
The end result of this stage is a source program that can be translated into machine
readable form for the computer to execute and solve the target problem.

Rules followed in coding a program.

1. Use the standard identifiers or reserved words.


2. Make the program more readable by using meaningful identifiers.
3. Don’t use similar variables.
4. Keep spellings as normal as possible.
5. Use comments to explain variables & procedures. This makes the program
readable.
6. Avoid tricks – write the program using straightforward codes that people can
readily understand.
7. Modularize your program.

Program Testing and Debugging


After designing & coding, the program has to be tested to verify that it is correct,
and any errors detected removed (debugged).

Testing
Testing is the process of running computer software to detect/find any errors (or
bugs) in the program that might have gone unnoticed.

During program testing, the following details should be checked;


The reports generated by the system.
The files maintained in connection to the system’s information requirements.
The input to the system.
The processing tasks.
The controls incorporated within the system.

27
Note. The testing process is a continuous process, and it ends only when the
Programmer & the other personnel involved are satisfied that when operational, the
program will meet the objectives and the growing demands of the organization.

Types of program errors


There are 5 main types of errors that can be encountered when testing a program.
These are:
1. Syntax errors.
2. Run-time (Execution) errors.
3. Logical (arithmetic) errors.
4. Semantic errors.
5. Lexicon errors.

Syntax errors
Every programming language has a well-defined set of rules concerning formal
spellings, punctuations, naming of variables, etc. The instructions are accepted only
in a specified form & and must be obeyed by the programmer.
Syntax errors are therefore, programming errors/mistakes that occur if the
grammatical rules of a particular language are not used correctly.
Examples:
1. Punctuation mistakes, i.e., if the programmer does not use the right
punctuations & spaces needed by the translator program, e.g., omitting a
comma or a semicolon.
2. Improper naming of variables.
3. Wrong spellings of user defined and reserved words.

Reserved words are those words that have a special meaning to the programming
language, and should not be used by the programmer for anything else.

28
Syntax errors are committed by the programmer when developing, or transcribing
the program, and can be detected by the language translators, such as the
Compiler as it attempts to translate a program. Such errors must be corrected by
the programmer before the program runs.

Logical (arithmetic) errors.

These are errors in the program logic. They relate to the logic of processing
followed in the program to get the desired results. E.g., they may occur as a result
of misuse of logical operators.

Logical errors cannot be detected by the translator. The programmer will detect
them when the program results are produced. The program will run, but give the
wrong output or stop during execution.

Run-time (Execution) errors.


These errors occur during program execution. Run-time (execution) errors occur
when the programmer introduces new features in the program, which are not part of
the translator’s standards.
For example; they may occur if:

1. The computer is asked to divide a number by zero.


2. The number generated as a result of an instruction is too large to fit in a
memory location.
3. When you raise a number to a very big power that cannot be accommodated
in the Register’s structure of the computer.
4. In case of a closed loop in the program, leading to a set of instructions being
executed repetitively for a long time.
Execution errors are not detected by the translator programs, but are detected by
the computer during execution. Sometimes, execution errors may lead to premature
end of a program. To detect and eliminate Execution errors, a test run should be
performed on the program after it has been translated.

29
Semantic errors.
These are meaning errors. They occur when the programmer develops statements,
which are not projecting towards the desired goal. Such statements will create
deviations from the desired objectives.
Semantic errors are not detected by the computer. The programmer detects them
when the program results are produced.

Lexicon errors.

These are the errors, which occur as a result of misusing Reserved words (words
reserved for a particular language).

Debugging:

The term Bug is used to refer to an error in a computer program. Most programming
errors often remain undetected until an attempt is made to translate a program.
The most common errors include: -
• Improperly declared Constants and Variables.
• A reference to undeclared variable.
• Incorrect punctuation.

Debugging is therefore, the process of detecting, locating & correcting (removing,


eliminating) all errors (mistakes or bugs) that may exist in a computer program.

Types of Testing (Methods of error detection)


For the program to be assumed as correct, several testing need to be conducted by
the programmer to ascertain/establish their validity.

There are several methods of testing a program for errors. These include:
1. Dry running (Desk checking).
2. Translator system checking.

30
3. Functional testing.
4. Use of Test data.
5. Use of debugging utilities.
6. Diagnostic procedures.
7. System test with actual data.

1 Dry Running (Desk checking):


Dry running is a method of checking a program for errors by making the corrections
on a paper before entering it in the program editor. It involves going through the
program while still on paper verifying & validating its possible results. If the final
results agree with the original test data used, the programmer can then type the
program into the computer and translate it.

√ Dry running helps the programmer to identify the program instructions, detect the
most obvious syntax and logical errors, & the possible output.
√ Dry running is much faster. This is because; it involves the use of human brain as
the processor, which has got a well inbuilt common sense.
2 Translator system checking:
This is a type of testing, which involves the computer & the translator programs.
After entering the program, it is checked using a translator to detect any syntax
errors. The translator can be a Compiler or an Interpreter, which goes through the
set of instructions & produces a list of errors, or a program/statement listing which is
free from errors.

3 Functional testing (White-box testing):


This type of testing is based upon examining the internal structure of a program &
selecting test data, which give rise to the alternative cases of control flow.

31
4 Use of Test data.
The accuracy of a program can be tested by inputting a set of values referred to as
Test data. The test data is designed to produce predictable output.

There are 2 types of test data;


(a). Real data (live data): - test data obtained from the real problem environment
(practical applications).
(b). Dummy data: - assumed test data.

The programmer invents simple test data, which he/she uses to carry out trial runs of
the new program. At each run, the programmer enters various data variations
including data with errors to test how the system will behave. For example, if the
input required is of numeric type, the programmer may enter alphabetic characters.
The programmer will then compare the output produced with the predicted (actual)
output.

Notes.
Where possible, the program should be tested using the same test data that
was used for desk checking. Stricter/rigid tests should be applied on the
program in order to test the program to its limits.
Only Logical errors & Semantic errors can be corrected by the programmer
using test data.
A good program should not crash due to incorrect data entry but should inform
the user about the irregularity and request for the correct data to be entered.

5 Use of debugging utilities.


After the program has been entered in the program editor, debugging utilities
which are built in the computer can be run during translation to detect any syntax

32
errors in the program. The errors are corrected and the debugging process is
repeated again to find out more errors, before the program is executed.

6 Diagnostic procedures.
For complex programs, diagnostic procedures, such as Trace routines, may be
used to find logical errors. A Trace prints out the results at each processing step to
enable errors to be detected quickly.

7 System Test with actual data.


This is whereby the new program is run in parallel with the existing system for a
short time so that results can be compared and adjustments made. In such cases, the
system test is made using actual data.

IMPLEMENTATION AND MAINTENANCE.

Implementation

Implementation refers to the actual delivery, installation and putting of the new
program into use. The program is put into use after it is fully tested, well
documented, and after training the staff who will be involved in the running of the
new program.

Structured Walk Through: It is an organized style of evaluating/reviewing a


program by a team of other programmers, which then reports to the
programming team.

Review and Maintenance


Once the program becomes operational, it should be maintained throughout its life,
i.e., new routines should be added, obsolete routines removed, & the existing
routines adjusted so that the program may adapt to enhanced functional
environments.

33
The main objective of maintenance is to keep the system functioning at an
acceptable level. Program maintenance mainly involves: -

√ Correcting errors that may be encountered after the program has been
implemented or exposed to extensive use.

√ Changing procedures.
√ Hardware and software maintenance.
√ Changing parameters and algorithms used to develop the original programs.
√ Making any adjustments as new technology comes.

Note. Program maintenance runs parallel to the maintenance of the program


documentation, i.e., any time maintenance is carried out on the program, the
documentation should also be updated to convey the right image of the system.

PROGRAM DOCUMENTATION
After writing, testing, and debugging a program, it must be documented. In other
words, the programmer should describe all what he was doing during the program
development stages.

Program documentation is the writing of supportive materials explaining how the


program can be used by users, installed by operators, or modified by other
programmers.

Note. All the program development activities (i.e., from the initial stage up to the
complete program) should be documented/recorded in order to assist in the
development of the program, future modification of the program, general
maintenance, machine & software conversion at a later date, and program
changeover.

34
Documentation can either be; Internal or External.
 Internal documentation is the writing of non-executable lines (comments) in
the source program that help other programmers to understand the code
statements.
 External documentation refers to reference materials such as user manuals
printed as booklets.

Types of program documentation.

There are 3 target groups for any type of documentation:

1. User-oriented documentation: This enables the user to learn how to use the
program as quickly as possible, and with little help from the program developer.
2. Operator-oriented documentation: This is meant for computer operators
such as the technical staff. It is used to help them install & maintain the program.
3. Programmer-oriented documentation: This is a detailed documentation
written for skilled programmers. It provides the necessary technical information
to help in future modification of the program.

Some documents used in program documentation.


(1). User guide/ manual.

This is a manual provided for an end-user to enable him/her use or operate the
program with minimal or no guidance. A User guide is used in user-oriented
documentation.
(2). Reference guide.
It is used by someone who already knows how to use the program but needs to
be reminded about a particular point or obtain more detailed information about a
particular feature.
(3). Quick Reference guide.
This could be a single sheet or card small enough to fit into a pocket. It is used
by the user to get help for the common tasks carried out within the program.

35
(4). Technical manuals.
They are intended for System analysts & Programmers. They assist in
maintaining & modifying the program design and code.

Contents in a program document.


Documentation includes:
1. Title of the program.
2. Function of the program.
3. Language used.
4. Hardware & Software required to support the processing of the system.
5. File specifications (details of the data structures used, & details of how data files
are to be organized, accessed, and kept secure).
6. Limitations of the program.
7. Format of the input & the output expected.
8. Design of the program using the design tools (i.e., detailed algorithms &
procedures used).
9. A listing of the Source program and the program flowcharts.
10. A carefully devised set of Test data, and a table of expected results.
11. Detailed instructions on how to run the program.

DEVELOPING OF ALGORITHMS

After carefully analyzing the requirements specification, the programmer usually


comes up with the algorithm.

Definition of an Algorithm:

 An Algorithm is a limited number of logical steps that a program follows in order


to solve a problem.

36
 A step-by-step (a set of) instructions which when followed will produce a solution
to a given problem.
 Algorithms take little or no account of the programming language.
 They must be precise/ accurate, unambiguous/clear and should guarantee a
solution.

Program design Tools.


Algorithms can be illustrated using the following tools:
 Pseudocodes.
 Flowcharts.
 Decision Tables.
 Decision Trees.

Note. For any given problem, the programmer must choose which algorithm
(method) is best suited to solve it.

PSEUDOCODES.
A pseudocode is a method of documenting a program logic in which English-like
statements are used to describe the processing steps.

 These are structured English-like phrases that indicate the program steps to be
followed to solve a given problem.

√ The term “Code” usually refers to a computer program. This implies that, some of
the words used in a pseudocode may be drawn from a certain programming
language and then mixed with English to form structured statements that are
easily understood by non-programmers, and also make a lot of sense to
programmers.
However, pseudocodes are not executable by a computer.

37
Guidelines for designing a good pseudocode.

1. The statements must be short, clear and readable.


2. The statements must not have more than one meaning (i.e., should not be
ambiguous).
3. The pseudocode lines should be clearly outlined and indented.
4. A pseudocode must have a Begin and an end.
i.e., a pseudocode should show clearly the start and stop of executable
statements and the control structures.
5. The input, output and processing statements should be clearly stated using
keywords such as PRINT, READ, INPUT, etc.

FLOWCHARTS
 A Flowchart is a diagrammatic or pictorial representation of a program’s
algorithm.
 It is a chart that demonstrates the logical sequence of events that must be
performed to solve a problem.
Types of Flowcharts.

There are 2 common types of Flowcharts:


1). System flowchart.

A System flowchart is a graphical model that illustrates each basic step of a data
processing system.

It illustrates (in summary) the sequence of events in a system, showing the


department or function responsible for each event.

2). Program flowchart.

This is a diagram that describes, in sequence, all the operations required to


process data in a computer program.

38
A program flowchart graphically represents the types of instructions contained in
a computer program as well as their sequence & logic.

PROGRAM FLOWCHARTS.

A Flowchart is constructed using a set of special shapes (or symbols) that have
specific meaning. Symbols are used to represent operations, or data flow on a
flowchart. Each symbol contains information (short text) that describes what must be
done at that point.

The symbols are joined by arrows to obtain a complete Flowchart. The arrows show
the order in which the instruction must be executed.

SYMBOLS USED IN PROGRAM FLOWCHARTS.


Below is a standard set of symbols used to draw program flowcharts as created by
American National Standard Institute (ANSI).

1. Terminal symbol.

Ellipse (Oval in shape)

It is used to indicate the point at which a flowchart, a process or an algorithm


begins & end.
√ All Flowcharts must have a START & STOP symbol. The START/BEGIN symbol is
the first symbol of a flowchart, & identifies the point at which the analysis of the
flowchart should begin. The STOP/END symbol is the last symbol of a
flowchart, & indicates the end of the flowchart.

√ The words Begin & End (or Start & Stop) should be inserted in the Terminal
symbol.

39
2. Input or Output symbol.
(Parallelogram)

- It is used to identify/specify an input operation or output operation.


For example;
READ Employee Name PRINT Employee Name

Input operation Output operation


Note. The words mostly associated with I/O operations are READ & PRINT.
READ describes the entry of computer data, while PRINT relates to the printed
output of information.

3. Process symbol.
(Rectangle)

- Process symbol is used to indicate that a processing or data transformation is


taking place.
The information placed within the process symbol may be an algebraic formula or a
sentence to describe processing.

SUM = A + B Commission is computed at 20% of Total


Sales
Processing defined as a Formula Processing defined as a Sentence
Decision symbol.

NO

(Rhombus)
YES

40
It is used to indicate/ specify a condition or to show the decision to be made.
There are 2 main components of a Decision symbol:
1. A question asked within the Decision symbol, that indicates the
comparison / logical operation.
2. The results of the comparison (which are given in terms of YES or NO).
The arrows labeled YES or NO lead to the required action corresponding to the
answer to the question.

4. Flow lines.

Flow lines with arrowheads are used to indicate the direction of processing of
the program logic, i.e., they show the order in which the instructions are to be
executed.
The normal flow of a flowchart is from Top to Bottom, and Left to Right.

Note. Flow lines should never cross each other.


Connector symbol.

Sometimes, a flowchart becomes too long to fit in a single page, such that the flow
lines start crisscrossing at many places causing confusion & also making the
flowchart difficult to understand.

The Connector symbol is used as a connecting point for arrows coming from
different directions.

A Connector symbol is represented by a Circle, and a letter or digit is placed


within the circle to indicate the link.

41
Note. Connectors do not represent any operation. They are used to connect two
parts of a flowchart, indicating that the flow of data is not broken.

General guidelines for drawing a program flowchart.


A flowchart should have only one entry/starting point and one exit point (i.e.,
ensure that the flowchart has a logical start and finish).

1. The flowchart should be clear, neat and easy to follow.


2. Use the correct symbol at each stage in the flowchart.
3. The flowchart should not be open to more than one interpretation.
4. Avoid overlapping the lines used to show the flow of logic as this can create
confusion in the flowchart.
5. Make comparison instructions simple, i.e., capable of YES/NO answers.
6. The logical flow should be clearly shown using arrows.
Note. A flowchart should flow from the Top to Bottom of a page, and from the Left
to the Right.
7. Where necessary, use Connectors to reduce the number of flow lines.
Connectors are helpful when a flowchart is several pages long, and where
several loops are needed in the logic of the flowchart.
8. Check to ensure that the flowchart is logically correct & complete.

Notes.
A flowchart must have a Start and an end.

A flowchart is useful when the algorithm is short & the flowchart can fit
conveniently on a single page. If the flowchart is too large, it is recommended to
use Pseudocodes for long & complicated programs.

Advantages of using Flowcharts.

(i). Quicker understanding of relationships.

42
They assist programmers to understand procedures more quickly.

A programmer can represent a lengthy procedure more easily with the help
of a flowchart than describing it by means of written notes.
(ii). Effective synthesis.
Flowcharts may be used as working models in the design of new programs
and systems.

(iii). Proper program documentation.


Program flowcharts serve as good program documentation, which is needed
for the following reasons:

(a). If programs are modified in future, the flowcharts will direct the
programmer on what was originally done.

(b). When staff changes occur, the flowcharts may help new employees
understand the existing programs.
(c). Flowcharts assist in program conversion when new hardware/software are
acquired.

(iv). Effective coding.


Program flowcharts act as a guide during the program preparation stage.
Instructions coded in a programming language may be checked against the
flowchart to ensure that no steps are omitted.

(v). Orderly debugging and testing of programs.


Flowcharts help in detecting, locating and removing mistakes.
The programmer can refer to the flowchart as he/she re-checks the coding
steps, & the logic of the written instructions.

(vi). Efficient program maintenance.


Flowcharts facilitate the maintenance of operating programs. They help the
programmer to concentrate on the part of the information flow which is to be
modified.

43
Limitations of using Flowcharts.
(i). Flowcharts are complex, clumsy & become unclear, especially when the
program logic is complex.

(ii). If changes are to be made, the flowchart may require complete re-drawing.
(iii). Reproduction of flowcharts is usually a problem, since the flowchart symbols
cannot be typed.
(iv). No uniform practice is followed for drawing flowcharts as it is used as an aid to
the program.
(v). Sometimes, it becomes difficult to establish the link between various conditions,
and the actions to be taken upon a particular condition.

FUNDAMENTALS OF C PROGRAMMING LANGUAGE


Introduction to C Programming
C is a procedural systems implementation language. It is called a high level, compiler
language. The aim of any high-level computer language is to provide an easy and
natural way of giving a program of instructions to a computer (a computer program).
The language of the raw computer is a stream of numbers called machine code. C is
one of a large number of high-level languages which can be used for general purpose
programming, that is, anything from writing small programs for personal amusement
to writing complex applications. C is a general-purpose computer programming
language developed between 1969 and 1973 by Dennis Ritchie.

Characteristics of C
We briefly list some of C's characteristics that define the language and also have led
to its popularity as a programming language.
 C is case sensitive. All commands in C must be lowercase.
 C has a free-form line structure.
 End of each statement must be marked/terminated with a semicolon.
 Multiple statements can be on the same line. White space is ignored.
Statements can continue over many lines.

44
C has now become a widely used professional language for various reasons.
 It has high-level constructs.
 It can handle low-level activities.
 It produces efficient programs.
 It can be compiled on a variety of computers.

Its main drawback is that it has poor error detection which can make it off putting to
the beginner.

Executing a C program
The steps involved in executing a C program include;
λ Creating the program
λ Compiling the program
λ Linking the program with functions that are needed from the C library
λ Executing the program

Compiling a C Program

A C program is first written in the form of a number of text files using a screen editor.
This form of the program is called the source program. It is not possible to execute
this file directly. The completed source file is passed to a compiler a program which
generates a new file containing a machine code translation of the source text. The
compiler translates the source code into machine code, and the compiled code is
called the object code. The object code may require an additional stage where it is
linked with other object code that readies the program for execution. The machine
code created by the linker is called the executable code or executable program.

Instructions in the program are finally executed when the executable program is
executed (run). During the stages of compilation, linking, and running, error
messages may occur that require the programmer to make corrections to the program
source (debugging). The cycle of modifying the source code, compiling, linking, and
running continues until the program is complete and free of errors. A compiler usually
operates in two or more phases (and each phase may have stages within it).

45
Pre-processor directives
The C Preprocessor (CPP) is not part of the compiler, but is a separate step in the
compilation process. In simplistic terms, a C Preprocessor is just a text substitution
tool and they instruct compiler to do required pre-processing before actual
compilation. All preprocessor commands begin with a pound symbol (#). It must be
the first nonblank character, and for readability, a preprocessor directive should
begin in first column. Following section lists down all important preprocessor
directives:
Directive Description
#define Substitutes a preprocessor macro
#include Inserts a particular header from another file
#undef Undefines a preprocessor macro
#ifdef Returns true if this macro is defined
#ifndef Returns true if this macro is not defined
#if Tests if a compile time condition is true
#else The alternative for #if
#elif #else an #if in one statement
#endif Ends preprocessor conditional
#error Prints error message on stderr
#pragma Issues special commands to the compiler, using a
standardized method

Preprocessors Examples
#define max_array_length 20 This directive tells the C compiler to replace instances
of max_array_length with 20. Use #define for constants to increase readability.

#include <stdio.h>
#include "myheader.h"

46
These directives tell the CPP to get stdio.h from System Libraries and add the text to
the current source file. The next line tells CPP to get myheader.h from the local
directory and add the content to the current source file.

C Libraries

In C, a library is a set of functions contained within a single "archive" file. The core of
the C language is small and simple. Special functionality is provided in the form of
libraries of ready-made functions. This is what makes C so portable. Libraries are files
of ready-compiled code which we can merge with a C program at compilation time.
Libraries provide frequently used functionality and, in practice, at least one library
must be included in every program: the so-called C library, of standard functions.
Each library comes with a number of associated header files which make the functions
easier to use. Header files contains the prototypes of the functions contained within
the library that may be used by a program, and declarations of special data types and
macro symbols used with these functions. It is up to every programmer to make sure
that libraries are added at compilation time by typing an optional string to the
compiler.

Including Library files in C Program The most commonly used header file is the
standard input/output library which is called stdio.h. This belongs to a subset of the
standard C library which deals with file handling and provides standard facilities for
input to and output from a program. Examples of Libraries header files

Stdio.h, (printf() and scanf() functions)


maths.h (for mathematical functions) etc.
conio.h (for handling screen out puts such as pausing program execution getch()
function)

The format for including the header file is #include header.h

47
C Program Structure
C program is can be divided into modules and functions.

Modules: A module is a set of functions that perform related operations. A simple


program consists of one file; i.e., one module. More complex programs are built of
several modules. Modules have two parts: the public interface, which gives a user all
the information necessary to use the module; and the private section, which actually
does the work.

Functions; the basic building block in a C program is the function. In general,


functions are blocks of code that perform a number of pre-defined commands to
accomplish something productive. It must have a name and it is reusable i.e., it can
be executed from as many different parts in a C Program as required. Information
passed to the function is called arguments and is specified when the function is called.
And the function either returns some value to the point it was called from or returns
nothing.

Function: a sub-program that may include one or more statements designed to


perform a specific task. Every C Program will have one or more functions and there is
one mandatory function which is called main() function. This function is prefixed with
keyword int which means this function returns an integer value when it exits. This
integer value is returned using return statement. Structure of a Function There are
two main parts of the function. The function header and the function body. int sum (int
x, int y) {int ans = 0; //holds the answer that will be returned ans = x + y; //calculate
the sum return ans //return the answer}

Function Header It is the first line of a function, example; int sum (int x, int y). It has
three main parts
The name of the function i.e., sum
The parameters of the function enclosed in paranthesis
Return value type i.e., int

48
Function Body Whatever is written with in {} in the above example is the body of the
function.
main() Function Section
The main() function is a special function used by C system to tell the computer where
the program starts. Every program must have exactly one main function. The main
function is the point by where all C programs start their execution, independently of
its location within the source code. It does not matter whether there are other functions
with other names defined before or after it - the instructions contained within this
function's definition will always be the first ones to be executed in any C program. For
that same reason, it is essential that all C programs have a main function. The main
function section has the following sections
Declaration part: where all variables used in the executable section are
declared.
Executable part: consist of the statements to be executed. { Declaration part
Executable part } There must be a least one statement in the executable part.
The two part must be included between the opening {, and the closing }. Program
execution begins at the opening {and closes at the closing }, which signifies the logical
end of the program. All the statements between the { and } forms the function body
and are the instructions to perform the given task. All statements in the Declaration
and Executable parts must end with a semicolon (;) Formats of main()
 main()

 int main()

 void main()

 main(void)

 int main(void)

( ) or word void means the function has no arguments or parameter and thus does not
return any information to the operating system. int means the function return an
integer value to the operating system

49
Note the followings
 C is a case sensitive programming language. It means in C printf and Printf will
have different meanings.
 C has a free-form line structure. End of each C statement must be marked with
a semicolon.
 Multiple statements can be done on the same line.
 White Spaces (ie tab space and space bar) are ignored.
 Statements can continue over multiple lines.
 Printf is a predefined C function for printing out put
 Everything between the starting and ending quotation marks “ ” to be printed.
 To print on separate line; Use the command \n
 int = integer data type
 float = floating point number data type
 %d = formatting command that prints the output as a decimal integer
 %5.2f” = formatting command that prints the output as a floating-point integer
with five places in all and two places to the right of the decimal point.

Input/output statements
scanf() function: The scanf() function scans the data input through the keyboard and
by default delimits values by whitespace. Whitespace is defined as being a TAB, a
blank or the newline character ('\n'). This is the function which can be used to read an
input from the command line.
printf() function:Output functions or streams are used are to display different type of
data (Such as numeric, string or character) and results on the screen. You can use
more than one types of output functions in the program.

First program
#include <stdio.h>
main()
{

50
/* My first program */
printf("Hello World! \n");
}
The C program starting point is identified by the word main().
• This informs the computer as to where the program actually starts. The parentheses
that follow the keyword main indicate that there are no arguments supplied to this
program (this will be examined later on).
• The two braces, { and }, signify the begin and end segments of the program. In
general, braces are used throughout C to enclose a block of statements to be treated
as a unit.
#include <stdio.h>
main()
{
/* My first program */
printf("Hello World! \n");
return 0;
}
The purpose of the statement #include <stdio.h> is to allow the use of the printf
statement to provide program output. For each function built into the language, an
associated header file must be included. Text to be displayed by printf() must be
enclosed in double quotes. The program only has the one printf() statement.

printf() is actually a function (procedure) in C that is used for printing variables and
text. Where text appears in double quotes "", it is printed without modification.
There are some exceptions however. This has to do with the \ and % characters.
These characters are modifiers, and for the present the \ followed by the n character
represents a newline character.

Comments
Comments are like helping text in your C program and they are ignored by the
compiler. They start with /* and terminate with the characters */ or two double

51
forward slashes (//). /*…. */ delimiters are used for multiple lines while // delimiters
are used on single lines e.g.,
/* My first program */
Comments can be inserted into C programs by bracketing text with the /* and */
delimiters. Comments are useful for a variety of reasons. Primarily they serve as
internal documentation for program structure and functionality.

Whitespace
A line containing only whitespace, possibly with a comment, is known as a blank line,
and a C compiler totally ignores it. Whitespace is the term used in C to describe
blanks, tabs, newline characters and comments. Whitespace separates one part of a
statement from another and enables the compiler to identify where one element in a
statement, such as int, ends and the next element begins. Therefore, in the following
statement:

int age;

there must be at least one whitespace character (usually a space) between int and age
for the compiler to be able to distinguish them. On the other hand, in the following
statement:

fruit = apples + oranges; // get the total fruit

no whitespace characters are necessary between fruit and =, or between = and apples,
although you are free to include some if you wish to increase readability.

Header files contain definitions of functions and variables which can be


incorporated into any C program by using the pre-processor #include statement.
Standard header files are provided with each compiler, and cover a range of areas:
string handling, mathematics, data conversion, printing and reading of variables,
etc.

52
To use any of the standard functions, the appropriate header file should be included.
This is done at the beginning of the C source file. For example, to use the function
printf() in a program, the line #include <stdio.h>. It should be at the beginning of
the source file, because the declaration for printf() is found in the file stdio.h. All
header files have the extension .h and generally reside in the /usr/include
subdirectory.
#include <string.h>
#include <math.h>
#include "mylib.h"

The use of angle brackets <> informs the compiler to search the compiler’s include
directories for the specified file. The use of the double quotes "" around the
filename informs the compiler to start the search in the current directory for the
specified file.

C TOKENS

A C program consists of various tokens and a token is either a keyword, an


identifier, a constant, a string literal, or a symbol. In a C program the smallest
individual units are known as C Tokens. For example, the following C statement
consists of six tokens:

53
Keywords/reserved words

Reserved words (occasionally called keywords) are one type of grammatical


construct in programming languages. These words have special meaning within the
language and are predefined in the language’s formal specifications.

Every C program a word is classified as either a keyword or identifier. All keywords


have the fixed meaning and these meaning cannot be changed and acts as the
building blocks for program statements. They identify language entities that have
special meanings to the compiler. C reserved words must be typed fully in lowercase.
List of C keywords are listed below.

Identifiers
Identifiers refer to the names of variables, functions ad arrays. These are user defined
names and consists of a sequence of letters and digits, with a letter as a first character.

A C identifier is a name used to identify a variable, function, or any other user-defined


item. This are programmer-defined words. They are needed for program variables,
functions, and other program constructs. Must be unique within the same scope.
Reserved words cannot be used as identifiers. Identifiers are case sensitive.

Rules for Identifiers


1. First letter must be an alphabet (or underscore).

54
2. Must consist of only letters, digits or underscore
3. Only first 31 characters are significant
4. Cannot be a Keyword.
5. Must not contain white spaces

Data types

Data types refer to an extensive system used for declaring variables or functions of
different types. The type of a variable determines how much space it occupies in
storage and how the bit pattern stored is interpreted. The types in C can be classified
as follows:

1. Integer: These are whole numbers, both positive and negative. Unsigned
integers (positive values only) are also supported. In addition, there are short
and long integers. These specialized integer types will be discussed later.

The keyword used to define integers is int

An example of an integer value is 32. An example of declaring an integer


variable called age is

int age;

2. Floating point: These are numbers which contain fractional parts, both
positive and negative, and can be written in scientific notation.
 The keyword used to define float variables is float
 Typical floating-point values are 1.73 and 1.932e5 (1.932 x 105). An
example of declaring a float variable called x is

float x;

3. Double: These are floating point numbers, both positive and negative, which
have a higher precision than float variables.

55
 The keyword used to define double variables is double
 An example of declaring a double variable called voltage is double
voltage;

4. Character: These are single characters. The keyword used to define


character variables is char

 Typical character values might be the letter A, the character 5, the


symbol “, etc. An example of declaring a character variable called
letter is char letter;

5. Void: Void data type specifies that no value is available, and usually used to
specify the void type of function which does not return any value to the calling
function example main(void)

Variables

A variable is a named memory location in which data of a certain type can be


stored. The contents of a variable can change, thus the name. User defined
variables must be declared before they can be used in a program. It is during the
declaration phase that the actual memory for the variable is reserved. All variables
in C must be declared before use.

The name of a variable can be composed of letters, digits, and the underscore
character. It must begin with either a letter or an underscore. Upper and lowercase
letters are distinct because C is case-sensitive.

Variable to be used in a C program need to be declared to the C compiler. Declaration


of variables does the following two things; it tells the compiler what the variable name
is and it specifies what type of data the variable will hold. A variable must be declared
before it is used in a C program. A variable can be used to store a value of any data
type.

56
A variable definition means to tell the compiler where and how much to create the
storage for the variable. A variable definition/declaration specifies a data type
and contains a list of one or more variables of that type as follows:
data_type variable_name;
Here, data type must be a valid C data type including char, int, float, double, etc.,
and variable_list may consist of one or more identifier names separated by
commas. Some valid declarations are shown here:

int i, j, k;
char c, ch;
float f, salary;
double d;

The line int i, j, k; both declares and defines the variables i, j and k; which instructs
the compiler to create variables named i, j and k of type int.
Variables can be initialized (assigned an initial value) in their declaration. The
initializer consists of an equal sign followed by a constant expression as follows:

type variable_name = value;

Some examples are:

int d = 3, f = 5; // declaration of d and f.


int d = 3, f = 5; // definition and initializing d and f.
byte z = 22; // definition and initializes z.
char x = 'x'; // the variable x has the value 'x'.

A variable declaration provides assurance to the compiler that there is one variable
existing with the given type and name so that compiler proceed for further
compilation without needing complete detail about the variable. A variable
declaration has its meaning at the time of compilation only, compiler needs actual
variable declaration at the time of linking of the program.

57
The declaration of variables is done after the opening brace of main().

main() {
int sum;

Constants

Constants in C Program refers to fixed values that do not change during the
execution of the program.

Backslash Character constants

C supports some special backslash constants are used in output functions Example
„\n‟ stands for new line. Through having two characters that represent one
character, these combinations are known as escape sequences. Table below gives a
list of the C backslash constants

scanf() formats

Commonly used Scanf() formats

%s Read a string

%d Read an integer

%f Read a floating-point value

%c Read a single character

58
%i Read a decimal, hexadecimal or
octal integer

Enhancing readability of Output.

 Provide enough blank spaces between two numbers.


 Introduce appropriate headings and variable names in the output
 Print special messages whenever a peculiar condition occurs in the output.
 Introduce blank lines between the important sections of the output.

OPERATORS AND EXPRESSIONS

Arithmetic Operators
C provides all the basic arithmetic operators listed in table below e.g., given A is 10
and B is 20

Basic arithmetic operators


Integer division truncates any fractional part. The modulo division operation produces
the remainder of an integer division. Example

𝒂 − 𝒃, 𝒂 + 𝒃, 𝒂 ∗ 𝒃, 𝒂/𝒃, 𝒂%𝒃 − 𝒂 ∗ 𝒃

59
Here a and b are variables and are known as operands. Operands is a term used to
describe any object that is capable of being manipulated; They are usually constant
values or the names of variables.

Relational Operators
We often compare two quantities and depending on their relation make certain
decision. The comparison is done using relational operator. C supports six
relational operators shown in table

C logical operators

C supports the following three logical operators

a) && meaning logical AND


b) || meaning logical OR
c) ! meaning logical NOT

60
The logical operators && and || used when testing more than one condition and
make a decision. if mark >= 80 && mark < 90. An expression combining two or
more relational expressions is called logical expression or compound relational
expression and yields the value of either 1 or 0 / true or false.

Assignment Operators

There are following assignment operators supported by C language:

Operator precedence

Operator precedence is used to determine how an expression involving more than


one operator is evaluated. Most programming language use PEMDAS (Parenthesis,
Exponential, Multiplication, Division, Addition and Subtraction) in that order when
evaluating an expression. To control order of expression evaluation, parenthesis
(brackets) is used.

61
CONTROL STRUCTURES

Introduction

A control structure is simply a pattern for controlling the flow of a program module.
The three fundamental control structures of a structured programming language are
sequence, selection, and iteration. One of the most important aspects of programming
is controlling which statement will execute next. Control structures/Control statements
enable a programmer to determine the order in which program statements are
executed. These control structures allow you to do two things:

1) Skip some statements while executing others, and


2) Repeat one or more statements while some condition is true.

Sequential Program Control

By sequential we mean "in sequence," one-after-the-other. Sequential logic is the


easiest to construct and follow. Essentially you place each statement in the order that
you want them to be executed and the program executes them in sequence from the
Start statement to the End statement. As you can see by the example program to the
right, the arrows linking the statements depict the execution flow. If your program

62
included 20 basic commands, then it would execute those 20 statements in order and
then quit.

When you are solving a problem as a programmer, you must determine what
statements are needed to create a solution to the problem and the order in which those
statements must be executed. Writing the correct statements is one task.

Sequential control is the "default" control in the sense that every statement
automatically points to the next statement in the flowchart diagram. However, using
sequential control alone will not allow the development of solutions for most real-
world problems. Most real-world problems include "conditions" that determine what
should be done next. The "condition" determines whether the action should be
executed or not executed.

Selection Control/ Decision

It is common that you will need to make a decision about some condition of your
program's data to determine whether certain statements should be executed.

63
A selection-control statement allows you to make "decisions" in your code about the
current state of your program's data and then to take one of two alternative paths to a
"next" statement.

All decisions are stated as "yes/no" questions. When a program is executed, if the
answer to a decision is "yes" (or true), then the left branch of control is taken. If the
answer is "no" (or false), then the right branch of control is taken. In the example to
the right, either statement 2a or statement 2b will be executed, but never both.
Selection is implemented by using if, if…else, elseif and switch statements.

In C, there are three decision making statements.

 if execute a statement or not


 if-else choose to execute one of two statements
 switch/if-elseif choose to execute one of a number of statements

The if statement

The if statement allows branching (decision making) depending upon a condition.


Program code is executed or skipped. The basic syntax is:

if (control expression)
{
program statement;
/* statement(s) will execute if the expression is true */
}

If the control expression is TRUE, the body of the if is executed. If it is FALSE, the body
of the if is skipped.

The if-else Statement


Used to decide between two courses of action. The syntax of the if-else statement is:

64
if (expression)

{
statement1; /* statement(s) will execute if the expression is true */
}
else
{
statement2; /* statement(s) will execute if the expression is false */
}
If the expression is TRUE, statement1 is executed; statement2 is skipped.
If the expression is FALSE, statement2 is executed; statement1 is skipped.

The if…elseif statement


An if statement can be followed by an optional else if...else statement, which is very
useful to test various conditions using single if...else if statement. When using if, else
if, else statements there are few points to keep in mind:
- An if can have zero or one else's and it must come after any else if's.
- An if can have zero to many else if's and they must come before the else.

65
Once an else if succeeds, none of the remaining else if's or else's will be tested.
The syntax of an if...else if...else statement in C programming language is:
if(expression 1)
{
statement 1; /* Executes when the expression 1 is true */
}
else if( expression 2)
{
statement 2; /* Executes when the expression 2 is true */
}
else if( expression 3)
{
statement 3; /* Executes when the expression 3 is true */
}
else
{
statement 4; /* executes when the none of the above condition is true */
}

As soon as a TRUE control expression is found, the statement associated with it is


executed and the rest of the ladder is bypassed. If no control expressions are found
to be TRUE, the final else statement acts as a default.

The switch statement

The switch statement is a better way of writing a program which employs an if-else
ladder. It is C’s built-in multiple branch decision statement. A switch statement allows
a variable to be tested for equality against a list of values. Each value is called a case,
and the variable being switched on is checked for each switch case. The syntax for a
switch statement in C programming language is as follows:

66
switch(expression)
{
case constant-expression:
statement(s);
break; /* optional */
case constant-expression:
statement(s);
break; /* optional */
/* you can have any number of case statements */
default: /* Optional */
statement(s);
}

The keyword break should be included at the end of each case statement. In general,
whenever a break statement is encountered in C, it interrupts the normal flow of
control. In the switch statement, it causes an exit from the switch shunt. The default
clause is optional. The right brace at the end marks the end of switch statement.

67
Loop (Iteration) Control

A Loop (iteration) control statement allows you to repeat one or more statements until
some condition becomes true. This type of control statement is what makes computers
so valuable. A computer can repeatedly execute the same instructions over-and-over
again without getting bored with the repetition.

One ellipse and one diamond symbol are used to represent a loop. The number of
times that the loop is executed is controlled by the decision expression that is entered
into the diamond symbol. During execution, when the diamond symbol is executed,
if the decision expression evaluates to "no," then the "no" branch is taken, which
always leads back to the Loop statement and repetition. The statements to be repeated
can be placed above or below the decision diamond.

 Iteration is implemented by using the while, do/while, and for statements.

Loop Type Description


Repeats a statement or group of statements while a
while loop given condition is true. It tests the condition before
executing the loop body.
Executes a sequence of statements multiple times and
for loop
abbreviates the code that manages the loop variable.
It is more like a while statement, except that it tests
do...while loop
the condition at the end of the loop body.

68
You can use one or more loops inside any other while,
nested loops
for, or do..while loop.

while Loop

A while loop in C programming repeatedly executes a target statement as long as a


given condition is true.

Syntax

The syntax of a while loop in C programming language is:

while(condition)
{
statement(s);
}

Here, statement(s) may be a single statement or a block of statements. The condition


may be any expression, and true is any nonzero value. The loop iterates while the
condition is true. When the condition becomes false, the program control passes to
the line immediately following the loop.

Flow Diagram

Here, the key point to note is that a while loop might not execute at all. When the
condition is tested and the result is false, the loop body will be skipped and the first
statement after the while loop will be executed.

69
For Loop
A for loop is a repetition control structure that allows you to efficiently write a loop that
needs to execute a specific number of times.
Syntax
The syntax of a for loop in C programming language is:
for (initialization; test-condition; increment)
{
body of the loop
}
Note the three sections in the for () statement must be separated by semicolons (;).
There is no (;) after the increment section (x++)

The execution of the for statement is as follows;


1. Initialization of the control variables is done first using assignment statements
such as i =1 and count = 0;. The i and count are called the loop control variables.
This step allows you to declare and initialize any loop control variables. You are
not required to put a statement here, as long as a semicolon appears.

70
2. The value of the control variable is then tested using the test-condition. The test-
condition is a relational expression such as i<10 that determines when the loop
will exit. If the condition is true, the body of the loop is executed; otherwise, the
loop is terminated and the execution continues with the statement that
immediately follows the loop.
3. When the body of the loop is executed, the control is transferred back to the
for statement after evaluating the last statement in the loop. The control is
incremented using an assignment statement such as i = i + 1 (i++) and the new
value of the control variable is again tested to see whether it satisfies the loop
condition.
4. The condition is now evaluated again. If it is true, the loop executes and the
process repeats itself (body of loop, then increment step, and then again
condition). After the condition becomes false, the ‘for’ loop terminates.
The Flow diagram

Initialization

Conditio
n

If condition is
true

Code block
If condition is
false
Increment

71
do...while loop
Unlike for and while loops, which test the loop condition at the top of the loop, the
do...while loop in C programming language checks its condition at the bottom of the
loop.

A do...while loop is similar to a while loop, except that a do...while loop is


guaranteed to execute at least one time. Do…while is used when the body of the
loop needs to be executed first before evaluating the test-condition.
Syntax
The syntax of a do...while loop in C programming language is:

do
{
body of the loop / statements;
}
while (test-condition);
Notice that the conditional expression appears at the end of the loop, so the
statement(s) in the loop execute once before the condition is tested.

On reaching the do statement, the program proceeds to evaluate the body of the loop
first, and at the end of the loop the test-condition in the while statement is evaluated.
If the test-condition is true the programs evaluate the body of loop and this process is
repeated until the test-condition is false. If the test-condition is false the loop is
terminated and the control goes to the statement that appears immediately after the
while statement.

Since the test-condition is evaluated at the bottom of the loop, the do…. while
construct provides an exit–controlled loop and thus the body of the loop is always
executed at least once.

72
Loop Control Statements

Loop control statements change execution from its normal sequence. When
execution leaves a scope, all automatic objects that were created in that scope are
destroyed. C supports the following control statements.
Control Statement Description
Terminates the loop or switch statement and transfers
execution to the statement immediately following the
break statement
loop or switch.

Causes the loop to skip the remainder of its body and


continue statement
immediately retest its condition prior to reiterating.
goto statement Transfers control to the labeled statement.

break Statement
The break statement in C programming has the following two usages:

73
 When a break statement is encountered inside a loop, the loop is immediately
terminated and the program control resumes at the next statement following
the loop.
 It can be used to terminate a case in the switch statement (covered in the next
chapter).

If you are using nested loops, the break statement will stop the execution of the
innermost loop and start executing the next line of code after the block.
Syntax
break;

continue Statement
The continue statement in C programming works somewhat like the break
statement. Instead of forcing termination, it forces the next iteration of the loop to
take place, skipping any code in between.

For the for loop, continue statement causes the conditional test and increment
portions of the loop to execute. For the while and do...while loops, continue
statement causes the program control to pass to the conditional tests.
Syntax
continue;

goto Statement
A goto statement in C programming provides an unconditional jump from the ‘goto’
to a labeled statement in the same function.

NOTE: Use of goto statement is highly discouraged in any programming language


because it makes difficult to trace the control flow of a program, making the
program hard to understand and hard to modify. Any program that uses a goto can
be rewritten to avoid them.
Syntax

74
goto label;
..
.
label: statement;
Here label can be any plain text except C keyword and it can be set anywhere in
the C program above or below to goto statement.

Functions in C

A function is a group of statements that together perform a task. Every C program has
at least one function, which is main(), and all the most trivial programs can define
additional functions.

You can divide up your code into separate functions. How you divide up your code
among different functions is up to you, but logically the division is such that each
function performs a specific task.

A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.

The C standard library provides numerous built-in functions that your program can
call. For example, strcat() to concatenate two strings, memcpy() to copy one memory
location to another location, and many more functions.

A function can also be referred as a method or a sub-routine or a procedure, etc.

Large programs are divided in functional parts, and each of the functional part is code
separately and independently but later combined into a single unit.

The independently coded programs are called sub-programs, and in C they are
referred to as “functions”. The functions can be called and used whenever needed.

75
Advantages of Using Functions

 The length of a source program can be reduced by using functions at


appropriate places.
 It is easy to locate and isolate a faulty function for further investigations.
 A function may be used by many other programs.

Types of Functions

There are two types of functions:

- Pre-defined (inbuilt) functions


- User-defined functions

1 Pre-defined Functions
 Pre-defined Functions were created by the programming language developers
to assist programmers e.g. C programming has the following:

- Input/output functions

- String functions

- Math functions

 Input/Output Functions

Input/Output files/libraries containing I/O functions include:

- stdio.h – consisting of printf () and scanf() functions.

- conio.h- Stands for CONsole Input Output. Consisting of the following functions:
clrscr(), delline(), getch(),getche(), gotoxy(), textcolor(), textbackground()

 String Functions

76
The string functions are stored in the <string.h> header file

Function Action
- Strcat() Concatenates two string
- Strcmp() Compares two strings
- Strlen() Finds the length of a string
- Strupr() Converts to upper case
- Strlwr() Converts to lower case
 Math Functions

Use math.h file.

- sqrt
- round
- truc
- sin
- tan
- cos

2 User-defined Function

It is a function developed by the programmer using the program. A user-defined


functions consists of these elements;

a. Function Definition: Write the function.


b. Function Call: The function is invoked or called by the main program.
Defining a Function

A function definition has the following elements;

1. Function name
2. Function return type
3. List of parameters.

77
4. Local variable declarations
5. Function body statements
6. A return statement.

 All the six elements are grouped together into two parts, namely; function
header and function body.

The general format of a function definition in C programming language is as follows:

return_type function_name ( parameter list )


{
body of the function;
}

A function definition in C programming consists of a function header and a function


body. The functional header consists of the first three (3) elements: function type (also
called return type); function name and the formal parameter list. No semi colon is used
at the end of the function header section.

Here are all the parts of a function −

 Return Type − specifies the type of value (int, float, void) that the function is
expected to return to the calling function. The return_type is the data type of
the value the function returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is the keyword void.
 Function Name − This is the actual name of the function. The function name
and the parameter list together constitute the function signature. It is any valid
C identifier and thus must follow the same rules of formulation as other variable
names in C.
 Parameters − A parameter is like a placeholder. Parameter list declares the
variables that will receive data sent by the calling program. They serve as input
data to the function to carry out the specified task. When a function is invoked,

78
you pass a value to the parameter. This value is referred to as actual parameter
or argument. The parameter list refers to the type, order, and number of the
parameters of a function. Parameters are optional; that is, a function may
contain no parameters.

 Function Body − The function body contains a collection of statements that


define what the function does. It contains the declaration and statements
necessary for performing the required task. It is enclosed on braces { } and
contain three parts;

 Local declarations that specify the variables needed by the function


 Function statements that perform the task of the function
 Return statement that returns the value evaluated by the function.

Example

Given below is the source code for a function called max(). This function takes two
parameters num1 and num2 and returns the maximum value between the two −

int max(int num1, int num2) {


int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}

Function Declarations

A function declaration tells the compiler about a function name and how to call the
function. The actual body of the function can be defined separately.

79
A function declaration has the following parts −

- return_type function_name( parameter list );

For the above defined function max(), the function declaration is as follows −

- int max(int num1, int num2);

Parameter names are not important in function declaration only their type is required,
so the following is also a valid declaration −

- int max(int, int);

Function declaration is required when you define a function in one source file and you
call that function in another file. In such case, you should declare the function at the
top of the file calling the function.

Calling a Function

While creating a C function, you give a definition of what the function has to do. To use
a function, you will have to call that function to perform the defined task.

When a program calls a function, the program control is transferred to the called
function. A called function performs a defined task and when its return statement is
executed or when its function-ending closing brace is reached, it returns the program
control back to the main program.

To call a function, you simply need to pass the required parameters along with the
function name, and if the function returns a value, then you can store the returned
value. For example –

#include <stdio.h>
/* function returning the max between two numbers */
int max(int num1, int num2) /* function declaration */

80
{ /* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result; }
int main () {
/* local variable definition */
int a = 100;
int b = 200;
int answer;
/* calling a function to get max value */
answer = max(a, b);
printf( "Max value is : %d\n", ret );
return 0;
}

We have kept max () along with main () and compiled the source code. While running
the final executable, it would produce the following result −

Max value is: 200

Function Arguments

If a function is to use arguments, it must declare variables that accept the values of the
arguments. These variables are called the formal parameters of the function.

Formal parameters behave like other local variables inside the function and are
created upon entry into the function and destroyed upon exit.

81
While calling a function, there are two ways in which arguments can be passed to a
function −

Call by value; This method copies the actual value of an argument into the formal
parameter of the function. In this case, changes made to the parameter inside the
function have no effect on the argument.
Call by reference; This method copies the address of an argument into the formal
parameter. Inside the function, the address is used to access the actual argument used
in the call. This means that changes made to the parameter affect the argument.

By default, C uses call by value to pass arguments. In general, it means the code
within a function cannot alter the arguments used to call the function.

Scope Rules

scope in any programming is a region of the program where a defined variable can
have its existence and beyond that variable it cannot be accessed. There are three
places where variables can be declared in C programming language −

 Inside a function or a block which is called local variables.


 Outside of all functions which is called global variables.
 In the definition of function parameters which are called formal parameters.

Let us understand what are local and global variables, and formal parameters.

Local Variables

Variables that are declared inside a function or block are called local variables. They
can be used only by statements that are inside that function or block of code. Local
variables are not known to functions outside their own. The following example shows
how local variables are used. Here all the variables a, b, and c are local to main()
function.

82
#include <stdio.h>
int main () {
/* local variable declaration */
int a, b; int c;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf ("value of a = %d, b = %d and c = %d\n", a, b, c);
return 0;
}

Global Variables

Global variables are defined outside a function, usually on top of the program. Global
variables hold their values throughout the lifetime of your program and they can be
accessed inside any of the functions defined for the program.

A global variable can be accessed by any function. That is, a global variable is
available for use throughout your entire program after its declaration. The following
program show how global variables are used in a program.

#include <stdio.h>
/* global variable declaration */
int g;
int main () {
/* local variable declaration */
int a, b;
/* actual initialization */
a = 10;
b = 20;

83
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0; }

A program can have same name for local and global variables but the value of local
variable inside a function will take preference. Here is an example −

#include <stdio.h>
int g = 20; /* global variable declaration */
int main () {
int g = 10; /* local variable declaration */
printf ("value of g = %d\n”, g);
return 0; }

When the above code is compiled and executed, it produces the following result −

value of g = 10

Function Parameters

Formal Parameters; Arguments which are mentioned in the definition of the function
is called formal arguments. They are local variables which are assigned values from
the arguments when the function is called. Formal parameters, are treated as local
variables within a function and they take precedence over global variables.

Actual Parameters or Arguments; When a function is called, the values


(expressions) that are passed in the call are called the arguments or actual parameters
(both terms mean the same thing). At the time of the call each actual parameter is
assigned to the corresponding formal parameter in the function definition.
For value parameters (the default), the value of the actual parameter is assigned to the
formal parameter variable. For reference parameters, the memory address of the
actual parameter is assigned to the formal parameter.

84
Introduction to Pointers

Pointers are variables that hold address of another variable of same data type.

Concept of Pointer

Whenever a variable is declared, system will allocate a location to that variable in


the memory, to hold value. This location will have its own address number.

Let us assume that system has allocated memory location 80F for a variable a.

int a = 10;

We can access the value 10 by either using the variable name a or the address 80F.
Since the memory addresses are simply numbers they can be assigned to some
other variable. The variable that holds memory address is called pointer variables.
A pointer variable is therefore nothing but a variable that contains an address,
which is a location of another variable. Value of pointer variable will be stored in
another memory location.

Pointers contain memory addresses, NOT data values!

Declaring a pointer variable

85
General syntax of pointer declaration is,

data-type *pointer_name;

Data type of pointer must be same as the variable, which the pointer is pointing.
void type pointer works with all data types, but isn't used often.

Initialization of Pointer variable

Pointer Initialization is the process of assigning address of a variable to pointer


variable. Pointer variable contains address of variable of same data type. In C
language address operator & is used to determine the address of a variable. The &
(immediately preceding a variable name) returns the address of the variable
associated with it.

int a = 10 ;
int *ptr ; //pointer declaration
ptr = &a ; //pointer initialization

or,

int *ptr = &a ; //initialization and declaration together

Pointer variable always points to same type of data.

float a;
int *ptr;
ptr = &a; //ERROR, type mismatch

Declaring a pointer variable

General syntax of pointer declaration is,

86
data-type *pointer_name;

Data type of pointer must be same as the variable, which the pointer is pointing.
void type pointer works with all data types, but isn't used oftenly.

Initialization of Pointer variable

Pointer Initialization is the process of assigning address of a variable to pointer


variable. Pointer variable contains address of variable of same data type. In C
language address operator & is used to determine the address of a variable. The &
(immediately preceding a variable name) returns the address of the variable
associated with it.

int a = 10 ;
int *ptr ; //pointer declaration
ptr = &a ; //pointer initialization
or,
int *ptr = &a ; //initialization and declaration together

Pointer variable always points to same type of data.

float a;
int *ptr;
ptr = &a; //ERROR, type mismatch

Dereferencing of Pointer

Once a pointer has been assigned the address of a variable. To access the value of
variable, pointer is dereferenced, using the indirection operator *.

int a,*p;
a = 10;

87
p = &a;

printf("%d",*p); //this will print the value of a.

printf("%d",*&a); //this will also print the value of a.

printf("%u",&a); //this will print the address of a.

printf("%u",p); //this will also print the address of a.

printf("%u",&p); //this will also print the address of p.

The following simple program demonstrates the difference between the contents of
a variable and its memory address:
#include <stdio.h>
main() {
float x;
x=2.171828;
printf("The value of x is %f\n",x);
printf("The address of x is %X\n",&x); }

The result is as shown

The value of x is 2.171828


The address of x is
EFFFFBA4
Features/characteristics of pointers

 They contain memory addresses


 They always point to same type of data

88
 It is a derived data type [Link] refers to another variable by storing the
variables memory address.
 They can reference any data type, even functions
 It does not contain data values

Benefit/ Advantages of using pointers

 Pointers are more efficient in handling Array and data Structure.


 Pointer allows references to function and thereby helps in passing of function
as arguments to other function.
 It reduces length and complexity of programs.
 It allows C to support dynamic memory management.
 It increases the execution speed and thus reduce the program execution time

Drawbacks/disadvantages

 Uninitialized pointers might cause segmentation fault.


 Dynamically allocated block needs to be freed explicitly. Otherwise, it would
lead to memory leak.
 Pointers are slower than normal variables.
 If pointers are updated with incorrect values, it might lead to memory
corruption.

Data structures

Data structures are specialized formats for organizing, processing, retrieving, and storing
data in computer memory. They provide a way to manage and manipulate data efficiently.

Key Characteristics:
1. Organization: Data structures organize data in a particular layout or pattern,
enabling efficient access and manipulation.

89
2. Efficiency: They aim to optimize various operations such as insertion, deletion,
searching, and sorting to achieve the best performance.
3. Abstraction: Data structures abstract the underlying implementation details,
providing a clear interface for working with data.
4. Flexibility: Different data structures offer different trade-offs in terms of
efficiency, memory usage, and ease of use, allowing developers to choose the
most suitable one for their specific needs.

Basic types of Data Structures

As we discussed above, anything that can store data can be called as a data
structure, hence Integer, Float, Boolean, Char etc, all are data structures. They are
known as Primitive Data Structures.

Then we also have some complex Data Structures, which are used to store large and
connected data. Some example of Abstract Data Structure are:

 Linked List
 Tree
 Graph
 Stack, Queue etc.

All these data structures allow us to perform different operations on data. We select
these data structures based on which type of operation is required. We will look into
these data structures in more details in our later lessons.

90
1. Arrays
Arrays a kind of data structure that can store a fixed-size sequential collection of
elements of the same type. An array is used to store a collection of data, but it is
often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, ..., and


number99, you declare one array variable such as numbers and use numbers [0],
numbers [1], and ..., numbers [99] to represent individual variables.

A specific element in an array is accessed by an index. All arrays consist of


contiguous memory locations. The lowest address corresponds to the first element
and the highest address to the last element.

First element Last element

Numbers[0] Numbers[1] Numbers[2] ……

Declaring Arrays

To declare an array in C, a programmer specifies the type of the elements and the
number of elements required by an array as follows:

91
type arrayName [ arraySize ];

This is called a single-dimensional array. The arraySize must be an integer constant


greater than zero and the type can be any valid C data type. For example, to declare
a 10-element array called balance of type double, use this statement:

double balance [10];

Here, balance is a variable array that is sufficient to hold up to 10 double numbers.

Initializing Arrays

You can initialize an array in C either one by one or using a single statement as
follows:

double balance [5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

The number of values between braces { } cannot be larger than the number of
elements that we declare for the array between square brackets [ ].

If you omit the size of the array, an array just big enough to hold the initialization is
created. Therefore, if you write:

double balance [] = {1000.0, 2.0, 3.4, 7.0, 50.0};

You will create exactly the same array as you did in the previous example. Following
is an example to assign a single element of the array:

Balance [4] = 50.0;

The above statement assigns the 5th element in the array with a value of 50.0. All
arrays have 0 as the index of their first element which is also called the base index
and the last index of an array will be total size of the array minus 1. Shown below is
the pictorial representation of the array we discussed above:

0 1 2 3 4

Balance 1000.0 2.0 3.4 7.0 50.0

92
Accessing Array Elements
An element is accessed by indexing the array name. This is done by placing the
index of the element within square brackets after the name of the array. For
example:

double salary = balance [9];

The above statement will take the 10th element from the array and assign the value
to salary variable. The following example shows how to use all the three above-
mentioned concepts viz. declaration, assignment, and accessing arrays:

#include <stdio.h>
int main ()
{
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;

/* initialize elements of array n to 0 */

for ( i = 0; i < 10; i++ )


{
n[ i ] = i + 100; /* set element at location i to i + 100 */

/* output each array element's value */

for (j = 0; j < 10; j++ )


{
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}

When the above code is compiled and executed, it produces the following result:

93
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

Multidimensional Arrays
C programming language allows multidimensional arrays. Here is the general form
of a multidimensional array declaration:

type name[size1][size2]...[sizeN];

For example, the following declaration creates a three-dimensional integer array:

int threedim[5][10][4];

Two-dimensional Arrays
The simplest form of multidimensional array is the two-dimensional array. A two-
dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-
dimensional integer array of size [x][y], you would write something as follows:

type arrayName [x][y];

Where type can be any valid C data type and arrayName will be a valid C
identifier. A two-dimensional array can be considered as a table which will have x
number of rows and y number of columns. A two-dimensional array a, which
contains three rows and four columns can be shown as follows:

94
Thus, every element in the array a is identified by an element name of the form
a[ i ][ j ], where ‘a’ is the name of the array, and ‘i' and ‘j’ are the subscripts that
uniquely identify each element in ‘a’.

2. Stacks

Stack is an abstract data type with a bounded(predefined) capacity. It is a simple


data structure that allows adding and removing elements in a particular order. Every
time an element is added, it goes on the top of the stack, the only element that can
be removed is the element that was at the top of the stack, just like a pile of objects.

Basic features of Stack

1. Stack is an ordered list of similar data type.


2. Stack is a LIFO structure. (Last in First out).

95
3. push() function is used to insert new elements into the Stack and pop() is used
to delete an element from the stack. Both insertion and deletion are allowed at
only one end of Stack called Top.
4. Stack is said to be in Overflow state when it is completely full and is said to be
in Underflow state if it is completely empty.

3. Queues

Queue is also an abstract data type or a linear data structure, in which the first
element is inserted from one end called REAR(also called tail), and the deletion of
existing element takes place from the other end called as FRONT(also called head).
This makes queue as FIFO data structure, which means that element inserted first
will also be removed first.

The process to add an element into queue is called Enqueue and the process of
removal of an element from queue is called Dequeue.

Basic features of Queue

1. Like Stack, Queue is also an ordered list of elements of similar data types.

96
2. Queue is a FIFO( First in First Out ) structure.
3. Once a new element is inserted into the Queue, all the elements inserted
before the new element in the queue must be removed, to remove the new
element.
4. peek( ) function is often used to return the value of first element without
dequeuing it.

Applications of Queue

Queue, as the name suggests is used whenever we need to have any group of
objects in an order in which the first one coming in, also gets out first while the
others wait for their turn, like in the following scenarios :

1. Serving requests on a single shared resource, like a printer, CPU task


scheduling etc.
2. In real life, Call Center phone systems will use Queues, to hold people calling
them in an order, until a service representative is free.

4. Linked Lists

Linked List is a linear data structure and it is very common data structure which
consists of group of nodes in a sequence which is divided in two parts. Each node
consists of its own data and the address of the next node and forms a chain. Linked
Lists are used to create trees and graphs.

Advantages of Linked Lists

97
 They are a dynamic in nature which allocates the memory when required.
 Insertion and deletion operations can be easily implemented.
 Stacks and queues can be easily executed.
 Linked List reduces the access time.

Disadvantages of Linked Lists

 The memory is wasted as pointers require extra memory for storage.


 No element can be accessed randomly; it has to access each node
sequentially.
 Reverse Traversing is difficult in linked list.

5. Graphs

Graph is a nonlinear data structure, it contains a set of points known as nodes (or
vertices) and set of links known as edges (or Arcs) which connects the vertices. A
graph is defined as follows...
 Graph is a collection of vertices and arcs which connects vertices in the graph
 Graph is a collection of nodes and edges which connects nodes in the graph
A graph is a pictorial representation of a set of objects where some pairs of objects
are connected by links. The interconnected objects are represented by points
termed as vertices, and the links that connect the vertices are called edges.
Generally, a graph G is represented as G = (V, E), where V is set of vertices and E
is set of edges.

Example
The following is a graph with 5 vertices and 6 edges. This graph G can be defined as
G = (V , E ).
Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.

98
Graph Terminology
 Vertex − Each node of the graph is represented as a vertex. In the following
example, the labeled circle represents vertices. Thus, A to G are vertices. We
can represent them using an array as shown in the following image. Here A can
be identified by index 0. B can be identified using index 1 and so on.

 Edge − Edge represents a path between two vertices or a line between two
vertices. In the following example, the lines from A to B, B to C, and so on
represents edges. Edges are three types.
 Undirected Edge - An undirected edge is a bidirectional edge. If there
is an undirected edge between vertices A and B then edge (A, B) is
equal to edge (B, A).
 Directed Edge - A directed edge is a unidirectional edge. If there is a
directed edge between vertices A and B then edge (A, B) is not equal to
edge (B, A).
 Weighted Edge - A weighted edge is an edge with cost on it.
 Undirected Graph; A graph with only undirected edges is said to be
undirected graph.
 Directed Graph; A graph with only directed edges is said to be directed
graph.
 Mixed Graph; A graph with undirected and directed edges is said to be
mixed graph.

99
 Adjacency − Two node or vertices are adjacent if they are connected to each
other through an edge. In the following example, B is adjacent to A, C is adjacent
to B, and so on.
 Path − Path represents a sequence of edges between the two vertices. In the
following example, ABCD represents a path from A to D.

Graph Traversals
Graph traversal is technique used for searching a vertex in a graph. The graph
traversal is also used to decide the order of vertices to be visited in the search
process. A graph traversal finds the edges to be used in the search process without
creating loops that means using graph traversal we visit all vertices of graph without
getting into looping path. There are two graph traversal techniques and they are as
follows...
 DFS (Depth First Search)
 BFS (Breadth First Search)
DFS (Depth First Search)
DFS traversal of a graph, produces a spanning tree as final result. Spanning Tree is
a graph without any loops. Depth First Search (DFS) algorithm traverses a graph in a
depth-ward motion and uses a stack to remember to get the next vertex to start a

100
search, when a dead end occurs in any iteration. We use the following steps to
implement BFS traversal...

Step 1: Define a Stack of size total number of vertices in the graph.

Step 2: Select any vertex as starting point for traversal. Visit that vertex and
push it on to the Stack.

Step 3: Visit any one of the adjacent vertex of the vertex which is at top of the
stack which is not visited and push it on to the stack.

Step 4: Repeat step 3 until there are no new vertex to be visit from the vertex on
top of the stack.

Step 5: When there is no new vertex to be visit then use back tracking and pop
one vertex from the stack.

Step 6: Repeat steps 3, 4 and 5 until stack becomes Empty.

Step 7: When stack becomes Empty, then produce final spanning tree by
removing unused edges from the graph

Back tracking is coming back to the vertex from which we came to current vertex.

Example 1; consider the following graph to perform DFS traversal

101
102
103
104
105
Example 2

Step Traversal Description

1 Initialize the stack.

Mark S as visited and put it onto the


stack. Explore any unvisited adjacent
node from S. We have three nodes
2
and we can pick any of them. For this
example, we shall take the node in
an alphabetical order.

Mark A as visited and put it onto the


stack. Explore any unvisited adjacent
3 node from A. Both S and D are
adjacent to A but we are concerned
for unvisited nodes only.

106
Visit D and mark it as visited and put
onto the stack. Here, we have B and
C nodes, which are adjacent to D
4
and both are unvisited. However, we
shall again choose in an alphabetical
order.

We choose B, mark it as visited and


put onto the stack. Here B does not
5
have any unvisited adjacent node.
So, we pop B from the stack.

We check the stack top for return to


the previous node and check if it has
6
any unvisited nodes. Here, we find D
to be on the top of the stack.

Only unvisited adjacent node is from


7 D is C now. So we visit C, mark it as
visited and put it onto the stack.

107
As C does not have any unvisited adjacent node so we keep popping the stack until
we find a node that has an unvisited adjacent node. In this case, there's none and we
keep popping until the stack is empty.

BFS (Breadth First Search)


BFS traversal of a graph, produces a spanning tree as final result. We use Queue
data structure with maximum size of total number of vertices in the graph to
implement BFS traversal of a graph. Breadth First Search (BFS) algorithm traverses a
graph in a breadth-ward motion and uses a queue to remember to get the next
vertex to start a search, when a dead end occurs in any iteration
We use the following steps to implement BFS traversal...
Step 1: Define a Queue of size total number of vertices in the graph.
Step 2: Select any vertex as starting point for traversal. Visit that vertex and insert it
into the Queue.
Step 3: Visit all the adjacent vertices of the vertex which is at front of the Queue
which is not visited and insert them into the Queue.
Step 4: When there is no new vertex to be visit from the vertex at front of the Queue
then delete that vertex from the Queue.
Step 5: Repeat step 3 and 4 until queue becomes empty.
Step 6: When queue becomes Empty, then produce final spanning tree by removing
unused edges from the graph

Example 1

108
109
110
Example 2

At this stage, we are left with no unmarked (unvisited) nodes. But as per the
algorithm we keep on queuing in order to get all unvisited nodes. When the queue
gets emptied, the program is over.

Step Traversal Description


1 Initialize the queue.

2 We start from
visiting S (starting
node), and mark it
as visited.

111
3 We then see an
unvisited adjacent
node from S. In this
example, we have
three nodes but
alphabetically we
choose A, mark it as
visited and enqueue
it.
4 Next, the unvisited
adjacent node from
S is B. We mark it as
visited and enqueue
it.

5 Next, the unvisited


adjacent node from
S is C. We mark it as
visited and enqueue
it.

6 Now, S is left with no


unvisited adjacent
nodes. So, we
dequeue and find A.

112
7 From A we have D
as unvisited
adjacent node. We
mark it as visited
and enqueue it.

6. Tree Data Structures

In linear data structure, data is organized in sequential order and in non-linear data
structure, data is organized in random order. Tree is a very popular data structure
used in wide range of applications. A tree data structure can be defined as follows...

 Tree is a non-linear data structure which organizes data in hierarchical


structure and this is a recursive definition.
 Tree data structure is a collection of data (Node) which is organized in
hierarchical structure and this is a recursive definition

A tree is a widely used abstract data type (ADT)—or data structure implementing
this ADT—that simulates a hierarchical tree structure, with a root value and subtrees
of children with a parent node, represented as a set of linked nodes. In tree data
structure, every individual element is called as Node. Node in a tree data structure,
stores the actual data of that particular element and link to next element in
hierarchical structure.

113
Example

Terminology
 Root: In a tree data structure, the first node is called as Root Node. Every tree
must have root node. The root node is the origin of tree data structure. In any
tree, there must be only one root node. A tree can never have multiple root
nodes in a tree.

 Edge; In a tree data structure, the connecting link between any two nodes is
called as EDGE. In a tree with 'N' number of nodes there will be a maximum of
'N-1' number of edges.

 Parent; In a tree data structure, the node which is predecessor of any node is
called as PARENT NODE. In simple words, the node which has branch from it

114
to any other node is called as parent node. Parent node can also be defined as
"The node which has child / children".

 Child; In a tree data structure, the node which is descendant of any node is
called as CHILD Node. In simple words, the node which has a link from its
parent node is called as child node. In a tree, any parent node can have any
number of child nodes. In a tree, all the nodes except root are child nodes.

 Siblings; In a tree data structure, nodes which belong to same Parent are
called as SIBLINGS. In simple words, the nodes with same parent are called as
Sibling nodes.

 Leaf; In a tree data structure, the node which does not have a child is called
as LEAF Node. In simple words, a leaf is a node with no child. In a tree data

115
structure, the leaf nodes are also called as External Nodes. External node is
also a node with no child. In a tree, leaf node is also called as 'Terminal' node.

 Internal Nodes; In a tree data structure, the node which has at least one child
is called as INTERNAL Node. In simple words, an internal node is a node with
at least one child. Nodes other than leaf nodes are called as Internal Nodes.
The root node is also said to be Internal Node if the tree has more than one
node. Internal nodes are also called as 'Non-Terminal' nodes.

 Degree; In a tree data structure, the total number of children of a node is


called as DEGREE of that Node. In simple words, the Degree of a node is total
number of children it has. The highest degree of a node among all the nodes
in a tree is called as 'Degree of Tree'

116
 Level; In a tree data structure, the root node is said to be at Level 0 and the
children of root node are at Level 1 and the children of the nodes which are at
Level 1 will be at Level 2 and so on... In simple words, in a tree each step from
top to bottom is called as a Level and the Level count starts with '0' and
incremented by one at each level (Step).

 Height; In a tree data structure, the total number of edges from leaf node to a
particular node in the longest path is called as HEIGHT of that Node. In a tree,
height of the root node is said to be height of the tree. In a tree, height of all
leaf nodes is '0'.

 Depth; In a tree data structure, the total number of edges from root node to a
particular node is called as DEPTH of that Node. The total number of edges
from root node to a leaf node in the longest path is said to be Depth of the
tree. In simple words, the highest depth of any leaf node in a tree is said to be
depth of that tree. In a tree, depth of the root node is '0'.

117
 Path; In a tree data structure, the sequence of Nodes and Edges from one
node to another node is called as PATH between that two Nodes. Length of a
Path is total number of nodes in that path. In below example the path A - B - E -
J has length 4.

 Sub Tree; In a tree data structure, each child from a node forms a
subtree recursively. Every child node will form a subtree on its parent
node.

Why Trees?

118
 One reason to use trees might be because you want to store information that
naturally forms a hierarchy.
 Trees provide moderate access/search (quicker than Linked List and slower
than arrays).
 Trees provide moderate insertion/deletion (quicker than Arrays and slower
than Unordered Linked Lists).
 Like Linked Lists and unlike Arrays, Trees don’t have an upper limit on
number of nodes as nodes are linked using pointers.

Binary Tree
In a normal tree, every node can have any number of children. Binary tree is a
special type of tree data structure in which every node can have a maximum of 2
children. One is known as left child and the other is known as right child. A tree in
which every node can have a maximum of two children is called as Binary Tree. In a
binary tree, every node can have either 0 children or 1 child or 2 children but not
more than 2 children.
Binary Tree Traversals
When we wanted to display a binary tree, we need to follow some order in which all
the nodes of that binary tree must be displayed. In any binary tree displaying order
of nodes depends on the traversal method.
Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree
Traversal.
There are three types of binary tree traversals.
1) In - Order Traversal
2) Pre - Order Traversal
3) Post - Order Traversal
In-order Traversal; In this traversal method, the left subtree is visited first, then the
root and later the right sub-tree. We should always remember that every node may
represent a subtree itself. If a binary tree is traversed in-order, the output will
produce sorted key values in an ascending order.

119
We start from A, and following in-order traversal, we
move to its left subtree B. B is also traversed in-order. The
process goes on until all the nodes are visited. The output
of in order traversal of this tree will be −

D→B→E→A→F→C→G

In-Order Traversal for above example of binary tree is:


I-D-J-B-F-A-G-K-C-H

Pre-order Traversal; In this traversal method, the root node is visited first, then the
left subtree and finally the right subtree.

We start from A, and following pre-order traversal, we first visit A itself and then
move to its left subtree B. B is also traversed pre-order. The process goes on until all
the nodes are visited. The output of pre-order traversal of this tree will be −

A→B→D→E→C→F→G

120
Pre-Order Traversal for above example
binary tree is
A-B-D-I-J-F-C-G-K-H

Post-order Traversal; In this traversal method, the root node is visited last, hence
the name. First we traverse the left subtree, then the right subtree and finally the
root node.

We start from A, and following Post-order


traversal, we first visit the left subtree B. B is also
traversed post-order. The process goes on until
all the nodes are visited. The output of post-
order traversal of this tree will be

D→E→B→F→G→C→A

Post-Order Traversal for above example binary tree


is
I-J-D-F-B-K-G-H-C-A

121
STRUCTURES
C supports a constructed data type called structures as a mechanism for packing
data of different types. A structure is a convenient tool for handling a group of
logically related data items.
Structures are used to represent a record. Suppose you want to keep track of your
books in a library. You might want to track the following attributes of each book:
 Title
 Author
 Subject
 Book ID

Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines
a new data type, with more than one member. The format (syntax) of the struct
statement is as follows:
struct tag_name
{
member definition;
member definition;
...
member definition;
};
The structure tag is optional and each member definition is a normal variable
definition, such as int i; or float f; or any other valid variable definition. At the
end of the structure's definition, before the final semicolon, you can specify
one or more structure variables but it is optional. Here is the way you would
declare the Book structure:

122
struct Books
{
char title[20];
char author[15];
int pages;
float price;
};
The keyword struct declares a structure to hold the details of four data fields; - title,
author, pages, and price. These fields are called structure elements or members and
each may belong to a different data type.

Accessing Structure Members

To access any member of a structure, we use the member access operator (.). The
member access operator is coded as a period between the structure variable name
and the structure member that we wish to access. You would use the keyword struct
to define variables of structure type. The following example shows how to use a
structure in a program:
#include <stdio.h>
#include <string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( )
{

123
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */

/* book 1 specification */
strcpy( [Link], "C Programming");
strcpy( [Link], "Nuha Ali");
strcpy( [Link], "C Programming Tutorial");
Book1.book_id = 6495407;

/* book 2 specification */
strcpy( [Link], "Telecom Billing");
strcpy( [Link], "Zara Ali");
strcpy( [Link], "Telecom Billing Tutorial");
Book2.book_id = 6495700;

/* print Book1 info */


printf( "Book 1 title : %s\n", [Link]);
printf( "Book 1 author : %s\n", [Link]);
printf( "Book 1 subject : %s\n", [Link]);
printf( "Book 1 book_id : %d\n", Book1.book_id);

/* print Book2 info */


printf( "Book 2 title : %s\n", [Link]);
printf( "Book 2 author : %s\n", [Link]);
printf( "Book 2 subject : %s\n", [Link]);
printf( "Book 2 book_id : %d\n", Book2.book_id);
return 0; }

124
When the above code is compiled and executed, it produces the following
result:
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700

File Handling

In a lot of cases, hardcopy (a printout) of program results is needed, thus the


program will send the output to either the printer or the disk instead of the screen. A
program which either reads information from, or writes information to, a place on a
disk, is performing file handling.

A file represents a sequence of bytes on the disk where a group of related data is
stored. File is created for permanent storage of data. It is a ready-made structure.

In C language, we use a structure pointer of file type to declare a file.

Syntax: FILE *fp;

C provides a number of functions that helps to perform basic file operations.


Following are the functions,

125
Function Description

fopen() create a new file or open a existing


file
fclose() closes a file
getc() reads a character from a file
putc() writes a character to a file
fscanf() reads a set of data from a file
fprintf() writes a set of data to a file
getw() reads an integer from a file
putw() writes an integer to a file
fseek() set the position to the desired point
ftell() gives the current position in the file
rewind() set the position to the beginning point

Opening a File or Creating a File

The fopen() function is used to create a new file or to open an existing file.

General Syntax:

*fp = FILE *fopen(const char *filename, const char *mode);

Here filename is the name of the file to be opened and mode specifies the purpose
of opening the file. Mode can be of following types,

*fp is the FILE pointer (FILE *fp), which will hold the reference to the opened (or
created) file.

126
mode description

r opens a text file in reading mode - Open a text file for reading
Opens or creates a text file in writing mode i.e over writes on existing file
w
or creates a new file if the file does not exist.
a opens a text file in append mode
Opens a text file in both reading and writing mode – new data is written at
r+ the beginning overwriting the existing data. Open a text file for update
(that is, for both reading and writing)
opens a text file in both reading and writing mode – overwrite on existing
w+
data
opens a text file in both reading and writing mode – new data is appended
a+
at the end of file
rb opens a binary file in reading mode
wb opens or create a binary file in writing mode
ab opens a binary file in append mode
rb+ opens a binary file in both reading and writing mode
wb+ opens a binary file in both reading and writing mode
ab+ opens a binary file in both reading and writing mode

Explanation:

1. File can be opened in basic 3 modes: Reading Mode, Writing Mode,


Appending Mode
2. If File is not present on the path specified, then New File can be created
using Write and Append Mode.
3. Writing on the file will overwrite previous content

Closing a File

The fclose() function is used to close an already opened file.

127
General Syntax:

int fclose (FILE *fp);


Here fclose() function closes the file and returns zero on success, or EOF if there is
an error in closing the file. This EOF is a constant defined in the header file stdio.h.

fprintf() function directly writes into the file, while fscanf() reads from the file, which
can then be printed on console using standard printf() function.

Difference between Append and Write Mode

Write (w) mode and Append (a) mode, while opening a file are almost the same.
Both are used to write in a file. In both the modes, new file is created if it doesn't
exist already.

The only difference they have is, when you open a file in the write mode, the file is
reset, resulting in deletion of any data already present in the file. While in append
mode this will not happen. Append mode is used to append or add data to the
existing data of file (if any). Hence, when you open a file in Append (a) mode, the
cursor is positioned at the end of the present data in the file.

Reading and Writing in a Binary File

A Binary file is similar to the text file, but it contains only large numerical data. The
Opening modes are mentioned in the table for opening modes above.

fread() and fwrite() functions are used to read and write in a binary file.

fwrite(data-element-to-be-written, size_of_elements, number_of_elements, pointer-


to-file);

128
fread() is also used in the same way, with the same arguments like fwrite() function.
Below mentioned is a simple example of writing into a binary file

const char *mytext = "The quick brown fox jumps over the lazy dog";
FILE *bfp= fopen("[Link]", "wb");
if (bfp) {
fwrite(mytext, sizeof(char), strlen(mytext), bfp) ;
fclose(bfp) ;
}

fseek(), ftell() and rewind() functions

 fseek() - It is used to move the reading control to different positions using


fseek function.
 ftell() - It tells the byte location of current position of cursor in file pointer.
 rewind() - It moves the control to beginning of the file.

129

You might also like