Introduction To Computer Science 1.1
Introduction To Computer Science 1.1
Introduction To Computer Science 1.1
By:
Huong Nguyen
Introduction to Computer Science
By:
Huong Nguyen
Online:
< http://cnx.org/content/col10776/1.1/ >
CONNEXIONS
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Attributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
iv
The content of the human mind can be classied into four categories:
• Data: symbols;
• Information: data that are processed to be useful; provides answers to "who", "what", "where", and
"when" questions;
• Knowledge: understanding of data and information; answers "how" questions;
• Wisdom: evaluated understanding.
Data
Data consist of raw facts and gures - it does not have any meaning until it is processed and turned into
something useful.
Data comes in many forms; the main ones are letters, numbers and symbols.
Data is a prerequisite to information. For example, the two data items below could represent some very
important information:
DATA INFORMATION
123424331911 Your winning Lottery ticket number
211192 Your Birthday
An organization sometimes has to decide on the nature and volume of data that is required for creating the
necessary information.
Information
Information is the data that has been processed in such a way as to be meaningful to the person who
receives it.
1
2 CHAPTER 1. INTRODUCTION TO COMPUTER SCIENCE
Information processing is the change (processing) of information in any manner detectable by an observer.
Information processing may more specically be dened in terms by Claude E. Shannon as the conversion of
latent information into manifest.Input, process, output is a typical model for information processing. Each
stage possibly requires data storage.
Now that computer systems have become so powerful, some have been designed to make use of information
in a knowledgeable way. The following denition is of information processing
The electronic capture, collection, storage, manipulation, transmission, retrieval, and presentation of
information in the form of data, text, voice, or image and includes telecommunications and oce automation
functions.
History and Classication of Computers
Webster's Dictionary denes "computer" as any programmable electronic device that can store, retrieve, and
process data.
Blaise Pascal invents the rst commercial calculator, a hand powered adding machine
In 1946, ENIAC, based on John Von Neuman model completes.The rst commercially successful computer
is IBM 701.
A generation refers to the state of improvement in the development of a product. This term is also
used in the dierent advancements of computer technology. With each generation, the circuitry has gotten
smaller and more advanced than the previous generations before it. As a result of the miniaturization, the
speed, power and memory of computers has proportionally increased. New discoveries are constantly being
developed that aect the way we live, work and play. In terms of technological developments over time,
computers have been broadly classed into ve generations.
The rst computers used vacuum tubes for circuitry and magnetic drums for memory, and were often
enormous, taking up entire rooms. They were very expensive to operate and in addition to using a great
deal of electricity, they generated a lot of heat, which was often the cause of malfunctions. First generation
computers relied on machine language to perform operations, and they could only solve one problem at a
time. Input was based on punched cards and paper tape, and output was displayed on printouts.
The computers UNIVAC , ENIAC of the US and BESEM of the former Soviet Union are examples of
rst-generation computing devices.
Transistors replaced vacuum tubes and ushered in the second generation of computers. Computers becomed
smaller, faster, cheaper, more energy-ecient and more reliable than their rst-generation predecessors.
Second-generation computers still relied on punched cards for input and printouts for output. High-level
programming languages were being developed, such as early versions of COBOL and FORTRAN.
The rst computers of this generation were developed for the atomic energy industry.
The computers IBM-1070 of the US and MINSK of the former Soviet Union belonged to the second
generation.
The development of the integrated circuit was the hallmark of the third generation of computers. Transistors
were miniaturized and placed on silicon chips, called semiconductors, which drastically increased the speed
and eciency of computers. Users interacted with third generation computers through keyboards and
monitors and interfaced with an operating system, which allowed the device to run many dierent applications
at one time. Typical computers of the third generation are IBM 360 (United States) and EC (former Soviet
Union).
The microprocessor brought the fourth generation of computers, as thousands of integrated circuits were
built onto a single silicon chip. What in the rst generation lled an entire room could now t in the palm
of the hand. The Intel 4004 chip, developed in 1971, located all the components of the computer - from the
central processing unit and memory to input/output controls - on a single chip.
In 1981 IBM introduced its rst computer for the home user, and in 1984 Apple introduced the Macintosh.
Microprocessors also moved out of the realm of desktop computers and into many areas of life as more and
more everyday products began to use microprocessors.
As these small computers became more powerful, they could be linked together to form networks, which
eventually led to the development of the Internet. Fourth generation computers also saw the development
of GUI (Graphic User Interface), the mouse and handheld devices.
Fifth generation computing devices, based on articial intelligence, are still in development, though there are
some applications, such as voice recognition, that are being used today. The use of parallel processing and
superconductors is helping to make articial intelligence a reality. Quantum computation and molecular and
nanotechnology will radically change the face of computers in years to come. The goal of fth-generation
computing is to develop devices that respond to natural language input and are capable of learning and
self-organization.
Computers are available in dierent shapes, sizes and weights, due to these dierent shapes and sizes they
perform dierent sorts of jobs from one another.
The biggest in size, the most expensive in price than any other is classied and known as super computer.
It can process trillions of instructions in seconds. Governments specially use this type of computer for their
dierent calculations and heavy jobs. This kind of computer is also helpful for forecasting weather reports
worldwide.
Another giant in computers after the super computer is Mainframe, which can also process millions
of instruction per second and capable of accessing billions of data. This computer is commonly used in
big hospitals, airline reservations companies, and many other huge companies prefer mainframe because of
its capability of retrieving data on a huge basis. This is normally too expensive and out of reach from a
salary-based person who wants a computer for his home.
• Minicomputers
This computer oers less than mainframe in work and performance. These are the computers, which are
mostly preferred by the small type of business personals, colleges, and so on.
• Microcomputers
These computers are lesser in cost than the computers given above and also, small in size; They can store
a big amount of data and have a memory to meet the assignments of students and other necessary tasks of
business people. There are many types of microcomputers: desktop, workstation, laptop, PDA , etc.
In 1957 the German computer scientist Karl Steinbuch coined the word informatik by publishing a pa-
per called Informatik: Automatische Informationsverarbeitung (i.e. "Informatics: automatic information
processing"). The French term informatique was coined in 1962 by Philippe Dreyfus together with vari-
ous translationsinformatics (English), informatica (Italian, Spanish, Portuguese), informatika (Russian)
referring to the application of computers to store and process information.
The term was coined as a combination of "information" and "automation", to describe the science of
automatic information processing.
Informatics is more oriented towards mathematics than computer science.
Computer Science is the study of computers, including both hardware and software design. Computer science
is composed of many broad disciplines, for instance, articial intelligence and software engineering.
Includes all matters concerned with the furtherance of computer science and technology and with the design,
development, installation, and implementation of information systems and applications
ICT (information and communications technology - or technologies) is an umbrella term that includes any
communication device or application, encompassing: radio, television, cellular phones, computer and network
hardware and software, satellite systems and so on, as well as the various services and applications associated
with them, such as videoconferencing and distance learning.
2
1.2 Data Representation in a Computer
Computer must not only be able to carry out computations, they must be able to do them quickly and
eciently. There are several data representations, typically for integers, real numbers, characters, and
logical values.
b basic symbols (or digits) corresponding to natural numbers between 0 and b − 1 are used in the represen-
tation of numbers.
To generate the rest of the numerals, the position of the symbol in the gure is used. The symbol in the
last position has its own value, and as it moves to the left its value is multiplied by b.
We write a number in the numeral system of base b by expressing it in the form
N(b), with n+1 digit for integer and m digits for fractional part, represents the sum:
Figure 1.3
in the decimal system. Note that ai is the ith digit from the position of a0
Decimal, Binary, Octal and Hexadecimal are common used numeral system. The decimal system has ten
as its base. It is the most widely used numeral system, because humans have four ngers and a thumb on
each hand, giving total of ten digit over both hand.
Switches, mimicked by their electronic successors built of vacuum tubes, have only two possible states:
"open" and "closed". Substituting open=1 and closed=0 yields the entire set of binary digits. Modern
computers use transistors that represent two states with either high or low voltages. Binary digits are
arranged in groups to aid in processing, and to make the binary numbers shorter and more manageable for
humans.Thus base 16 (hexadecimal) is commonly used as shorthand. Base 8 (octal) has also been used for
this purpose.
Decimal System
Decimal notation is the writing of numbers in the base-ten numeral system, which uses various symbols
(called digits) for no more than ten distinct values (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9) to represent any number,
no matter how large. These digits are often used with a decimal separator which indicates the start of a
fractional part, and with one of the sign symbols + (positive) or − (negative) in front of the numerals to
indicate sign.
Decimal system is a place-value system. This means that the place or location where you put a numeral
determines its corresponding numerical value. A two in the one's place means two times one or two. A two
in the one-thousand's place means two times one thousand or two thousand.
The place values increase from right to left. The rst place just before the decimal point is the one's
place, the second place or next place to the left is the ten's place, the third place is the hundred's place, and
so on.
The place-value of the place immediately to the left of the "decimal" point is one in all place-value number
systems. The place-value of any place to the left of the one's place is a whole number computed from a
product (multiplication) in which the base of the number system is repeated as a factor one less number of
times than the position of the place.
The binary number system is base 2 and therefore requires only two digits, 0 and 1. The binary system is
useful for computer programmers, because it can be used to represent the digital on/o method in which
computer chips and memory work.
A binary number can be represented by any sequence of bits (binary digits), which in turn may be
represented by any mechanism capable of being in two mutually exclusive states.
Counting in binary is similar to counting in any other number system. Beginning with a single digit,
counting proceeds through each symbol, in increasing order. Decimal counting uses the symbols 0 through
9, while binary only uses the symbols 0 and 1.
When the symbols for the rst digit are exhausted, the next-higher digit (to the left) is incremented, and
counting starts over at 0A single bit can represent one of two values, 0 or 1.Binary numbers are convertible
to decimal numbers.
Here's an example of a binary number, 11101.11(2) , and its representation in the decimal notation
Figure 1.4
The hexadecimal system is base 16. Therefore, it requires 16 digits. The digits 0 through 9 are used, along
with the letters A through F, which represent the decimal values 10 through 15. Here is an example of a
hexadecimal number and its decimal equivalent:
The hexadecimal system (often called the hex system) is useful in computer work because it is based on
powers of 2. Each digit in the hex system is equivalent to a four-digit binary number. Table below shows
some hex/decimal/binary equivalents.
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111
10 16 10000
F0 240 11110000
FF 255 11111111
Table 1.1
Binary is also easily converted to the octal numeral system, since octal uses a radix of 8, which is a power
of two (namely, 23, so it takes exactly three binary digits to represent an octal digit). The correspondence
between octal and binary numerals is the same as for the rst eight digits of hexadecimal in the table above.
Binary 000 is equivalent to the octal digit 0, binary 111 is equivalent to octal 7, and so forth.
Converting from octal to binary proceeds in the same fashion as it does for hexadecimal:
To convert a decimal fraction to another base, say base b, you split it into an integer and a fractional part.
Then divide the integer by b repeatedly to get each digit as a remainder. Namely, with value of integer part
= dn−1 dn−2 ...d2 d1 d0(10) , rst divide value by b the remainder is the least signicant digit a0 . Divide the
result by b, the remainder is a1 .Continue this process until the result is zero, giving the most signicant
digit, an−1 . Let's convert 43868(10) to hexadecimal:
After that, multiply the fractional part by b repeatedly to get each digit as an integer part. We will
continue this process until we get a zero as our fractional part or until we recognize an innite repeating
pattern.
Data Representation refers to the methods used internally to represent information stored in a computer.
Computers store lots of dierent types of information:
• numbers
• text
• graphics of many varieties (stills, video, animation)
• sound
At least, these all seem dierent to us. However, all types of information stored in a computer are stored
internally in the same simple format: a sequence of 0's and 1's. How can a sequence of 0's and 1's represent
things as diverse as your photograph, your favorite song, a recent movie, and your term paper?
Digital signal
Continuous signalPhysical signalComputerConvert ADSensor
Depending on the nature of its internal representation, data items are divided into:
• Basic types (simple types or type primitives) : the standard scalar predened types that one would
expect to nd ready for immediate use in any programming language
• Structured types(Higher level types) are then made up from such basic types or other existing higher
level types.
The most basic unit of information in a digital computer is called a BIT, which is a contraction of Binary
Digit. In the concrete sense, a bit is nothing more than a state of "on" or "o" (or "high" and "low")
within a computer circuit. In 1964, the designers of the IBM System/360 mainframe computer established
a convention of using groups of 8 bits as the basic unit of addressable computer storage. They called this
collection of 8 bits a byte.
Computer words consist of two or more adjacent bytes that are sometimes addressed and almost always
are manipulated collectively. The word size represents the data size that is handled most eciently by a
particular architecture. Words can be 16 bits, 32 bits, 64 bits, or any other size that makes sense within the
context of a computer's organization.
Some other units of information are described in the following table :
Figure 1.7
Representation of Integers
An integer is a number with no fractional part; it can be positive, negative or zero. In ordinary usage, one
uses a minus sign to designate a negative integer. However, a computer can only store information in bits,
which can only have the values zero or one. We might expect, therefore, that the storage of negative integers
in a computer might require some special technique - allocating one sign bit (often the most signicant bit)
to represent the sign: set that bit to 0 for a positive number, and set to 1 for a negative number.
Unsigned integers are represented by a xed number of bits (typically 8, 16, 32, and/or 64)
If an operation on bytes has a result outside this range, it will cause an `overow'
The binary representation discussed above is a standard code for storing unsigned integer numbers. However,
most computer applications use signed integers as well; i.e. the integers that may be either positive or
negative.
In binary we can use one bit within a representation (usually the most signicant or leading bit) to
indicate either positive (0) or negative (1), and store the unsigned binary representation of the magnitude
in the remaining bits.
However, for reasons of ease of design of circuits to do arithmetic on signed binary numbers (e.g. addition
and subtraction), a more common representation scheme is used called two's complement. In this scheme,
positive numbers are represented in binary, the same as for unsigned numbers. On the other hand, a negative
number is represented by taking the binary representation of the magnitude:
• Complement the bits : Replace all the 1's with 0's, and all the 0's with 1's;
• Add one to the complemented number.
Example
+4210 = 001010102
and so
-4210 = 110101102
Example
Performing two's complement on the decimal 42 to get -42
Using a eight-bit representation
1 1 1 carries
1 1 0 1 0 0 0 1 = 208 (base 10)
+ 0 1 0 0 1 0 0 1 = 73 (base 10)
----------------
1 0 0 0 1 1 0 1 0 = 281 (base 10)
The result exceeds the magnitude which can be represented with 8 bits. This is an overow.
Subtraction is executed by using two's complement
Addition and subtraction of signed binary numbers
Binary Multiplication
Multiplication in the binary system works the same way as in the decimal system:
0 x 0 = 0
0 x 1 = 0
1 x 0 = 0
1 x 1 = 1, and no carry or borrow bits
Example
0 0 1 0 1 0 0 1 = 41(base 10)
× 0 0 0 0 0 1 1 0 = 6(base 10)
----------------------
0 0 0 0 0 0 0
0 1 0 1 0 0 1
0 1 0 1 0 0 1
----------------------------
0 0 1 1 1 1 0 1 1 0 = 246(base 10)
0 0 0 1 0 1 1 1 = 23(base 10)
× 0 0 0 0 0 0 1 1 = 3(base 10)
----------------------
1 1 1 1 1 carries
0 0 1 0 1 1 1
0 0 1 0 1 1 1
0 0 1 0 0 0 1 0 1 = 69(base 10)
Figure 1.8
x y x AND y x OR y x XOR y
1 1 1 1 0
1 0 0 1 1
0 1 0 1 1
0 0 0 0 0
Table 1.2
NOT 0111
= 1000
AND operation
An AND operation takes two binary representations of equal length and performs the logical AND
operation on each pair of corresponding bits. In each pair, the result is 1 if the rst bit is 1 AND the second
bit is 1. Otherwise, the result is 0.
Example
0101
AND 0011
= 0001
OR operation
An OR operation takes two bit patterns of equal length, and produces another one of the same length by
matching up corresponding bits (the rst of each; the second of each; and so on) and performing the logical
OR operation on each pair of corresponding bits.
Example
0101
OR 0011
= 0111
XOR Operation
An exclusive or operation takes two bit patterns of equal length and performs the logical XOR operation
on each pair of corresponding bits.
Example
0101
XOR 0011
= 0110
It is important to handle character data. Character data is not just alphabetic characters, but also numeric
characters, punctuation, spaces, etc. They need to be represented in binary.
There aren't mathematical properties for character data, so assigning binary codes for characters is
somewhat arbitrary.
ASCII Code Table
ASCII stands for American Standard Code for Information Interchange. The ASCII standard was devel-
oped in 1963, permitted machines from dierent manufacturers to exchange data.
ASCII code table consists of 128 binary values (0 to 127), each associated with a character or command.
The non-printing characters are used to control peripherals such as printer.
The extended ASCII character set also consists 128 128 characters representing additional special, math-
ematical, graphic and foreign characters.
There are some problems with the ASCII code table. With ASCII character set, string datatypes allocated
one byte per character. But logographic languages such as Chinese, Japanese, and Korean need far more
than 256 characters for reasonable representation. Even Vietnamese, a language uses almost Latin letters,
need 61 characters for representation. Where can we nd numbers for our characters? is it a solution : 2
bytes per character?
Hundreds of dierent encoding systems were invented. But these encoding systems conict with one
another : two encodings can use the same number for two dierent characters, or use dierent numbers for
the same character.
The Unicode standard was rst published in 1991. With two bytes for each character, it can represent
216-1 dierent characters.
The Unicode standard has been adopted by such industry leaders as HP, IBM, Microsoft, Oracle, Sun,
and many others. It is supported in many operating systems, all modern browsers, and many other products.
The obvious advantages of using Unicode are :
• To oer signicant cost savings over the use of legacy character sets.
• To enable a single software product or a single website to be targeted across multiple platforms,
languages and countries without re-engineering.
• To allow data to be transported through many dierent systems without corruption.
No human system of numeration can give a unique representation to real numbers. If you give the rst few
decimal places of a real number, you are giving an approximation to it.
Mathematicians may think of one approach : a real number x can be approximated by any number in
the range from x - epsilon to x + epsilon. It is xed-point representation. Fixed-point representations are
unsatisfactory for most applications involving real numbers.
Scientists or engineers will probably use scientic notation: a number is expressed as the product of a
mantissa and some power of ten.
A system of numeration for real numbers will typically store the same three data a sign, a mantissa,
and an exponent into an allocated region of storage
The analogues of scientic notation in computer are described as oating-point representations.
In the decimal system, the decimal point indicates the start of negative powers of 10.
If we are using a system in base k (ie the radix is k), the `radix point' serves the same function:
= 5.625(10)
A oating point representation allows a large range of numbers to be represented in a relatively small
number of digits by separating the digits used for precision from the digits used for range.
To avoid multiple representations of the same number oating point numbers are usually normalized so
that there is only one nonzero digit to the left of the `radix' point, called the leading digit.
A normalized (non-zero) oating-point number will be represented using
s
(−1) d0 · d1 d2 ...dp−1 × be (1.11)
where
• s is the sign,
• d0 · d1 d2 ...dp−1 - termed the signicand - has p signicant digits, each digit satises 0≤ di <b
• e, emin ≤ e ≤ emax , is the exponent
• b is the base (or radix)
Example
If k = 10 (base 10) and p = 3, the number 0·1 is represented as 0.100
If k = 2 (base 2) and p = 24, the decimal number 0·1 cannot be represented exactly but is approximately
1 · 10011001100110011001101 × 2−4
Formally,
s s
d0 + d1 b−1 + d2 b−2 ...d−1 b(p−1) be
(−1) d0 · d1 d2 ...dp−1 be represents the value (−1)
In brief, a normalized representation of a real number consist of
• The range of the number : the number of digits in the exponent (i.e. by emax ) and the base b to which
it is raised
• The precision : the number of digits p in the signicand and its base b
There are many ways to represent oating point numbers. In order to improve portability most computers
use the IEEE 754 oating point standard.
There are two primary formats:
Notes
• Single precision has 24 bits precision, equivalent to about 7.2 decimal digits.
• The largest representable non-innite number is almost 2 × 2127 ∼= 3.402823 × 1038
−127 ∼
• The smallest representable non-zero normalized number is 1 × 2 = 1.17549 × 10−38
3
1.3 Computer Systems
A computer is an electronic device that performs calculations on data, presenting the results to humans
or other computers in a variety of (hopefully useful) ways. The computer system includes not only the
hardware, but also software that are necessary to make the computer function.
Computer hardware is the physical part of a computer, including the digital circuitry, as distinguished
from the computer software that executes within the hardware.
Computer software is a general term used to describe a collection of computer programs, procedures and
documentation that perform some task on a computer system
A computer performs basically ve major operations or functions irrespective of their size and make.
1. Input: This is the process of entering data and programs in to the computer system. You should
know that computer is an electronic machine like any other machine which takes as inputs raw data and
performs some processing giving out processed data. Therefore, the input unit takes data from us to the
computer in an organized manner for processing.
2. Storage: The process of saving data and instructions permanently is known as storage. Data has
to be fed into the system before the actual processing starts. It is because the processing speed of Central
Processing Unit (CPU) is so fast that the data has to be provided to CPU with the same speed. Therefore
the data is rst stored in the storage unit for faster access and processing. This storage unit or the primary
storage of the computer system is designed to do the above functionality. It provides space for storing data
and instructions.
The storage unit performs the following major functions:
- All data and instructions are stored here before and after processing.
- Intermediate results of processing are also stored here.
3. Processing: The task of performing operations like arithmetic and logical operations is called
processing. The Central Processing Unit (CPU) takes data and instructions from the storage unit and
makes all sorts of calculations based on the instructions given and the type of data provided. It is then sent
back to the storage unit.
4. Output: This is the process of producing results from the data for getting useful information.
Similarly the output produced by the computer after processing must also be kept somewhere inside the
computer before being given to you in human readable form. Again the output is also stored inside the
computer for further processing.
5. Control: The manner how instructions are executed and the above operations are performed.
Controlling of all operations like input, processing and output are performed by control unit. It takes
care of step by step processing of all operations in side the computer.
In order to carry out the operations mentioned above, the computer allocates the task between its various
functional units. The computer system is divided into several units for its operation.
• CPU (central processing unit) : The place where decisions are made, computations are performed, and
input/output requests are delegated
• Memory: stores information being processed by the CPU
• Input devices : allows people to supply information to computers
• Output devices : allows people to receive information from computers
• Buses : a bus is a subsystem that transfers data or power between computer components inside a
computer.
The basic function of a computer is program execution. When a program is running the executable binary le
is copied from the disk drive into memory. The process of program execution is the retrieval of instructions
and data from memory, and the execution of the various operations.The cycles with complex instruction sets
typically utilize the following stages :
Fetch the instruction from main memory
The CPU presents the value of the program counter (PC) on the address bus. The CPU then fetches
the instruction from main memory via the data bus into the Memory Data Register (MDR). The value from
the MDR is then placed into the Current Instruction Register (CIR), a circuit that holds the instruction so
that it can be decoded and executed.
Decode the instruction
The instruction decoder interprets and implements the instruction.
Fetch data from main memory
Read the eective address from main memory if the instruction has an indirect address. Fetch required
data from main memory to be processed and placed into registers.
Execute the instruction
From the instruction register, the data forming the instruction is decoded by the control unit. It then
passes the decoded information as a sequence of control signals to the relevant function units of the CPU to
perform the actions required by the instruction such as reading values from registers, passing them to the
Arithmetic logic unit (ALU) to calculate the result and writing the result back to a register. A condition
signal is sent back to the control unit by the ALU if it is involved.
Store results
The result generated by the operation is stored in the main memory, or sent to an output device. Based
on the condition feedback from the ALU, the PC is either incremented to address the next instruction or
updated to a dierent address where the next instruction will be fetched. The cycle is then repeated.
You may call CPU as the brain of any computer system. It is the brain that takes all major decisions, makes
all sorts of calculations and directs dierent parts of the computer functions by activating and controlling
the operations.
CPU has four key parts
• Control Unit
• Arithmetic & Logic Unit
• Registers
• Clock
1.3.1.3 Memory
Memory refer to computer components, devices and recording media that retain digital data used for com-
puting for some interval of time. Computer memory includes internal and external memory.
Internal memory
The internal memory is accessible by a processor without the use of the computer input-output channels.It
usually includes several types of storage, such as main storage, cache memory, and special registers, all of
which can be directly accessed by the processor.
Cache memory : A buer, smaller and faster than main storage, used to hold a copy of instructions
and data in main storage that are likely to be needed next by the processor and that have been obtained
automatically from main storage.
Main memory (Main Storage) : addressable storage from which instructions and other data may be
loaded directly into registers for subsequent execution or processing.
Storage capacity of the main memory is the total amount of stored information that the memory can
hold. It is expressed as a quantity of bits or bytes. Each address identies a word of storage. So the capacity
of the main memory depends on the number of bits allowed to address. For instance, a computer allows
also 32-bit memory addresses; a byte-addressable 32-bit computer can address 232 = 4,294,967,296 bytes of
memory, or 4 gigabytes (GB). The capacity of the main memory is 4 GB.
The main memory consists of ROM and RAM.
• Random Access Memory (RAM): The primary storage is referred to as random access memory (RAM)
because it is possible to randomly select and use any location of the memory directly store and retrieve
data. It takes same time to any address of the memory as the rst address. It is also called read/write
memory. The storage of data and instructions inside the primary storage is temporary. It disappears
from RAM as soon as the power to the computer is switched o.
• Read Only Memory (ROM): There is another memory in computer, which is called Read Only Memory
(ROM). Again it is the ICs inside the PC that form the ROM. The storage of program and data
in the ROM is permanent. The ROM stores some standard processing programs supplied by the
manufacturers to operate the personal computer. The ROM can only be read by the CPU but it
cannot be changed. The basic input/output program is stored in the ROM that examines and initializes
various equipment attached to the PC when the switch is made ON.
External Memory
The external memory holds information too large for storage in main memory. Information on external
memory can only be accessed by the CPU if it is rst transferred to main memory. External memory is slow
and virtually unlimited in capacity. It retains information when the computer is switched o and is used to
keep a permanent copy of programs and data.
Hard Disk: is made of magnetic material. Magnetic disks used in computer are made on the same
principle. It rotates with very high speed inside the computer drive. Data is stored on both the surface of
the disk. Magnetic disks are most popular for direct access storage device. Each disk consists of a number
of invisible concentric circles called tracks. Information is recorded on tracks of a disk surface in the form of
tiny magnetic spots. The presence of a magnetic spot represents one bit and its absence represents zero bit.
The information stored in a disk can be read many times without aecting the stored data. So the reading
operation is non-destructive. But if you want to write a new data, then the existing data is erased from the
disk and new data is recorded. The capacity of a hard disk is possibly 20 GB, 30 GB, 40 GB, 60 GB or
more.
Floppy Disk: It is similar to magnetic disk discussed above. They are 5.25 inch or 3.5 inch in diameter.
They come in single or double density and recorded on one or both surface of the diskette. The capacity of
a 5.25-inch oppy is 1.2 mega bytes whereas for 3.5 inch oppy it is 1.44 mega bytes. The oppy is a low
cost device particularly suitable for personal computer system.
Optical Disk:With every new application and software (includes sounds, images and videos) there is
greater demand for memory capacity. It is the necessity to store large volume of data that has led to the
development of optical disk storage medium. There are two commonly used categories of optical disks: CD
with the approximate capacity of 700MB and DVD with the approximate capacity of 4.7GB
Memory Stick (Flash card, ash drive) a removable ash memory card format, with 128MB, 256 MB,
512 MB, 1 GB, 2 GB , 4 GB or more capacities
A computer is only useful when it is able to communicate with the external environment. When you work
with the computer you feed your data and instructions through some devices to the computer. These devices
are called Input devices. Similarly the computer after processing, gives output through other devices called
output devices.
Common input and output devices are: Speakers, Mouse, Scanner, Printer,Joystick, CD-ROM, Keyboard,
Microphone, DVD, Floppy drive, Hard drive, Magnetic tape, and Monitor. Some devices are capable of both
input and output.
Input Devices
Input devices are necessary to convert our information or data in to a form which can be understood by
the computer. A good input device should provide timely, accurate and useful data to the main memory of
the computer for processing followings are the most useful input devices.
Keyboard: - This is the standard input device attached to all computers. The layout of keyboard is just
like the traditional typewriter. It also contains some extra command keys and function keys. It contains
a total of 101 to 104 keys. You must press correct combination of keys to input data. The computer
can recognize the electrical signals corresponding to the correct key combination and processing is done
accordingly.
Mouse: - Mouse is an input device that is used with your personal computer. It rolls on a small ball
and has two or three buttons on the top.When you roll the mouse across a at surface the screen censors
the mouse in the direction of mouse movement. The cursor moves very fast with mouse giving you more
freedom to work in any direction. It is easier and faster to move through a mouse.
Scanner: The keyboard can input only text through keys provided in it. If we want to input a picture
the keyboard cannot do that. Scanner is an optical device that can input any graphical matter and display
it back.
Output Devices
Monitor: The most popular input/output device is the monitor. A Keyboard is used to input data and
Monitor is used to display the input data and to receive massages from the computer. A monitor has its
own box which is separated from the main computer system and is connected to the computer by cable. It
can be color or monochrome. It is controlled by an output device called a graphics card. Displayable area
measured in dots per inch, dots are often referred to as pixels. Standard resolution is 640 by 480. Many
cards support resolution of 1280 by 1024 or better. Number of colors supported varies from 16 to billions
Printer: It is an important output device which can be used to get a printed copy of the processed text
or result on paper. There are dierent types of printers that are designed for dierent types of applications.
1.3.1.5 Buses
Bus is a subsystem that transfers data or power between computer components inside a computer or between
computers. Bus can logically connect several peripherals over the same set of wires. Each bus denes its set
of connectors to physically plug devices, cards or cables together. The buses are categorized depending on
their tasks:
There are many steps involved in writing a computer program to solve a given problem. The steps go form
problem formulation and specication, to design of the solution, to implementation, testing and documen-
tation, and evaluation the solution.
Once we have a suitable mathematical model for our problem, we attempt to nd a solution in term of
that model. Our initial goal is to nd a solution in the form of an algorithm. So what is an algorithm?
Algorithm is a nite sequence of instructions each of which has a clear meaning and can be performed
with a nite amount of eort in a nite length of time.
How do you represent an algorithm? The most obvious representation: source code of a programming
language. However, writing source code before you fully understand an algorithm often leads to dicult-to-
nd bugs. So, algorithms may be presented ...
1. In words
To present the algorithm in words we may describe the tasks step by step.
2.As a owchart
A familiar technique for overcoming those bugs involves owcharts.
A owchart is a visual representation of an algorithm's control ow. That representation illustrates
statements that need to execute, decisions that need to be made, logic ow (for iteration and other purposes),
and terminals that indicate start and end points.
To present that visual representation, a owchart uses various symbols, which Figure 3.5.shows.
Figure 1.17: Flowchart symbol for statements, decisions, logic ows, etc.
This review was essential because we will be using these building blocks quite often today.
3. In pseudocode
Pseudocode (derived from pseudo and code) is a compact and informal high-level description of a com-
puter programming algorithm that uses the structural conventions of programming languages, but omits
detailed subroutines, variable declarations or language-specic syntax. The programming language is aug-
mented with natural language descriptions of the details, where convenient, or with compact mathematical
notation.
Example
Present the algorithm of converting an integer from decimal to binary
a. By words
Step 1: Let x is the decimal integer you want to convert and let k=1
Step 2 : Divide x by 2, saving the quotient as Q, and the remainder (in binary) as Rk
Step 3 : If Q is not zero, let X=Q, and go back to step 2. Otherwise go to step 4.
Step 4 : Assume step 1-3 were repeated n times. Arrange the remainders as string for digit
Rn Rn−1 ...R3 R2 R1 .
b. As a owchart
Figure 1.18: Flowchart of the algorithm of converting an integer from decimal to binary
c. By pseudocode
BEGIN
input x.
y=''''
remainder=0,
while (x>0)
begin
quotient=x/2
remainder=x mod 2
y=conc(remainder,y)
x=quotient
end
print y
END.
Example
Bubble Sort
Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted,
comparing two items at a time and swapping them if they are in the wrong order. The pass through the list
is repeated until no swaps are needed, which indicates that the list is sorted.
5 1 4 2 8 - unsorted array
1 4 2 5 8 - after one pass
1 2 4 5 8 - sorted array
The algorithm gets its name from the way smaller elements "bubble" to the top (i.e. the beginning) of
the list via the swaps.Because it only uses comparisons to operate on elements, it is a comparison sort. This
is the easiest comparison sort to implement.
Here are the presentations of bubble sort algorithm
a. By words
Step 1: Get the length of the list : N and the list: list[1],list[2],. . .,list[N]
Step 2: M ← N.
Step 3: If M < 2 then print the list, stop.
Step 4: M ← M 1, i ← 0.
Step 5: Increase i by 1
Step 6: If i > M then go to step 3.
Step 7: If list[i] > list[i+1] swap list[i] and list[i+1]
Step 8: Go to step 5.
b. As a ow chart
c. In pseudocode
A simple way to express bubble sort in pseudocode is as follows:
• Exactness
• Eectiveness
• Guaranteed termination
• Generality
The concept of structured programming says that any programming logic problem can be solved using an
appropriate combination of only three programming structures,
1.Sequence: a sequence of instructions that are executed in the precise order they are written in
Figure 1.20
2. Conditional : Select between alternate courses of action depending upon the evaluation of a condition
Figure 1.21
Figure 1.22
Programs
A computer program is an algorithm written for a computer in a special programming language.
Programming languages
A programming language is an articial language that can be used to control the behavior of a machine,
particularly a computer. It is dened through the use of syntactic and semantic rules, to determine structure
and meaning respectively.
Programming languages are used to facilitate communication about the task of organizing and manipu-
lating information, and to express algorithms precisely.
There are large number of programming language in use. We can identify three type of programming
languages : machine languages, assembly languages, high-level languages.
Machine Languages
Machine code or machine language is a system of instructions and data directly executed by a computer's
central processing unit. Machine code is the lowest-level of abstraction for representing a computer pro-
gram.Instructions are patterns of bits with dierent patterns corresponding to dierent commands to the
machine. Machine code has several signicant disadvantages : very dicult for a human to read and write,
a program written on one computer cannot run on a dierent computer, so it cannot be used to write large
program or program intended to run on dierent machines.
Assembly Languages
An assembly language is a low-level language for programming computers. It implements a symbolic
representation of the numeric machine codes and other constants needed to program a particular CPU
architecture.
This representation is usually dened by the hardware manufacturer, and is based on abbreviations
(called mnemonics) that help the programmer remember individual instructions, registers, etc. An assembly
language is thus specic to a certain physical or virtual computer architecture
A utility program called an assembler, is used to translate assembly language statements into the target
computer's machine code.
Although assembly is more friendly than machine code, use of assembly oer several disadvantages, for
instance, each type of computer has its own assembly language or programming assembly requires much time
and eort.
Hence, assembly language is not use to write large programs. However, there are some computer appli-
cation, such as in writing program that control peripherals, assembly is still a necessity.
High-level languages
A high-level programming language is a programming language that, may be more abstract, easier to
use, or more portable across platforms.
Examples: Pascal, C, Visual Basic, SQL, . . . .
Such languages often abstract away CPU operations such as memory access models and management of
scope.These languages have been implemented by translating to machine languages.
There are two types of translators
• Compiler is a program that translate source code from a high-level programming language to a lower
level language (e.g., assembly language or machine language)
• Interpreter is a program that translates and executes source language statements one line at a time.
Figure 1.23 below shows the process of solving problem with computers
Domain Analysis
Often the rst step in attempting to design a new piece of software, whether it be an addition to an
existing software, a new application, a new subsystem or a whole new system, is, what is generally referred
to as "Domain Analysis". The more knowledgeable they are about the domain already, the less the work
required. Another objective of this work is to make the analysts who will later try to elicit and gather the
requirements from the area experts or professionals, speak with them in the domain's own terminology and
to better understand what is being said by these people. Otherwise they will not be taken seriously. So, this
phase is an important prelude to extracting and gathering the requirements.
Software Elements Analysis
The most important task in creating a software product is extracting the requirements. Customers typi-
cally know what they want, but not what software should do, while incomplete, ambiguous or contradictory
requirements are recognized by skilled and experienced software engineers. Frequently demonstrating live
code may help reduce the risk that the requirements are incorrect.
Specication
Specication is the task of precisely describing the software to be written, possibly in a rigorous way. In
practice, most successful specications are written to understand and ne-tune applications that were already
well-developed, although safety-critical software systems are often carefully specied prior to application
development. Specications are most important for external interfaces that must remain stable.
Software architecture
The architecture of a software system refers to an abstract representation of that system. Architecture
is concerned with making sure the software system will meet the requirements of the product, as well as
ensuring that future requirements can be addressed. The architecture step also addresses interfaces between
the software system and other software products, as well as the underlying hardware or the host operating
system.
Implementation (or coding)
Reducing a design to code may be the most obvious part of the software engineering job, but it is not
necessarily the largest portion.
Testing
Testing of parts of software, especially where code by two dierent engineers must work together, falls to
the software engineer.
Documentation
An important (and often overlooked) task is documenting the internal design of software for the purpose
of future maintenance and enhancement. Documentation is most important for external interfaces.
The software is divided to System Software and Application Software with each having several sub levels.
System software is the low level software required to manage computer resources and support the
production or execution of application program.
Application software is software program that perform a specic function directly for the end user.
System Software includes
• General business productivity applications : software program that perform a specic function di-
rectly for the end user, examples include : oce applications, word processors, spreadsheet, project
management system ,etc.
• Home use applications : software used in the home for entertainment, reference or educational purposes,
examples include games, home education etc.
• Cross-industry application software : software that is designed to perform and/or manage a specic
business function or process that is not unique to a particular industry, examples include professional
accounting software, human resources management, Geographic Information Systems (GIS) software,
etc.
• Vertical market application software : software that perform a wide range of business functions for a
specic industry such as manufacturing, retail, healthcare , engineering, restaurant, etc.
• Utilities software : a small program that performs a very specic task. Examples include : compression
programs, antivirus, search engines, font, le viewers, voice recognition software, etc.
4
1.4 Operating Systems
• Has a set of commands that allow for manipulation of the le system: sort, delete, copy, etc.
• Perform input and output on a variety of devices
• Allocate Resources
• Manage the running systems
1.4.1.1 File
A computer le is a block of arbitrary information, or resource for storing information, which is available
to a computer program and is usually based on some kind of durable storage. A le is durable in the sense
that it remains available for programs to use after the current program has nished. Computer les can
be considered as the modern counterpart of paper documents which traditionally were kept in oces' and
libraries' les, which are the source of the term.
A lename is a special kind of string used to uniquely identify a le stored on the le system of a computer.
Depending on the operating system, such a name may also identify a directory. Dierent operating systems
impose dierent restrictions regarding length and allowed characters on lenames.
Many operating systems, including MS-DOS, Microsoft Windows, allow a lename extension that consists
of one or more characters following the last period in the lename, thus dividing the lename into two parts:
the base name (the primary lename) and the extension (usually indicating the le type associated with a
certain le format). The base name and the extension are separated by a dot.
Commonly, the extension indicates the content type of the le, for example,
exe: executable le, txt : text le, pas : pascal source le, cpp : C++ source le. . .
In MS-DOS, Microsoft Windows, you can use wildcards ? and *. ? marks a single character while *
Marks any sequence of characters.
Example *.pas : all pascal source les of the current directory , possibly t1.pas, book.pas. . .
b*.cpp : all C++ source les beginning with b.
Structures of Disks
Floppy disk can be single-sided or double-sided. Data is stored on a disk in circular tracks. Tracks are
numbered 0, 1. . . Each track is broken up into arcs called sectors. Each sector stores a xed amount of
data. The typical formatting of these media provide space for 512 bytes (for magnetic disks) or 2048 bytes
(for optical discs) of user-accessible data per sector.
The rst or top-most directory in a hierarchy is the root directory (symbolized by the back slash \)
The current directory is the directory in which a user is working at a given time.
Full name of a le
A full lename includes one or more of these components
An operating system includes several les, for instant, MS-DOS includes MSDOS.SYS, IO.SYS, COM-
MAND.COM . . .
MS-DOS (short for Microsoft Disk Operating System) is an operating system commercialized by Microsoft.
It was the most commonly used member of the DOS family of operating systems and was the dominant
operating system for the PC compatible platform during the 1980s. It has gradually been replaced on
consumer desktop computers by various generations of the Windows operating system.
MS-DOS employs a command line interface and a batch scripting facility via its command interpreter,
COMMAND.COM.
Microsoft Windows is the name of several families of software operating systems by Microsoft. Microsoft
Windows interest in graphical user interfaces (GUI)
MsWindows are introduced in detail in the next section.
Every operating system need a system of command for managing les and disks. Commonly used types are
:
In 1983 Microsoft announced its development of Windows, a graphical user interface (GUI) for its own
operating system. Windows 3.0, released in 1990, was a complete overhaul of the Windows environment
with the capability to address memory beyond 640K and a much more powerful user interface.
Windows for Workgroups 3.1 was the rst integrated Windows and networking package oered by Mi-
crosoft.Windows for Workgroups also includes two additional applications: Microsoft Mail, a network mail
package, and Schedule+, a workgroup scheduler.
Windows 95, released in August of 1995. A 32-bit system providing full pre-emptive multitasking,
advanced le systems, threading, networking and more.Also includes a completely revised user interface.
Windows 98, released in June of 1998. Integrated Web Browsing gives your desktop a browser-like
interface.
Windows 2000 provides an impressive platform of Internet, intranet, extranet, and management applica-
tions that integrate tightly with Active Directory.
In September 2000 Microsoft released Windows Me, short for Millennium Edition, which is aimed at the
home user. The Me operating system boasts some enhanced multimedia features, such as an automated
video editor and improved Internet plumbing.
Windows XP-Microsoft ocially launches it on October 25th. 2001.XP is a whole new kind of Windows
for consumers. Under the hood, it contains the 32-bit kernel and driver set from Windows NT and Windows
2000. Naturally it has tons of new features that no previous version of Windows has.
Windows Vista is a line of graphical operating systems used on personal computers, including home and
business desktops, notebook computers, Tablet PCs, and media centersWindows Vista contains hundreds
of new and reworked features; some of the most signicant include an updated graphical user interface and
visual style dubbed Windows Aero, improved searching features, new multimedia creation tools such as
Windows DVD Maker, and completely redesigned networking, audio, print, and display sub-systems.
Originally developed as a part of its eort to introduce Windows NT to the workstation market, Microsoft
released Windows NT 4.0, which features the new Windows 95 interface on top of the Windows NT kernel.
Windows NT (New Technology) is a family of operating systems produced by Microsoft, the rst version
of which was released in July 1993. It was originally designed to be a powerful high-level-language-based,
processor-independent, multiprocessing, multiuser operating system with features comparable to Unix. It
was intended to complement consumer versions of Windows that were based on MS-DOS. NT was the rst
fully 32-bit version of Windows, whereas its consumer-oriented counterparts, Windows 3.1x and Windows 9x,
were 16-bit/32-bit hybrids. Windows 2000, Windows XP, Windows Server 2003, Windows Vista, Windows
Server 2008 (beta), and Windows Home Server are based upon the Windows NT system, although they are
not branded as Windows NT.
Windows XP is the most popular version of Microsoft Windows. Windows provides a graphical interface,
through which you can run programs, manage les, connect to the internet, and perform many other task
as well.
Windows XP starts automatically when you turn on your computer. Depending on the way your PC is
currently set up, you may be prompted to select a user account when you start up your PC. Windows will
display a welcome screen, from which you click your user name and indicate who you are by entering your
password.
Once Windows XP has initialized, the following screen will appear.
Each user has his own ideas about what constitutes attractive screen colors, important shortcut to place
on the desktop etc. This combination can be saved as user prole and Windows remembers all the setting
and preferences.
Figure 1.27
When you nished using your PC, you shouldn't turn o the power because that could cause later problems
in Windows. Instead, you should use the Shut Down command on the Start menu (or press Ctrl+Esc if the
Start menu is invisible). This approach ensure that Windows shuts down in an orderly way that closes all
opened les and saves your work in any open program.
When shutting down, you have two options: Turn O and Restart. If you are probably to be away from
the computer, you will probably want to turn it o. If the computer is acting strangely and you want to
start fresh, you will want to refresh.
If for some reason, the computer is not ready to shut down , the computer will remind you in dialog
boxes.
The Icons
On the desktop we have icons that allow us to open the corresponding program.
For example, by clicking on the icon Internet Explorer will open up.
The windows
All the windows have the same structure;The window above is the one that opens when you click on My
Computer. Its structure is very similar to the others.
Figure 1.28
• The title bar contains the name of the program you are working with and in some cases the name of the
opened document also appears. In the top right corner we can nd the minimize, maximize/restore,
and close buttons.
• The minimize button shrinks the window it turns it into a button located in the WindowsXP
task bar.
• The maximize amplies the size of the window to the whole screen.
• The close button closes the window. If we have modied the document, we are asked if we want
to save the changes before closing.
The dialog box is a small window-like box that opens after an operation has been selected. In it, you
select options and settings to tailor the operation before it proceeds.
Figure 1.29
Text box : a control in which a user can enter texts (or numbers).
List box : a box that contains a list of selectable items. In some instances, you select an arrow button
on the right of the box in order to display the selectable items.
Combo box : a combination of a drop-down list or list box and a single-line textbox, allowing the user
either to type a value directly into the control or choose from the list of existing options.
Check box : a control that permits the user to make single selection or multiple selections from a
number of options. Normally, check boxes are shown on the screen as a square box that can contain white
space (for false) or a tick mark or X (for true).
Command Button: A control used to initiate an action. The most common buttons are :
• OK
• Close
• Cancel
• Apply
• Default
Use the mouse to interact with items on your screen as you would use your hands to interact with objects
in the physical world. You can move objects, open them, change them, or throw them away, among other
things.
A mouse has a primary and secondary mouse button. Use the primary mouse button to select and click
items, position the cursor in a document, and drag items.
Use the secondary mouse button to display a menu of tasks or options that change depending on where
you click. This menu is useful for completing tasks quickly. Clicking the secondary mouse button is called
right-clicking.
The primary mouse button is normally the left button on the mouse. On a trackball, the primary mouse
button is normally the lower button.
You can reverse the buttons and use the right mouse button as the primary button.Most mice now include
a wheel that helps you to scroll through documents more easily.
Pointing
Pointing at items on the screen is the most basic mouse function. When instructions tell you to point
your mouse at something, move your mouse on your desk until the mouse pointer is pointing at the object
on the screen you need to select.
Clicking
After you have pointed your mouse at an item, you can click on the item to select it.
Double clicking
To double-click an item, point at the item and press your primary button twice quickly without moving
the mouse. Double-clicking allows two dierent actions to be associated with the same mouse button.
Often, single-clicking selects (or highlights) an object, while a double-click executes that object, but this is
not universal.
Drag and drop
to move the item from one place to another using the mouse. Point at the item you need to move, and
single click on it. Instead of releasing the mouse button after clicking, hold it down, and move your mouse
to where you want to move the item. Release the mouse button to drop the item into place.
Right clicking
Right-clicking an item usually brings up a menu of actions you can take with the item. To right-click,
point at an item and press the secondary (right) button on your mouse.
Control Panel allows users to view and manipulate basic system settings and controls, such as adding
hardware, adding and removing software, controlling user accounts, and changing accessibility options.
To start the Control Panel, from the Start menu, click on Control Panel. Here is the Control Panel
window:
Figure 1.30
Conguring the screen is important because sometimes we spend many hours in front of the screen, so we
hope it can be the most comfortable as possible.
Open the Display Tool (or right-click somewhere that has no icons on the desktop and select the option
Properties from the shortcut menu that is displayed. The Display properties window will appear where we
can change the conguration parameters.
Figure 1.31
Sometimes the computer remains inactive a few minutes. It is recommended to have a screensaver to avoid
having a still image on the screen too long because this can damage the monitor.
From the list, choose the screensaver you like best; a small preview is shown above.
You can modify the time it takes for the screensaver to appear by adjusting the time on Wait.
The mouse is a tool that is used constantly and it is recommendable to have it set up to our needs as well
as possible. In the following page we show you how to set it up to your own needs.
The Buttons
On the Buttons tab you can adjust the set up of the mouse to suit your needs.If you are left handed.
WindowsXP allows you to change the conguration of the buttons so that the right button realizes these
functions.
We can also adjust the Double-click speed for a slower or a faster double-click.
The pointer
On the Pointers tab we can choose the type of pointer the mouse is to have when it moves, when it is
busy, when it is used, etc.
Figure 1.32
You can use the Regional and Language Options tool in Control Panel to customize the way Windows
handles dates, times, currency values, and numbers.
To open the Regional and Language Options tool
Figure 1.33
To Change the Date In the Customize Regional Options dialog box, click the Date tab to specify any changes
you want to make to the short date and the long date.
To Change the Time In the Customize Regional Options dialog box, click the Time tab to specify any
changes you want to make.
To Change the Currency Value Display
In the Customize Regional Options dialog box, click the Currency tab to specify any changes you want
to make. You can change the currency symbol, the formats used for positive or negative amounts, and the
punctuation marks.
To Change the Number Display
In the Customize Regional Options dialog box, click the Numbers tab to specify any changes you want
to make. You can change the decimal symbol and list separator, the format used for negative numbers and
leading zeros, and the measurement system (U.S. or metric).
The Explorer is an indispensable tool in an operating system, since with it we can organize and control the
les and folders of the dierent storage systems at our disposal such as the hard drive, disk drive, etc.
The Windows Explorer is also known as the File Manager. Through it we can delete, see, copy, or move
les and folders.
The quickest way to start up the Explorer is through the icon on the task bar or desktop. If you
don't already have the icon created, you can open the Explorer as follows:
• Click on Start [U+F0AE] Select All programs [U+F0AE] Select Accessories [U+F0AE] Select Windows
Explorer
• Right click on Start button and select Explore
• From the Start button, choose My documents, My images or My music; the dierence is that in these
cases we will go directly to those folders.
The explorer consists basically of two sections. On the left side there is the directory tree, which is the list
of units and folders that we have. Only units and folders appear, no les. On this image we can see a few
folders such as My Documents, aulaclic, ... the My Computer icon, My Network Places and the Recycle Bin.
Figure 1.34
On the right side there is another section, which will show the content of the folder that we have opened
on the left section. This section shows its folders and les. In this case the les that are contained in the
folder WinXP appear. Depending on the type of view that we have activated we will see dierent type of
information regarding the les.
Next we will explain the dierent bars that make up this window.
Figure 1.35
The standard bar contains the buttons for the most used operations.
If this bar is not visible select from the menu View, the option Toolbars, next select the option Standard
buttons.
The Back button will allow us to go to the last page that we have seen. The button
next to it, when activated, allows us to move one page forward.
The up button will allow us to go up one level, which means going back to the folder that contains
the folder we are working with.
The search button displays a window where we can search for the le we want.
The folders button shows the folder's structure on the left side of the screen, or it can display an
area with the most frequent tasks, depending on the le we have selected. In this area we can nd, among
others, the following buttons:
The last button allows us to change the views on the folders (view details, Thumbnails,...)
We'll explain this in more detailed on the next page.
Figure 1.36
The Address Bar is well known for Internet because it shows the address of the web we are viewing. With
Windows Explorer it functions the same way, but it shows the name of the folder we are working with.
If we click on the black arrow it will show the structure with our computer's drives.
If we write a name in the address bar and we click on the green arrow, it will search for this name.
Windows explorer allows us to see the folder's information in dierent ways or views to facilitate specic
searching.
Go to the folder you wish to see:
If you click on the arrow of the button a menu with the following options will appear:
Figure 1.37
Tiles. The les and folders are shown with large images with the name, le type and size in KB; if it is
a picture le the size is shown in pixels. The elements are organized one next to the other from left to right.
Icons. The les are represented with an icon smaller than a tile. The only information shown is the
name of the le. This type of icon is used when the selected folder has an average quantity of elements.
List. Shows small icons, one below the other, so it's easier to search by name. On this view, only the
name of the le or folder appears.
Details. Icons are shown one below the other, with some of their properties. This type of display is used
when we want to nd an element with certain characteristics, such as size, le type, date of modication,
etc.
With this type of view we can organize the elements by size, modication date, name, etc.
For example, to organize by the modication date it is enough to click on the box Date Modied, and it
will arrange the les by date from greater to lesser. If we click on it again it will arrange it from lesser to
greater. The older dates are considered lesser.
On the views List or Details the elements appear one below the other and in the case of deleting or
adding, the elements will reorganize themselves.
Thumbnails . A small representation of the content will appear with the format of the image, such as
jpg., jpeg., bmp., gif., etc.
Filmstrip. This view is only available for images. On the bottom part a strip will appear with the
images in thumbnail format and on the top we will see a larger representation of the image selected on the
bottom.
If you wish to select a single le or folder you simply need to click on it. This way any operation you perform
will only apply to the selected le or folder.
If you wish to realize an operation on several les or folders, Windows Explorer will allow you to select
several elements at the same time.
To select consecutive elements
Click on the rst element and then click on the last element while keeping Shift key pressed. This can also
be done with the mouse. To do this, click on the left of the rst element (but not on it) and, without letting
go, drag it. A frame should appear that shows the area that the frame encompasses. Continue dragging
until all the desired elements are within the frame, then let go of the left mouse button..
To select several elements that are not consecutive
Select the rst element and continue to select the desired elements while keeping the Ctrl key pressed.
To create a folder we need to place the pointer where we want the folder to be.Open the folders that we have
by clicking on the + located to the left of the folders.
If we click on the plus sign of a particular folder it will display and show all of the folders contained in it
and the plus sign will become a minus sign -; this will take care of retracting the folders displayed, or hide
the content of the folder selected.
Once we have the folder that we want open we will select it by clicking on the appropriate folder .Open
the menu File, select the option New and then select the option Folder.
Now we can view on the bottom right window a new folder that has the name New Folder. This is the
name that Windows gives new folders by default. In the event that it nds another folder with that same
name, it will subsequently name the new folders New Folder(1), New Folder(2), etc...
The name of the folder can be changed
Select the element to be copied.Click on Copy and it will open a dialog box titled Copy Items. If we do not
have this button on the tool bar, we can go to the Edit menu and select Copy to Folder... First select the
item to copy
Search for the folder to which we will copy the selected element. It works like Windows explorer. If we
click on the + that appears on the left, the contents of the folder will be displayed.
Once the folder has been selected, click on Copy.
In the case of not having the folder created to which we want to copy to, click Make new folder, write
the name of the new folder and Click OK.
Moving a le or folder means copying the element to the desired location and then deleting its original
location. The following steps are very similar.
• Click on , or Edit > Move to Folder which will open a new window titled Move Items.
• Search for the folder where the element are to be moved to.
• In the case of not having the folder created to which we want to move the information to, simply click
Make New Folder.
When moving or copying an item, its name can coincide with the name of a le or folder that is in the
destination folder. In this case Windows will ask if we want to substitute the existing le or folder by the
new one. When folder is moved or copied, its entire content is also moved or copied.
• Select the le or folder that you want to change the name of.
• Select Rename from the shortcut menu, then the name of the le or folder will be highlighted and with
the pointer blinking inside the name box.
• Click Enter or click outside the le or folder so that the changes take place.
• You can also do this with Rename option from File menu.
Both les and folders have their own characteristics, for example size, location, date of creation, attributes,
etc.
To know the characteristics of a particular le or folder we need to:
Click on the OK button to accept or the Cancel button to discard all changes.
Before Windows was created, the most common operating system that runs on PC was DOS. Though
Windows does not run on DOS, they do have something called the command prompt, which has a similar
appearance to DOS.
Figure 1.38
To use the command prompt you would type in the commands and instructions you want and press enter.
The recycle bin provides a safety net when deleting les and folders. When you delete any of these items
from your hard disk, Windows places it in the Recycle Bin. Items deleted from a oppy disk or a network
drive are permanently deleted and are not sent to the Recycle Bin.
Items in the Recycle Bin remain there until you decide to permanently delete them from your computer.
To delete or restore les in the Recycle Bin
On the desktop, double-click Recycle Bin.Do one of the following:
5
1.5 Computer Networks
• By network layer
• By scale
• By connection method
• By functional relationship
• By network topology
• By protocol
A Local Area Network (LAN) is a group of computers and associated devices that share a common com-
munications line and typically share the resources of a single processor or server within a small geographic
area
A Wide Area Network (WAN) is a computer network that spans a relatively large geographical area
(diameter of about 200 km)
GAN (Global Area Network) A network spanning a between geographically distinct cities
Server based (client/server):Computers set up to be primary providers of services such as le service or
mail service.
The computers providing the service are called servers
The computers that request and use the service are called client computers.
Peer-to-peer
Various computers on the network can act both as clients and servers.
Example Many Microsoft Windows based computers allow le and print sharing.
Many networks are combination peer-to-peer and server based networks.
• Computers: critical elements of any computer network. They can be considered nodes
• A network card, NIC (network interface card) is a piece of computer hardware designed to allow
computers to communicate over a computer network.
• Network media (sometimes referred to as networked media) refers to media mainly used in computer
networks : cable, telephone line or wireless.
• Network connection equipments : HUB, SWITCH, ROUTER,etc.
• Network Operating System (NOS) is an operating system that includes special functions for connecting
computers and devices into a local-area network (LAN) or Inter-networking. Some popular NOSs for
DOS and Windows systems include Novell NetWare, Windows NT and 2000, Sun Solaris and IBM
OS/2.
• Network software.
• Network services, for example email.
Network topology is the arrangement or mapping of the elements (links, nodes, etc.) of a network, especially
the physical (real) and logical (virtual) interconnections between nodes .
There are three basic categories of network topologies:
• Physical topologies
• Signal topologies
• Logical topologies
The simplest topology is a permanent link between two endpoints. Switched point-to-point topologies are
the basic model of conventional telephony. The value of a permanent point-to-point network is the value of
guaranteed, or nearly so, communications between the two endpoints. The value of an on-demand point-to-
point connection is proportional to the number of potential pairs of subscribers.
1.5.3.3 Bus
Bus networks (not to be confused with the system bus of a computer) use a common backbone to connect all
devices. A single cable, the backbone functions as a shared communication medium that devices attach or
tap into with an interface connector. A device wanting to communicate with another device on the network
sends a broadcast message onto the wire that all other devices see, but only the intended recipient actually
accepts and processes the message.
1.5.3.4 Ring
In a ring network, every device has exactly two neighbors for communication purposes. All messages travel
through a ring in the same direction (either "clockwise" or "counterclockwise"). A failure in any cable or
device breaks the loop and can take down the entire network.
The history of the Internet dates back to the early development of communication networks. The idea
of a computer network intended to allow general communication between users of various computers has
developed through a large number of stages. The melting pot of developments brought together the network
of networks that we know as the Internet. This included both technological developments, as well as the
merging together of existing network infrastructure and telecommunication systems.
The earliest versions of these ideas appeared in the late 1950s. Practical implementations of the concepts
began during the late 1960s and 1970s. By the 1980s, technologies we would now recognize as the basis of
the modern Internet began to spread over the globe. In the 1990s the introduction of the World Wide Web
saw its use become commonplace.
The Internet or the World Wide Web is indeed a wonderful and amazing addition in our lives. The Internet
can be known as a kind of global meeting place where people from all parts of the world can come together.
The major advantages of the internet are:
• E-mail: E-mail is an online correspondence system. With e-mail you can send and receive instant
electronic messages, which works like writing letters.
• Access Information: The Internet is a virtual treasure trove of information. The `search engines' on
the Internet can help you to nd data on any subject that you need.
• Shopping: Along with getting information on the Internet, you can also shop online. There are many
online stores and sites that can be used to look for products as well as buy them using your credit card
• Online Chat: There are many `chat rooms' on the web that can be accessed to meet new people, make
new friends, as well as to stay in touch with old friends.
• Downloading Software: You can download innumerable, games, music, videos, movies, and a host of
other entertainment software from the Internet, most of which are free.
Before you can connect to the Internet and access the World Wide Web, you need to have certain equipment:
• Small size
• Extensive use of function calls
• Loose typing unlike PASCAL
• Structured language
• Low level (BitWise) programming readily available
• Pointer implementation - extensive use of pointers for memory, array, structures and functions.
C has now become a widely used professional language for various reasons.
Its main drawback is that it has poor error detection which can make it o putting to the beginner. However
diligence in this matter can pay o handsomely since having learned the rules of C we can break them. Not
many languages allow this. This if done properly and carefully leads to the power of C programming.
C's power and exibility soon became apparent. Because of this, the Unix operating system which was
originally written in assembly language, was almost immediately re-written in C ( only the assembly language
code needed to "bootstrap" the C code was kept ). During the rest of the 1970's, C spread throughout many
colleges and universities because of it's close ties to Unix and the availability of C compilers. Soon, many
dierent organizations began using their own versions of C causing compatibility problems. In response to
65
66 CHAPTER 2. THE C PROGRAMMING LANGUAGES
this in 1983, the American National Standards Institute ( ANSI ) formed a committee to establish a standard
denition of C which became known as ANSI Standard C. Today C is in widespread use with a rich standard
library of functions.
Select the File menu to create a new le or open an existing le to edit.
Figure 2.2
Choose a le to open. This screen allow you to change to your directory.
Figure 2.4
To exit from IDE, select the le menu, choose quit or press Alt+X
A keyword is an identier which indicate a specic command. Keywords are also considered reserved words.
You shouldn't use them for any other purpose in a C program.
The most important keywords of Turbo C are
while
Table 2.1
2.1.3.3 Identiers
Identiers or names refer to a variety of things : functions; tag of structures, union and enumerations; member
of structures or unions; enumeration constants; typedef names and objects. There are some restrictions on
the names .
Names are made up of letters and digit; The rst character must be a letter. The underscore _ count
as a letter; sometime it is useful for improving the readability of long variable names. For example, name
unit_price is easier to understand than unitprice. However, don't begin variable names with underscore,
since library routines often use such names.
Upper and lower case are distinct, so x and X are dierent names. Traditional C practice use lower case
for variable names, and all upper case for symbolic constant.
Only the rst 31 characters are signicant.
Keywords are reserved: you can't use them as variable names.
Example The following names are valid
i, x, b55, max_val
and the following names are invalid
Table 2.2
It is wise to choose variable names that are related to the purpose of the variable, for example,
count_of_girls, MAXWORD.
Data is valuable resources of computers. Data may comprise numbers, text, images . . .
They belong to dierent data types.
In programming languages, a data type is a set of values and the operations on those values.
For example, int type is the set of 32-bit integers within the range -2,147,483,648 to 2,147,483,647 together
with the operations described in the following table.
Operations Symbol
Opposite -
Addition +
Subtraction -
Multiplication *
Division /
Modulus %
Equal to = =
Table 2.3
A data type can also be thought of as a constraint placed upon the interpretation of data in a type system
in computer programming.
Common types of data in programming languages include primitive types (such as integers, oating point
numbers or characters), tuples, records, algebraic data types, abstract data types, reference types, classes
and function types. A data type describes representation, interpretation and structure of values manipulated
by algorithms or objects stored in computer memory or other storage device. The type system uses data
type information to check correctness of computer programs that access or manipulate the
data.
2.1.3.5 Constants
In general, a constant is a specic quantity that does not or cannot change or vary. A constant's value is
xed at compile-time and cannot change during program execution. C supports three types of constants :
numeric, character, string.
Numeric constants of C are usually just the written version of numbers. For example 1, 0, 56.78, 12.3e-4.
We can specify our constant in octal or hexadecimal, or force them to be treated as long integers.
Character constants are usually just the character enclosed in single quotes; `a', `b', `c'. Some characters
can't be represented in this way, so we use a 2 character sequence (escape sequence).
`\n' newline
`\b' backspace
`\\' backslash
Table 2.4
Character constants participate in numeric operations just as any other integers (they are represented
by their order in the ASCII character set), although they are most often used in comparison with other
characters.
Character constants are rarely used, since string constants are more convenient. A string constant is a
sequence of characters surrounded by double quotes e.g. Brian and Dennis.
A character is a dierent type to a single character string. This is important.
It is helpful to assign a descriptive name to a value that does not change later in the program. That is
the value associated with the name is constant rather than variable, and thus such a name is referred to as
symbolic constant or simply a constant.
2.1.3.6 Variables
Variables are the names that refer to sections of memory into which data can be stored.
Let's imagine that memory is a series of dierent size boxes. The box size is memory storage area
required in bytes.In order to use a box to store data, the box must be given a name, this process is known
as declaration. It helps if you give a box a meaningful name that relates to the type of information and it
is easier to nd the data.The boxes must be of the correct size for the data type you are going to put into
it. An integer number such as 2 requires a smaller box than a oating point number as 123e12.
Data is placed into a box by assigning the data to the box. By using the name of the box you can retrieve
the box contents, some kind of data.
Variable named by an identier. The conventions of identiers were shown in 1.3.3.
Names should be meaningful or descriptive, for example, studentAge or student_age is more meaningful
than age, and much more meaniful than a single letter such as a.
2.1.3.7 Operators
Programming languages have a set of operators that perform arithmetical operations , and others such
as Boolean operations on truth values, and string operators manipulating strings of text. Computers are
mathematical devices , but compilers and interpreters require a full syntactic theory of all operation in order
to parse formulae involving any combination correctly.
2.1.3.8 Expressions
C expressions are arranged in the following groups based on the operators they contain and how you use
them:
• Arithmetic expression
• Conditional expression
• Assignment expression
• Comma expression
• lvalue
• Constant expression
2.1.3.9 Functions
A subprogram (also known as a procedure or subroutine) is nothing more than a collection of instructions
forming a program unit written independently of the main program yet associated with it through a trans-
fer/return process. Control is passed to the subprogram at the time its services are required, and then
control is returned to the main program after the subprogram has nished.
The syntax used to represent the request of subprogram varies among the dierent language. The
techniques used to describe a subprogram also varies from language to language. Many systems allow such
program units to be written in languages other than that of the main program.
In most procedural programming languages, a subprogram is implemented as though it were completely
separate entity with its own data and algorithm so that an item of data in either the main program or the
subprogram is not automatically accessible from within the other. With this arrangement, any transfer of
data between the two program parts must be specied by the programmer. This is usually done by listing the
items called parameters to be transferred in the same syntactic structure used to request the subprogram's
execution.
The names used for the parameters within the subprogram can be thought of as merely standing in for
the actual data values that are supplied when the subprogram is requested. As a result, you often hear them
called formal parameters, whereas the data values supplied from the main program are refereed to actual
parameters.
Figure 2.6
C only accept one kind of subprogram, function. A function is a sub program in which input values
are transferred through a parameter list. However, information is returned from a function to the main
program in the form of the value of the function. That is the value returned by a function is associated
with the name of the function in a manner similar to the association between a value and a variable name.
The dierence is that the value associated with a function name is computed (according to the function's
denition) each time it is required, whereas when a variable `s value is required, it is merely retrieve from
memory.
C also provide a rich collection of built-in functions.There are more than twenty functions declared in
<math.h>. Here are some of the more frequently used.
ceil(x) smallest integer not less than parameter dxe ceil(2.5) is 3; ceil(-2.5) is 2
oor(x) largest integer not greater than parameter bxc oor(2.5) is 2; oor(-2.5) is 3
Table 2.5
Library
The library is not part of the C language proper, but an environment that support C will provide the
function declarations and type and macro denitions of this library.The functions, types and macro of the
library are declared in headers.
C header les have extensions .h. Header les should not contain any source code. They are used purely
to store function prototypes, common #dene constants, and any other information you wish to export from
the C le that the header le belongs to.
A header can be accessed by
#include <header>
Here are some headers of Turbo C library
stdio.h Provides functions for performing input and output.
stdlib.h Denes several general operation functions and macros.
conio.h Declares several useful library functions for performing "console input and output" from a
program.
math.h Denes several mathematic functions.
string.h Provides many functions useful for manipulating strings (character arrays).
io.h Denes the le handle and low-level input and output functions
graphics.h Includes graphics functions
2.1.3.10 Statements
A statement species one or more action to be perform during the execution of a program.
C requires a semicolon at the end of every statement.
2.1.3.11 Comments
Comments are marked by symbol /* and */. C also use // to mark the start of a comment and the end
of a line to indicate the end of a comment.
Example 2.1
By the rst way, a program may have a multi-line comments and comments in the middle of a line of
code.However, you shouldn't mix the two style in the same program.
Figure 2.7
2
2.2 Data Types and Expressions
Table 2.6
Variables
A variable is an object of a specied type whose value can be changed. In programming languages, a
variable is allocated a storage location that can contain data that can be modied during program execution.
Each variable has a name that uniquely identies it within its level of scope.
In C, a variable must be declared before use, although certain declarations can be made implicitly by
content. Variables can be declared at the start of any block of code, but most are found at the start of each
function. Most local variables are created when the function is called, and are destroyed on return from that
function.
A declaration begins with the type, followed by the name of one or more variables. Syntax of declare
statement is described as:
data_type list_of_variables;
A list of variables includes one or many variable names separated by commas.
Example 2.2
Single declarations
Variables can also be initialized when they are declared, this is done by adding an equals sign and the
required value after the declaration.
Example 2.3
Constants
A constant is an object whose value cannot be changed. There are two method to dene constant in C:
Usually i/o, input and output, form the most important part of any program. To do anything useful your
program needs to be able to accept input data and report back your results. In C, the standard library
(stdio.h) provides routines for input and output. The standard library has functions for i/o that handle
input, output, and character and string manipulation. Standard input is usually means input using the
keyboard. Standard output is usually means output onto the monitor.
To use printf and scanf functions, it is required to declare the header <stdio.h>
The printf() function
The standard library function printf is used for formatted output. It makes the user input a string and
an optional list of variables or strings to output. The variables and strings are output according to the
specications in the printf() function. Here is the general syntax of printf .
printf(``[string]''[,list of arguments]);
The list of arguments allow expressions, separated by commas.
The string is all-important because it species the type of each variable in the list and how you want it
printed. The string is usually called the control string or the format string. The way that this works is that
printf scans the string from left to right and prints on the screen any characters it encounters - except when
it reaches a % character.
The % character is a signal that what follows it is a specication for how the next variable in the list of
variables should be printed. printf uses this information to convert and format the value that was passed
to the function by the variable and then moves on to process the rest of the control string and anymore
variables it might specify.
For Example
printf("Hello World");
only has a control string and, as this contains no % characters it results in Hello World being displayed
and doesn't need to display any variable values. The specier %d means convert the next value to a signed
decimal integer and so:
printf("Total = %d",total);
will print Total = and then the value passed by total as a decimal integer.
The % Format Speciers
The % speciers that you can use are:
Table 2.7
flag width.precision
The ag can be any of
ag meaning
- left justify
Table 2.8
The width species the number of characters used in total to display the value and precision indicates
the number of characters used after the decimal point.
For example,
%10.3f will display the oat using ten characters with three digits after the decimal point. Notice that
the ten characters includes the decimal point, and a - sign if there is one. If the value needs more space than
the width species then the additional space is used - width species the smallest space that will be used to
display the value.
%-10d will display an int left justied in a ten character space.
The specier %+5d will display an int using the next ve character locations and will add a + or - sign
to the value.
Strings will be discussed later but for now remember: if you print a string using the %s specier then all
of the characters stored in the array up to the rst null will be printed. If you use a width specier then the
string will be right justied within the space. If you include a precision specier then only that number of
characters will be printed.
For Example
printf("%s,Hello")
will print Hello,
printf("%25s ,Hello")
will print 25 characters with Hello right justied
Also notice that it is ne to pass a constant value to printf as in printf("%s,Hello").
Finally there are the control codes:
\b backspace
\f formfeed
\n new line
\r carriage return
\t horizontal tab
\0 null
Table 2.9
If you include any of these in the control string then the corresponding ASCII control code is sent to the
screen, or output device, which should produce the eect listed. In most cases you only need to remember
\n for new line.
The scanf() function
The scanf function works in much the same way as the printf. That is it has the general form:
scanf(``control string'',variable,variable,...)
In this case the control string species how strings of characters, usually typed on the keyboard, should be
converted into values and stored in the listed variables. However there are a number of important dierences
as well as similarities between scanf and printf.
The most obvious is that scanf has to change the values stored in the parts of computers memory that
is associated with parameters (variables).
To understand this fully you will have to wait until we have covered functions in more detail. But, just
for now, bare with us when we say to do this the scanf function has to have the addresses of the variables
rather than just their values. This means that simple variables have to be passed with a preceding &.
The second dierence is that the control string has some extra items to cope with the problems of reading
data in. However, all of the conversion speciers listed in connection with printf can be used with scanf.
The rule is that scanf processes the control string from left to right and each time it reaches a specier
it tries to interpret what has been typed as a value. If you input multiple values then these are assumed to
be separated by white space - i.e. spaces, newline or tabs. This means you can type:
3 4 5
or
3
4
5
and it doesn't matter how many spaces are included between items. For Example
scanf("%d %d",&i,&j);
will read in two integer values into i and j. The integer values can be typed on the same line or on dierent
lines as long as there is at least one white space character between them.
The only exception to this rule is the %c specier which always reads in the next character typed no
matter what it is. You can also use a width modier in scanf. In this case its eect is to limit the number
of characters accepted to the width.
For Example
scanf("%l0d",&i)
would use at most the rst ten digits typed as the new value for i.
Here is an example to demonstrate the usage of printf and scanf functions
#include <conio.h>
#include <stdio.h>
void main()
{
// variable declaration
int a;
float x;
char ch;
char* str;
// Enter data
printf(``Input an integer'');
scanf(``%d'',&a);
printf(``\n Input a real number'');
scanf(``%f'',&x);
printf(``\n Input a character'');
fflush(stdin); scanf(``%c'',&ch);
printf(``\n Input a string'' );
fflush(stdin); scanf(``%s'',str);
// Output the data
printf(``\n Your data'');
printf(``\n Integer: %d'',a);
printf(``\n Float : %.2f'',x);
printf(``\n Char: %c:,ch);
printf(``\n String : %s'',str);
}
(Function ush are used to avoid stopping the reading process when meet one or more spaces)
There is one main problem with scanf function which can make it unreliable in certain cases. The reason
being is that scanf tends to ignore white spaces, i.e. the space character. If you require your input to contain
spaces this can cause a problem.
Scanf will skip over white space such as blanks, tabs and new lines in the input stream. The exception
is when trying to read single characters with the conversion speciers %c. In this case, white space is read
in. So, it is more dicult to use scanf for single characters. An alternate technique, using getchar, will be
described later.
getch
The computer asks the user press a key. This key does not appear on screen. The syntax is:
getch();
getch() is used to terminate a program when the user press a key.
gets
gets reads a line of input into a character array. The syntax is:
gets(name_of_string);
puts
puts writes a line of output to standard output. The syntax is:
puts(name of string);
It terminates the line with a new line, '\n'. It will return EOF is an error occurred. It will return a positive
number on success.
For using the statements mentioned above, you must declare the library <conio.h>.
2.2.2 Expressions
2.2.2.1 Operators
• Arithmetic
• Assignment
• Logical/relational
• Bitwise
The arithmetic operators are +, -, /, * and the modulus operator %. Integer division truncates any fractional
part. The expression x%y produces the remainder when x is divided by y, and thus is zero when y divide x
exactly.The % operator cannot be applied to a oat or double.
The binary + and operators have the same precedence, which is lower than the precedence of *, / and
%, which is turn lower than unary + and - . Arithmetic operators associate left to right.
int a, b;
-12; -a; -25.6;
float x, y;
5 + 8; a + x;
3.6 + 2.9;
3 -- 1.6; a -- 5;
a * b; b * y;
2.6 * 1.7;
10.0/3.0;
(= 3.33. . .)
10/3.0; (= 3.33. . .)
10.0/3; (= 3.33. . .)
10/3; (= 3)
% modulus integer
10%3; (=1)
Table 2.10
These all perform an arithmetic operation on the lvalue and assign the result to the lvalue. So what does
this mean in English? Here is an Example
counter = counter + 1;
can be reduced to
counter += 1;
Here is the full set.
*= Multyply
/= Divide
%= Modulus
+= Add
-= Subtract
= Left Shift
= Right Shift
|= Bitwise Ixclusive OR
Table 2.11
Relational operators
2 > 3 (is 0)
6 > 4 (is 1)
a > b
6 >= 4 (is 1)
x >= a
5 <= 5 (is 1)
2 <= 9 (is 1)
== equal to
3 == 4 (is 0)
a == b
!= not equal to
5 != 6 (is 1)
6 != 6 (is 0)
Table 2.12
Logical operators
6 || 0 (is 1)
3<2 || 3<3 (is 1)
x >= a || x == 0
!3 (is 0)
!(2>5) (is 1)
Table 2.13
0 & 0 (is 0)
0 & 1 (is 0)
1 & 0 (is 0)
1 & 1 (is 1)
101 & 110 (is 100)
0 | 0 (is 0)
0 | 1 (is 0)
1 | 0 (is 0)
1 | 1 (is 1)
101 | 110 (is 111)
0 ^ 0 (is 0)
0 ^1 (is 1)
1 ^ 0 (is 1)
1 ^ 1 (is 0)
101 ^ 110 (is 011)
a n (is a*2n)
101 2 (is 10100)
a n (is a/2n)
101 2 (is 1)
∼ 0 (is 1)
∼ 1 (is 0)
∼ 110 (is 001)
Table 2.14
Incrementing, decrementing and doing calculations on a variable is a very common programming task and
C has quicker ways of writing the code. The code is rather cyptic in appearance.
The increment operator ++ adds 1 to its operand while the decrement operator - -subtract 1. We have
frequently used ++ to increment variables, as in
if (c = = '\n')
++n;
The unusual aspect is that ++ and - - may be used either as prex operators (before the variable, as in
++n) or postx operators (after the variable, as in n++). In both cases, the eect is to increment n. But
the expression ++n increments n before its value is used, while n++ increment n after its value has been
used. This mean that in a context where the value is being used, not just the eect, ++n and n++ are
dierent. For example, if n is 5, then
x = n++;
sets x to 5 but
x = ++n;
sets x to 6. In both cases, n becomes 6.
note: The increment and decrement operator can only be applied to variables; an expression like
(i + j)++ is illegal.
Table 2.15
When an operator has operands of dierent types, they are converted to a common type according to a small
number of rules. In general, the only automatic conversion era those that convert a narrower operand into
a wider one without loosing information, such as converting an integer into oating point .
If there are no unsigned operands, the following informal set of rules will suce:
If either operand is long double, convert the other to long double.
Otherwise, if either operand is double, convert the other to double.
Otherwise if either operand is oat, convert the other to oat.
Otherwise convert char and short to int.
Then if either operand is long, convert the other to long.
A char is just a small integer, so chars may be freely used in arithmetic expressions
4 + - >
5 >
7 == != >
8 & >
9 ^ >
10 | >
11 && >
12 || >
13 ?: <
14 = += -= <
Table 2.16
4
2.3 The Control Flow
The control ow of a language specify the order in which operations are performed. Each program in-
cludes many statements. Statements are processed one after another in sequence, except where such control
statements result in jumps.
x=0;
i++;
printf(. . . .);
In the C language, the semicolon is a statement terminator.
A block also called a compound statement, or compound statement, lets you group any number of data
denitions, declarations, and statements into one statement. All denitions, declarations, and statements
enclosed within a single set of braces are treated as a single statement. You can use a block wherever a single
statement is allowed.
In blocks, declarations and denitions can appear anywhere, mixed in with other code. Note that there
is no semicolon after the right brace that ends a block.
Example 2.6
{ into I = 0; /* Declarations */
static long a;
extern long max;
++a; /* Statements */
if( a >= max)
{ . . . } /* A nested block */
. . .
}
[expression] ;
Example 2.7
y = x; // Assignment
The expressionan assignment or function call, for exampleis evaluated for its side eects. The type and
value of the expression are discarded.
A statement consisting only of a semicolon is called an empty statement, and does not perform any
operation. For Example
if(expression) statement
and
if(expression) statement1
else statement2
In the rst form, if (and only if ) the expression is non-zero, the statement is executed. If the expression
is zero, the statement is ignored. Remember that the statement can be compound; that is the way to put
several statements under the control of a single if.
The second form is like the rst except that if the statement shown as statement1 is selected then
statement2 will not be, and vice versa.
Here are the owcharts of the two forms of if statement
Figure 2.8
The form involving else works the same way, so we can also write this.
if(expression)
if(expression)
statement
else
statement
this is now ambiguous. It is not clear, except as indicated by the indentation, which of the ifs is responsible
for the else. If we follow the rules that the previous example suggests, then the second if is followed by a
statement, and is therefore itself a statement, so the else belongs to the rst if.
That is not the way that C views it. The rule is that an else belongs to the rst if above that hasn't
already got an else. In the example we're discussing, the else goes with the second if.
To prevent any unwanted association between an else and an if just above it, the if can be hidden away
by using a compound statement, here it is.
if(expression){
if(expression)
statement
}else
statement
if(expression){
if(expression){
if(expression){
statement
}
}else{
statement
}
Example
Example 2.8
#include <conio.h>
#include <stdio.h>
void main()
{
// variable declaration
float a, b;
float max;
printf(`` Enter the values of a and b: ``);
scanf(``%f %f'',&a,&b);
if(a<b) //Assign the greater of x and y to the variable max
max = b;
else
max = a;
printf(``\n The greater of two numbers %.0f and %.0f is %.0f ``,a,b,max);
getch();
}
Figure 2.9
switch (expression){
case const1: statements
case const2: statements
. . . .
default: statements
}
Figure 2.10
The expression is evaluated and its value is compared with all of the const etc. expressions, which must
all evaluate to dierent constant values (strictly they are integral constant expressions). If any of them
has the same value as the expression then the statement following the case label is selected for execution.
If the default is present, it will be selected when there is no matching value found. If there is no default
and no matching value, the entire switch statement will do nothing and execution will continue at the next
statement.
Example 2.9
OK=1;
switch (OP)
{
case `+':
z=x+y;
break;
case `-':
z=x-y;
break;
case `*':
z=x*y;
break;
case '/':
if (y!=0 )
z=x/y;
else OK=0;
default :
OK=0;
}
• The switch requires an integer-compatible value. This value may be a constant, variable, function call,
or expression. The switch statement does not work with oating point data types.
• The value after each case label must be a constant.
• C++ does not support case label with ranges of values. Instead, each value must appear in a separate
case label.
• You need to use a break statement after each set of executable statements. The break statement causes
program execution to resume after the end of the current switch statement. If you do not use the break
statement, the program execution resumes at subsequent case labels.
• The default clause is a catch-all clause.
• The set of statements in each case or grouped case labels need not be enclosed in open and close braces.
The following program writes out the day of the week depending on the value of an integer variable day. It
assumes that day 1 is Sunday.
#include <stdio.h>
#include <conio.h>
void main()
{int day;
printf(``Enter the value of a weekday'');
scanf(``%d'',&day);
switch (day)
{
case 1 : printf( "Sunday");
break;
case 2 : printf( "Monday");
break;
case 3 : printf( "Tuesday");
break;
case 4 : printf("Wednesday");
break;
case 5 : printf("Thursday");
break;
case 6 : printf("Friday");
break;
case 7 : printf("Saturday");
break;
default : printf("Not an allowable day number");
break;
}
getch();
}
If it has already been ensured that day takes a value between 1 and 7 then the default case may be missed
out. It is allowable to associate several case labels with one statement. For example if the above example is
amended to write out whether day is a weekday or is part of the weekend:
switch (day)
{
case 1 :
case 7 : printf( "This is a weekend day");
break;
case 2 :
case 3
:
case 4
:
case 5
:
case 6
: printf( "This is a weekday");
break;
default : printf( "Not a legal day");
break;
}
while(expression)
statement
Figure 2.11
The statement is only executed if the expression is non-zero. After every execution of the statement, the
expression is evaluated again and the process repeats if it is non-zero. What could be plainer than that?
The only point to watch out for is that the statement may never be executed, and that if nothing in the
statement aects the value of the expression then the while will either do nothing or loop for ever, depending
on the initial value of the expression.
Example 2.10
#include <stdio.h>
#include <stdlib.h>
main(){
int i;
/* initialize */
i = 0;
/* check */
while(i <= 10){
printf("%d\n", i);
/* update */
i++;
}
}
It is occasionally desirable to guarantee at least one execution of the statement following the while, so an
alternative form exists known as the do statement. It looks like this:
do
statement
while(expression);
Figure 2.12
and you should pay close attention to that semicolonit is not optional! The eect is that the statement
part is executed before the controlling expression is evaluated, so this guarantees at least one trip around
the loop. It was an unfortunate decision to use the keyword while for both purposes, but it doesn't seem to
cause too many problems in practice.
A very common feature in programs is loops that are controlled by variables used as a counter. The counter
doesn't always have to count consecutive values, but the usual arrangement is for it to be initialized outside
the loop, checked every time around the loop to see when to nish and updated each time around the loop.
There are three important places, then, where the loop control is concentrated: initialize, check and update.
This example shows them.
As you will have noticed, the initialization and check parts of the loop are close together and their location
is obvious because of the presence of the while keyword. What is harder to spot is the place where the update
occurs, especially if the value of the controlling variable is used within the loop. In that case, which is by far
the most common, the update has to be at the very end of the loop: far away from the initialize and check.
Readability suers because it is hard to work out how the loop is going to perform unless you read the whole
body of the loop carefully. What is needed is some way of bringing the initialize, check and update parts
into one place so that they can be read quickly and conveniently. That is exactly what the for statement is
designed to do. Here it is.
Figure 2.13
The rst expression (expression1) is the initialize part; nearly always an assignment expression which is used
to initialize the control variable. After the initialization, the check expression (expression2) is evaluated: if it
is non-zero, the statement is executed, followed by evaluation of the update expression (expression3) which
generally increments the control variable, then the sequence restarts at the check. The loop terminates as
soon as the check evaluates to zero.
There are two important things to realize about that last description:
one, that each of the three parts of the for statement between the parentheses are just expressions;
two, that the description has carefully explained what they are intended to be used for without proscribing
alternative usesthat was done deliberately. You can use the expressions to do whatever you like, but at
the expense of readability if they aren't used for their intended purpose.
Here is a program that does the same thing twice, the rst time using a while loop, the second time with
a for. The use of the increment operator is exactly the sort of use that you will see in everyday practice.
Example 2.11
#include <stdio.h>
#include <stdlib.h>
void main(){
int i;
/* the same done using ``for'' */
for(i = 0; i <= 10; i++){
printf("%d\n", i);
}
}
There isn't any dierence between the two, except that in this case the for loop is more convenient and
maintainable than the while statement. You should always use the for when it's appropriate; when a loop
is being controlled by some sort of counter. The while is more at home when an indeterminate number of
cycles of the loop are part of the problem.
Any of the initialize, check and update expressions in the for statement can be omitted, although the
semicolons must stay. This can happen if the counter is already initialized, or gets updated in the body
of the loop. If the check expression is omitted, it is assumed to result in a `true' value and the loop never
terminates. A common way of writing never-ending loops is either
for(;;)
or
while(1)
and both can be seen in existing programs.
This is a simple statement. It only makes sense if it occurs in the body of a switch, do, while or for
statement. When it is executed the control of ow jumps to the statement immediately following the body
of the statement containing the break. Its use is widespread in switch statements, where it is more or less
essential to get the control that most people want.
The use of the break within loops is of dubious legitimacy. It has its moments, but is really only justiable
when exceptional circumstances have happened and the loop has to be abandoned. It would be nice if more
than one loop could be abandoned with a single break but that isn't how it works. Here is an example
#include <stdio.h>
#include <stdlib.h>
main(){
int i;
This statement has only a limited number of uses. The rules for its use are the same as for break, with the
exception that it doesn't apply to switch statements. Executing a continue starts the next iteration of the
smallest enclosing do, while or for statement immediately. The use of continue is largely restricted to the
top of loops, where a decision has to be made whether or not to execute the rest of the body of the loop. In
this example it ensures that division by zero (which gives undened behavior) doesn't happen
#include <stdio.h>
#include <stdlib.h>
main(){
int i;
goto L1;
/* whatever you like here */
L1: /* anything else */
A label is an identier followed by a colon. Labels have their own `name space' so they can't clash with
the names of variables or functions. The name space only exists for the function containing the label, so
label names can be re-used in dierent functions. The label can be used before it is declared, too, simply by
mentioning it in a goto statement.
Labels must be part of a full statement, even if it's an empty one. This usually only matters when you're
trying to put a label at the end of a compound statementlike this.
5
2.4 Pointers and Arrays
From the beginning, we only show how to access or change directly the values of variables through their
names. However, the C language provides the developers an eective method to access variables - pointer.
A pointer is a variable that contains the address of a variable. Pointers are much used in C, partly
because they are sometimes the only way to express a computation, and partly because they usually lead to
more compact and ecient code than can be obtained in other ways. Pointers and arrays are closely related;
this Unit also explores this relationship and shows how to exploit it.
int i, j;
i = 3;
j = i;
Type of these two variables is integer so they are stored in 2-byte memory area. Suppose that the compiler
allocates i at the FFEC address in memory and j in FFEE, we have:
i FFEC 3
j FFEE 3
Table 2.17
Two dierent variables have dierent addresses. The i = j assignment aects only on the value of variables,
that means the content of the memory area for j will be copied to the content of the memory area for i.
2.4.1.1 Pointers
A pointer is a group of cells (often two or four) that can hold an address. So if c is a char and p is a pointer
that points to it, we could represent the situation this way:
Figure 2.14
If you declare a variable, its name is a direct reference to its value. If you have a pointer to a variable or any
other object in memory, you have an indirect reference to its value. A pointer variable stores the address of
another object or a function. To start out, the declaration of a pointer to an object that is not an array has
the following syntax:
It is often useful to output addresses for verication and debugging purposes. The printf() functions
provide a format specier for pointers: %p. The following statement prints the address and contents of the
variable iPtr:
The unary operator & gives the address of an object, so the statement
p = &c;
assigns the address of c to the variable p, and p is said to point to c. The & operator only applies to
objects in memory: variables and array elements. It cannot be applied to expressions, constants, or register
variables.
* is the operator that retrieves the value stored at the address held in the pointer. The indirection
operator * yields the location in memory whose address is stored in a pointer. If ptr is a pointer, then *ptr
designates the object that ptr points to. Using the indirection operator is sometimes called dereferencing
a pointer. The type of the pointer determines the type of object that is assumed to be at that location in
memory. For example, when you access a given location using an int pointer, you read or write an object of
type int.
The indirection operator * is a unary operator; that is, it has only one operand.ptr points to the variable
x. Hence the expression *ptr is equivalent to the variable x itself.
Example 2.12
Do not confuse the asterisk (*) in a pointer declaration with the indirection operator. The syntax of the
declaration can be seen as an illustration of how to use the pointer.
double *ptr;
As declared here, ptr has the type double * (read: "pointer to double"). Hence the expression *ptr would
have the type double.
Of course, the indirection operator * must be used with only a pointer that contains a valid address.
This usage requires careful programming! Without the assignment ptr = &x in the listing above, all of the
statements containing *ptr would be senseless dereferencing an undened pointer value and might well cause
the program to crash.
Since pointers are variables, they can be used without dereferencing. Pointer assignment between two
pointers makes them point to the same pointee. So the assignment iq = ip; copies the contents of ip into
iq, thus making iq point to whatever ip pointed to. It makes iq point to the same pointee as ip. Pointer
assignment does not touch the pointees. It just changes one pointer to have the same reference as another
pointer. After pointer assignment, the two pointers are said to be "sharing" the pointee.
Example Consider the following programs:
main()
{
int i = 3, j = 6;
int *p1, *p2;
p1 = &i;
p2 = &j;
*p1 = *p2;
}
and
main()
{
int i = 3, j = 6;
int *p1, *p2;
p1 = &i;
p2 = &j;
p1 = p2;
}
Suppose the values of variables before executing the last assignment are
i FFEC 3
j FFEE 6
p1 FFDA FFEC
p2 FFDC FFEE
Table 2.18
i FFEC 6
j FFEE 6
p1 FFDA FFEC
p2 FFDC FFEE
Table 2.19
i FFEC 3
j FFEE 6
p1 FFDA FFEE
p2 FFDC FFEE
Table 2.20
Pointer variables with automatic storage duration start with an undened value, unless their declaration
contains an explicit initializer. You can initialize a pointer with the following kinds of initializers:
Besides using assignments to make a pointer refer to a given object or function, you can also modify an object
pointer using arithmetic operations. When you perform pointer arithmetic, the compiler automatically
adapts the operation to the size of the objects referred to by the pointer type.
You can perform the following operations on pointers to objects:
If ip points to the integer x, then *ip can occur in any context where x could, so
y = *ip + 1
takes whatever ip points at, adds 1, and assigns the result to y, while
*ip += 1
increments what ip points to, as do
++*ip
and
(*ip)++
The parentheses are necessary in this last example; without them, the expression would increment ip instead
of what it points to, because unary operators like * and ++ associate right to left.
When you subtract one pointer from another, the two pointers must have the same basic type, although
you can disregard any type. Furthermore, you may compare any pointer with a null pointer constant using
the equality operators (== and !=), and you may compare any object pointer with a pointer to void.
A pointer variable is itself an object in memory, which means that a pointer can point to it. To declare a
pointer to a pointer , you must use two asterisks, as in the following Example
There are times when it's necessary to have a pointer that doesn't point to anything. A null pointer is
what results when you convert a null pointer constant to a pointer type. A null pointer constant is an
integer constant expression with the value 0, or such an expression cast as the type void *.
Null pointers are implicitly converted to other pointer types as necessary for assignment operations, or
for comparisons using == or !=. Hence no cast operator is necessary in the previous example.
A pointer to void, or void pointer for short, is a pointer with the type void *. As there are no objects
with the type void, the type void * is used as the all-purpose pointer type. In other words, a void pointer
can represent the address of any object but not its type. To access an object in memory, you must always
convert a void pointer into an appropriate object pointer.
2.4.2 Arrays
2.4.2.1 Basic of Arrays
An array contains objects of a given type, stored consecutively in a continuous memory block.The individual
objects are called the elements of an array. The elements' type can be any object type. No other types are
permissible: array elements may not have a function type or an incomplete type.
An array is also an object itself, and its type is derived from its elements' type. More specically, an
array's type is determined by the type and number of elements in the array. If an array's elements have type
T, then the array is called an "array of T." If the elements have type int, for example, then the array's type
is "array of int." The type is an incomplete type, however, unless it also species the number of elements. If
an array of int has 16 elements, then it has a complete object type, which is "array of 16 int elements."
In C, there is a strong relationship between pointers and arrays, strong enough that pointers and arrays
should be discussed simultaneously. Any operation that can be achieved by array subscripting can also be
done with pointers. The pointer version will in general be faster but, at least to the uninitiated, somewhat
harder to understand.
The denition of an array determines its name, the type of its elements, and the number of elements in the
array. The general syntax for declaring a single-dimensional array is
int a[10];
denes an array of size 10, that is, a block of 10 consecutive objects named a[0], a[1], ...,a[9].
char buffer[4*512];
denes an array with the name buer, which consists of 2,048 elements of type char.
• The lower bound of an array is set at 0. C++ does not allow you to override or alter this lower bound
• Declaring a C++ array entails specifying the number of members. The number of member is equal to
the upper bound plus one
• The valid range of indices extends between 0 and number_of_elements -1.
A multidimensional array in C is merely an array whose elements are themselves arrays. The elements of an
n-dimensional array are (n-1)-dimensional arrays. For example, each element of a two-dimensional array is
a one-dimensional array. The elements of a one-dimensional array, of course, do not have an array type.
A multidimensional array declaration has a pair of brackets for each dimension:
float mat[3][5];
The three elements mat[0], mat[1], and mat[2] are the rows of the matrix mat. Each of these rows is an
array of ve oat elements. Thus the matrix contains a total of 3 x 5 = 15 oat elements, as the following
diagram illustrates:
0 1 2 3 4
Table 2.21
The subscript operator [ ] provides an easy way to address the individual elements of an array by index.
If myArray is the name of an one dimensional array and i is an integer, then the expression myArray[i]
designates the array element with the index i. Array elements are indexed beginning with 0. Thus, if len is
the number of elements in an array, the last element of the array has the index len-1.
The following code fragment denes the array myArray and assigns a value to each element.
#define A_SIZE 4
long myarray[A_SIZE];
for (int i = 0; i < A_SIZE; ++i)
myarray[i] = 2 * i;
The diagram in Figure 2.16 illustrates the result of this assignment loop.
To access a char element in the three-dimensional array screen, you must specify three indices. For
example, the following statement writes the character Z in a char element of the array:
screen[9][39][79] = 'Z';
If you do not explicitly initialize an array variable, the usual rules apply: if the array has automatic storage
duration, then its elements have undened values. Otherwise, all elements are initialized by default to the
value 0.
To read the value for each element of an array, you should use for statement. For example,
int b[10];
int i;
// Read the value for each element of the array
for(i = 0; i < 10; i++)
{
In case you do not now the exact number of elements, declare the maximum number of elements and use a
variable to store the actual size of the array
int a[100]; // Declare the array with the number of elements not greater than 100
int n; // n is the actual size of the array
int i;
printf(``\n Enter the number of elements: ``);
scanf(``%d'',&n);
for(i = 0; i < n; i++)
{
printf("\n a[%d] = ", i);
scanf("%d",&a[i]);
}
C allow you to associate initializers with specic elements . To specify a certain element to initialize, place
its index in square brackets. In other words, the general form of an element designator for array elements is:
printf function are used to print the element of an array. In the following example, we print the element of
array a in dierent ways
#include <stdio.h>
#include <conio.h>
void main()
{
int a[5];
int i, k;
// Read the elements of the array
for(i = 0; i < 5; i++)
{
printf(``\n a[%d] = ``, i);
scanf(``%d'', &a[i]);
}
// print the value of element a[3]
printf(``\n a[3] = %d'', a[3]);
// Display all the elements of array a, each element in a line.
for(i = 0; i < 5; i++)
printf(``\n%d'', a[i]);
// Display all the elements of array a in a line
printf(``\n''); // change to a new line
for(i = 0; i < 5; i++)
printf(``%d ``, a[i]);
// Display all the elements of array a, k elements in a line
printf(``\n Enter the value of k = ``);
scanf(``%d'',&k);
for(i = 0; i < 5; i++)
{
printf(``%d ``,a[i]);
if((i+1)%k == 0) // change to a new line after printing k
//elements
printf(``\n'');
}
getch();
}
a[0] = 6
a[1] = 14
a[2] = 23
a[3] = 37
a[4] = 9
a[3] = 37
6
14
23
37
9
6 14 23 37 9
Input the value of k = 2
6 14
23 37
9
The purpose of this function is to nd the maximum value stored in the array
• Set up a trial minimum value. The function begins by declaring a variable named min and initializing
that variable with a trial minimum value value of the rst element .
• When all of the array elements have been examined and processed in this manner, the variable named
max will contain the maximum value of all the values stored in the array.
int a[100];
int i, n;
int max;
printf("\n Enter the size of the array: ");
scanf("%d",&n);
// Read the number of elements of the array
for(i = 0; i < n; i++)
{
printf("\n a[%d] = ",i);
scanf("%d",&a[i]);
}
// Find the maximum element
max = a[0]; // max is initialized by a[0]
// compare max to other elements
for(i = 1; i < n; i++)
if(max < a[i]) //meet an element greater than max
max = a[i]; // replace max by the new value from the elements.
printf("\n The maximum element of the array is: %d", max);
2.4.2.3.4 Searching
The simplest type of searching process is the sequential search. In the sequential search, each element of the
array is compared to the key, in the order it appears in the array, until the rst element matching the key is
found. If you are looking for an element that is near the front of the array, the sequential search will nd it
quickly. The more data that must be searched, the longer it will take to nd the data that matches the key
using this process.
here is the sample session with the above program
#include <stdio.h>
#include <conio.h>
void main()
{
int m[100], idx[100];
int n; // n is the actual size of the array
int i, k, test;
clrscr(); // clear screen
// Read array m
2.4.2.3.5 Sorting
Selection sort is a sorting algorithm, specically an in-place comparison sort. Selection sort is noted for its
simplicity, and also has performance advantages over more complicated algorithms in certain situations. It
works as follows:
Eectively, we divide the list into two parts: the sublist of items already sorted, which we build up from
left to right and is found at the beginning, and the sublist of items remaining to be sorted, occupying the
remainder of the array.
31 25 12 22 11
11 25 12 22 31
11 12 25 22 31
11 12 22 25 31
#include <stdio.h>
#include <conio.h>
void main()
{
int m[100];//100 is the maximum size for array m
int n; // n is the number of elements int i, j, k;
clrscr(); // clear screen
// Read the elements of array m
// Read the actual size of the array
For the sake of example, the following description deals with an array of int. The same principles apply for
any other array type, including multidimensional arrays.
To declare a pointer to an array type, you must use parentheses, as the following example illustrates:
//
++arrPtr; // Advance the pointer to the next row.
(*arrPtr)[0] = 7; // Assign the value 7 to the first element of the
// second row.
After the initial assignment, arrPtr points to the rst row of the matrix, just as the array name matrix
does. At this point you can use arrPtr in the same way as matrix to access the elements. For example, the
assignment (*arrPtr)[0] = 5 is equivalent to arrPtr[0][0] = 5 or matrix[0][0] = 5.
However, unlike the array name matrix, the pointer name arrPtr does not represent a constant address,
as the operation ++arrPtr shows. The increment operation increases the address stored in an array pointer
by the size of one array in this case, one row of the matrix, or ten times the number of bytes in an int
element.
If you want to pass a multidimensional array to a function, you must declare the corresponding function
parameter as a pointer to an array type.
One more word of caution: if a is an array of ten int elements, then you cannot make the pointer from
the previous example, arrPtr, point to the array a by this assignment:
typedef int ARRAY_t[10]; // A type name for "array of ten int elements".
ARRAY_t a, // An array of this type,
*arrPtr; // and a pointer to this array type.
arrPtr = (ARRAY_t *)a; // Let arrPtr point to a.
Pointer arrays that is, arrays whose elements have a pointer type are often a handy alternative to two-
dimensional arrays. Usually the pointers in such an array point to dynamically allocated memory blocks.
For example, if you need to process strings, you could store them in a two-dimensional array whose row
size is large enough to hold the longest string that can occur:
The diagram in illustrates how the objects are stored in memory. The pointers not yet used can be made to
point to other strings at runtime. The necessary storage can be reserved dynamically in the usual way. The
memory can also be released when it is no longer needed.
6
2.5 Functions
Every function is dened exactly once. A program can declare and call a function as many times as
necessary.
The denition of a function consists of a function head (or the declarator) and a function block . The
function head species the name of the function, the type of its return value, and the types and names of its
parameters, if any. The statements in the function block specify what the function does. The general form
of a function denition is as follows:
//function head
type function-name(parameter declarations)
//function block
{
declarations and statements
}
In the function head, name is the function's name, while type (return-type) consists of at least one type
specier, which denes the type of the function's return value. The return type may be void or any object
type, except array types. Furthermore, type may include the function specier inline, and/or one of the
storage class speciers extern and static.
A function cannot return a function or an array. However, you can dene a function that returns a
pointer to a function or a pointer to an array.
The parameterdeclarations are contained in a comma-separated list of declarations of the function's
parameters. If the function has no parameters, this list is either empty or contains merely the word void.
The type of a function species not only its return type, but also the types of all its parameters. The
following listing is a simple function to calculate the volume of a cylinder.
return [expression];
expression is evaluated and the result is given to the caller as the value of the function call. This return
value is converted to the function's return type, if necessary.
A function can contain any number of return statements:
if ( a < b ) return a;
else return b;
}
The contents of this function block can also be expressed by the following single statement:
return ( a < b ? a : b );
The parentheses do not aect the behavior of the return statement. However, complex return expressions
are often enclosed in parentheses for the sake of readability.
A return statement with no expression can only be used in a function of type void. In fact, such functions
do not need to have a return statement at all. If no return statement is encountered in a function, the program
ow returns to the caller when the end of the function block is reached.
The instruction to execute a function, the function call, consists of the function's name and the operator ( ).
For example, the following statement calls the function maximum to compute the maximum of the matrix
mat, which has r rows and c columns:
maximum( r, c, mat );
The program rst allocates storage space for the parameters, then copies the argument values to the corre-
sponding locations. Then the program jumps to the beginning of the function, and execution of the function
begins with rst variable denition or statement in the function block.
If the program reaches a return statement or the closing brace } of the function block, execution of the
function ends, and the program jumps back to the calling function. If the program "falls o the end" of the
function by reaching the closing brace, the value returned to the caller is undened. For this reason, you
must use a return statement to stop any function that does not have the type void. The value of the return
expression is returned to the calling function.
One of the C language's strengths is its exibility in dening data storage. There are two aspects that can
be controlled in C: scope and lifetime. Scope refers to the places in the code from which the variable can be
accessed. Lifetime refers to the points in time at which the variable can be accessed.
Three scopes are available to the programmer:
• extern: This is the default for variables declared outside any function. The scope of variables with
extern scope is all the code in the entire program.
• static: The scope of a variable declared static outside any function is the rest of the code in that
source le. The scope of a variable declared static inside a function is the rest of the local block.
• auto: This is the default for variables declared inside a function. The scope of an auto variable is the
rest of the local block.
Three lifetimes are available to the programmer. They do not have predened keywords for names as scopes
do. The rst is the lifetime of extern and static variables, whose lifetime is from before main() is called
until the program exits. The second is the lifetime of function arguments and automatics, which is from the
time the function is called until it returns. The third lifetime is that of dynamically allocated data. It starts
when the program calls malloc() or calloc() to allocate space for the data and ends when the program
calls free() or when it exits, whichever comes rst.
A local block is any portion of a C program that is enclosed by the left brace ({) and the right brace (}). A
C function contains left and right braces, and therefore anything between the two braces is contained in a
local block. An if statement or a switch statement can also contain braces, so the portion of code between
these two braces would be considered a local block. Additionally, you might want to create your own local
block without the aid of a C function or keyword construct. This is perfectly legal. Variables can be declared
within local blocks, but they must be declared only at the beginning of a local block. Variables declared in
this manner are visible only within the local block. Duplicate variable names declared within a local block
take precedence over variables with the same name declared outside the local block. Here is an example of
a program that uses local blocks:
#include <stdio.h>
void main(void);
void main()
{
/* Begin local block for function main() */
int test_var = 10;
printf(``Test variable before the if statement: %d\n'', test_var);
if (test_var > 5)
{
/* Begin local block for ``if'' statement */
int test_var = 5;
printf(``Test variable within the if statement: %d\n'', test_var);
{
/* Begin independent local block (not tied to any function or keyword) */
int test_var = 0;
printf(``Test variable within the independent local block:%d\n'', test_var);
}
/* End independent local block */
}
/* End local block for ``if'' statement */
printf(``Test variable after the if statement: %d\n'', test_var);
}
/* End local block for function main() */
This example program produces the following output:
The function in the listing above is declared with the storage class specier extern. This is not strictly
necessary, since extern is the default storage class for functions. An ordinary function denition that does
not contain a static or inline specier can be placed in any source le of a program. Such a function is
available in all of the program's source les, because its name is an external identier. You merely have to
declare the function before its rst use in a given translation unit. Furthermore, you can arrange functions
in any order you wish within a source le. The only restriction is that you cannot dene one function within
another. C does not allow you to dene "local functions" in this way.
You can hide a function from other source les. If you declare a function as static, its name identies it
only within the source le containing the function denition. Because the name of a static function is not
an external identier, you cannot use it in other source les. If you try to call such a function by its name
in another source le, the linker will issue an error message, or the function call might refer to a dierent
function with the same name elsewhere in the program.
The function printArray( ) in the following listing might well be dened using static because it is a
special-purpose helper function, providing formatted output of an array of oat variables.
int main( )
{
float farray[123];
/* ... */
printArray( farray, 123 );
/* ... */
}
A function prototype in C++ is a declaration of a function that omits the function body but does specify
the function's name, arity, argument types and return type. While a function denition species what a
function does, a function prototype can be thought of as specifying its interface. Just like a blueprint, the
prototype tells the compiler what the function will return, what the function will be called, as well as what
arguments the function can be passed. The general format for a prototype is simple:
#include <stdio.h>
#include <conio.h>
int mult ( int x, int y );
int main()
{
int x;
int y;
printf("Please input two numbers to be multiplied: ");
scanf("%d%d", &x,&y);
printf("The product of your two numbers is %d\n", mult ( x, y )) ;
return 0;
getch();
}
int mult ( int x, int y )
{
return x * y;
}
register in declaring function parameters. The register specier is a request to the compiler to make a variable
as quickly accessible as possible. No other storage class speciers are permitted on function parameters.
If you need to pass an array as an argument to a function, you would generally declare the corresponding
parameter in the following form:
type name[ ]
Because array names are automatically converted to pointers when you use them as function arguments, this
statement is equivalent to the declaration:
type *name
When you use the array notation in declaring function parameters, any constant expression between the
brackets ([ ]) is ignored. In the function block, the parameter name is a pointer variable, and can be
modied. Thus the function addArray() in the following listing modies its rst two parameters as it adds
pairs of elements in two arrays.
void addArray( register float a1[ ], register const float a2[ ], int len )
{
register float *end = a1 + len;
for ( ; a1 < end; ++a1, ++a2 )
*a1 += *a2;
}
An equivalent denition of the addArray() function, using a dierent notation for the array parameters,
would be:
void addArray( register float *a1, register const float *a2, int len )
{ /* Function body as earlier. */ }
An advantage of declaring the parameters with brackets ([ ]) is that human readers immediately recognize
that the function treats the arguments as pointers to an array, and not just to an individual oat variable.
But the array-style notation also has two peculiarities in parameter declarations :
• In a parameter declaration and only there C allows you to place any of the type qualiers const, volatile,
and restrict inside the square brackets. This ability allows you to declare the parameter as a qualied
pointer type.
• Furthermore, in C you can also place the storage class specier static, together with a integer constant
expression, inside the square brackets. This approach indicates that the number of elements in the
array at the time of the function call must be at least equal to the value of the constant expression.
In the function dened here, the parameter array is a constant pointer to long, and so cannot be modied.
It points to the rst of at least ve array elements.
In the following listing, the maximum( ) function's third parameter is a two-dimensional array of
variable dimensions.
Since C passes arguments to functions by value, there is no direct way for the called function to alter a
variable in the calling function. For instance, a sorting routine might exchange two out-of-order arguments
with a function called swap. It is not enough to write
swap(a, b);
where the swap function is dened as
swap(&a, &b);
Since the operator & produces the address of a variable, &a is a pointer to a. In swap itself, the parameters
are declared as pointers, and the operands are accessed indirectly through them.
{
Pictorially in Figure 2.18
7
2.6 Strings
char str1[30] = { 'L', 'e', 't', '\'', 's',' ', 'g', 'o', '\0' };
7 This content is available online at <http://cnx.org/content/m27761/1.1/>.
An array holding a string must always be at least one element longer than the string length to accommodate
the terminating null character. Thus the array str1 can store strings up to a maximum length of 29. It
would be a mistake to dene the array with length 8 rather than 30, because then it wouldn't contain the
terminating null character.
If you dene a character array without an explicit length and initialize it with a string literal, the array
created is one element longer than the string length. An Example
#include <string.h>
/* ... */
The standard library provides a number of functions to classify characters and to perform conversions on
them. The header ctype.h declares such functions for byte characters, with character codes from 0 to 255.
The results of these functions, except for isdigit() and isxdigit(), depends on the current locale setting
for the locale category LC_CTYPE. You can query or change the locale using the setlocale() function.
The functions listed in Table 2.22: Character classication functions test whether a character belongs to a
certain category. Their return value is nonzero, or true, if the argument is a character code in the given
category.
Category Functions
Letters isalpha( )
Table 2.22
The functions isgraph() and iswgraph() behave dierently if the execution character set contains
other byte-coded, printable, whitespace characters (that is, whitespace characters which are not control
characters) in addition to the space character (' '). In that case, iswgraph() returns false for all such
printable whitespace characters, while isgraph() returns false only for the space character (' ').
The functions listed in Table 2.23: Character conversion functions yield the uppercase letter that corresponds
to a given lowercase letter, and vice versa. All other argument values are returned unchanged.
Table 2.23
A string is a continuous sequence of characters terminated by '\0', the string terminator character. The
length of a string is considered to be the number of characters before the string terminator. Strings are
stored in arrays whose elements have the type char or wchar_t. Strings of wide characters that is, characters
of the type wchar_tare also called wide strings.
C does not have a basic type for strings, and hence has no operators to concatenate, compare, or assign
values to strings. Instead, the standard library provides numerous functions, listed in
Table 2.24: String-processing functions to perform these and other operations with strings. The header
string.h declares the functions for conventional strings of char. The names of these functions begin with
str.
Like any other array, a string that occurs in an expression is implicitly converted into a pointer to its rst
element. Thus when you pass a string as an argument to a function, the function receives only a pointer to
the rst character, and can determine the length of the string only by the position of the string terminator
character.
String-processing functions
Purpose Functions
In a string, nd:
Table 2.24
Example 2.13
#include <stdio.h>
#include <conio.h>
#include <string.h> // You must declare the library string.h
// to use functions strcpy, strcmp...
void main()
{
char str1[10] = ``abc'';
char str2[10] = ``def'';
clrscr();
printf(`` str1: %s'',str1);
printf(``\n str2: %s'',str2);
printf(``\n strcmp(str1,str2) = %d'',strcmp(str1,str2));
printf(``\n strcat(str1,str2) = %s'',strcat(str1,str2));
printf(``\n str1: %s'',str1);
printf(``\n str2: %s'',str2);
str1: abc
str2: def
strcmp(str1,str2) = -3
strcat(str1,str2) = abcdef
str1: abcdef
str2: def
strcpy(str1,str2) = def
str1: def
str2: def
strcmp(str1,str2) = -3
8
2.7 Structures
2.7.1 Introduction
A structure type can contain a number of dissimilar data objects within it. Unlike a simple variable (which
contains only one data object) or an array (which, although it contains more than one data item, only
contains items of a single data type),a structure is a collection of related data of dierent types. a name,
for example, might be array of characters, an age might be integer. A structure representing a person, say,
could contain both a name and an age, each represented in the appropriate format.
• A unique student number, which could be represented as a string (an array of char).
• The student's name, which could be represented as a string (an array of char).
• Final mark for the Introduction to computer science course, which is a oating point value (a oat).
The denition of a structure type begins with the keyword struct, and contains a list of declarations of the
structure's members, in braces:
struct structTag
{
<list of members>;
};
Example 2.14
The three components above can be placed in a structure declared like this:
struct Student
{
char StudentID[10];
char name[30];
float markCS ;
};
The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces.
An optional name called a structure tag may follow the word struct (as with Student here). The tag names
this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces.
The variables named in a structure are called members. A structure member or tag and an ordinary (i.e.,
non-member) variable can have the same name without conict, since they can always be distinguished by
context. Furthermore, the same member names may occur in dierent structures, although as a matter of
style one would normally use the same names only for closely related objects.
Structure types are not considered a variable declaration, just denition of a new type, so they cannot store
anything until we declare variable of this type. Here is how we would create:
type_name_of_struct name_of_variable;
Example 2.15
Creating three variables a, b, c of the Student type:
Student a, b, c;
Creating an array of the Student type:
Student studentCS[50];
A member of a structure may have any desired complete type, including previously dened structure types.
They must not be variable-length arrays, or pointers to such arrays. For instance, now we want to record
more information of students, for example their date of birth, which comprises the day, month and year.
So rst, let's start with the date, because that is a new type that we may be able to use in a variety of
situations. We can declare a new type for a Date thus:
struct Date
{
int day;
int month;
int year;
};
We can now use this Date type, together with other types, as members of a Student type which we can
declare as follows:
struct Student
{
char studentID[10];
char name[30];
float markCS ;
Date dateOfBirth;
};
Or
struct Student
{
char studentID[10];
char name[30];
float markCS;
struct Date {
int day;
int month;
int year;
} dateOfBirth;
};
We can also declare structured variables when we dene the structure itself:
struct Student
{
char studentID[10];
char name[30];
float markCS ;
Date dateOfBirth;
} a, b, c;
C permits to declare untagged structures that enable us to declare structure variables without dening a
name for their structures. For example, the following structure denition declares the variables a, b, c but
omits the name of the structure:
struct
{
char studentID[10];
char name[30];
float markCS ;
Date dateOfBirth;
} a, b, c;
A structure type cannot contain itself as a member, as its denition is not complete until the closing brace (}).
However, structure types can and often do contain pointers to their own type. Such self-referential structures
are used in implementing linked lists and binary trees, for example. The following example denes a type
for the members of a singly linked list:
struct List
{ struct Student stu; // This record's data.
struct List *pNext; // A pointer to the next student.
};
Whenever we need to refer to the members of a structure, we normally use the dot operator.
For example, if we wanted to access the number member of newStudent we could do so as follows:
newStudent.studentID
We can then access the member as if it were a normal variable. For instance, we can write to this member
as follows.
newStudent.studentID= C0681008;
We can also read from the member in a similar fashion.
printf("Student Details\n");
printf("Identification: %s\n", newStudent.studentID);
printf("Name: %s\n", newStudent.name);
printf("Mark: %.2f\n", newStudent.markCS);
printf("Date of Birth: %i/%i/%i\n",
newStudent.dateOfBirth.day,
newStudent.dateOfBirth.month,
newStudent.dateOfBirth.year
);
Suppose we wish to input the details of this employee from a user. We could do so as follows.
Student newStudent;
printf("Enter student identification: ");
scanf("%s", &newStudent.studentID);
printf("Enter student name: ");
fflush(stdin);gets(newStudent.name);
printf("Enter mark for Introduction to computer science course: '');
scanf("%f", &newStudent.markCS);
printf("Enter birth date (dd/mm/yyyy): ");
scanf("%i/%i/%i",
&newStudent.dateOfBirth.day,
&newStudent.dateOfBirth.month,
&newStudent.dateOfBirth.year
);
When we declare a new variable of a basic data type we can initialize its value at declaration. We can also
initialize structure variables at declaration as shown below.
Student newStudent = {
"C0681008",
"Cao Anh Huy",
8.50,
{1, 2, 1985}
};
Notice how we include the initialization values in curly brackets, just like when we initialize an array.
Furthermore, we include the values for any nested structure type (in this case the dateOfBirth member is
a nested structure), in a further set of curly brackets.
One of the most convenient features of structures is that we can copy them in a single assignment operation.
This is unlike an array, which must be copied item-by-item. The name of a structure variable when it appears
on its own represents the entire structure. If a structure contains an array as a member, that array is copied
if the entire structure is copied.
We cannot compare structures in a single operation. If we wish to compare the values of two structure
variables, we need to compare each of their members.
Student students[100];
Student newStudent;
int i;
for (i=0; i<100; i++)
{
// Get the values for newStudent
...
// Copy into the next position in the array
students[i] = newStudent;
}
Structures can be passed to functions just like any other data type. Functions can also return structures,
just as they can return any basic type. Structures can be also be passed to functions by reference.
Just like passing variable of a basic data type, when we pass a structure as an argument to a function,
a copy is made of the entire structure. Structures are passed by value. We can easily take the code that
output a student and put it into a function as follows.
Student students[100];
int i;
...
for (i=0; i<100; i++) {
outputStudent(students[i]);
}
We could similarly place the code to input a student into a function, but now we have a problem. The
function can return a structure of type Student as follows.
Student inputStudent()
{
Student tempStudent;
printf("Enter Student identification: ");
scanf("%s", &tempStudent.studentID);
printf("Enter Student name: ");
fflush(stdin);gets(tempStudent.name);
printf("Enter final mark: ");
scanf("%f", &tempStudent.markCS);
printf("Enter birth date (dd/mm/yyyy):");
scanf("%i/%i/%i",
&tempStudent.dateOfBirth.day,
&tempStudent.dateOfBirth.month,
&tempStudent.dateOfBirth.year
);
return tempStudent;
}
In the example above we are lling the structure variable tempStudent with values. At the end of the
function, the value of tempStudent is returned as the return value of the function. The code to input 100
students can now be modied to use this function:
Student students[100];
int i;
for (i=0; i<100; i++) {
students[i] = inputStudent();
}
In order to dereference a pointer we would normally use the dereferencing operator (*) and if we our pointer
was to a structure, we could subsequently use the dot '.' operator to refer to a member of the structure.
Suppose we have declared a pointer which could be used to point to a structure of type employee as follows.
Student stuVariable;
Student *stuPtr;
stuPtr = &stuVariable;
To refer to the student identication we could say:
(*stuPtr).studentID
Note that the brackets are necessary because the dereference operator has lower precedence than the dot
operator. This form of syntax is a little cumbersome, so another operator is provided to us as a convenient
shorthand:
stuPtr->studentID
This method of accessing the number member through a pointer is completely equivalent to the previous
form. The '->' operator is called the indirect member selection operator or just the arrow operator and it is
almost always used in preference to the previous form.
Passing structures to a function using pass-by-value can be simple and successful for simple structures so long
as we do not wish to do so repeatedly. But when structures can contain a large amount of data (therefore
occupying a large chunk of memory) then creating a new copy to pass to a function can create a burden on
the memory of the computer. If we were going to do this repeatedly (say several thousand times within the
running of a computer) then there would also be a cost in time to copy the structure for each function call.
In the example at the beginning of this section we created and lled a structure variable called temp-
Student. When the function ended it returned the value of tempStudent. The same ineciency exists with
the return value from the function, where the Student structure must be copied to a local variable at the
function call.
Whether such ineciencies are of any signicance or not depends on the circumstances and on the size of
the structure. Each Student structure probably occupies about 50 bytes, so this is a reasonably signicant
amount of memory to be copying each time the output function is called or each time the input function
returns, especially if this is happening frequently.
A better solution would be to pass the Student structure by reference, which means we will pass a pointer
to the structure.
We can now revise the input function by passing an Student structure by reference using a pointer.
Because the function is no longer returning an Student structure, we can also enhance the function to return
a Boolean status indicating whether an Student structure was successfully read or not. We can enhance our
function to do some better error checking. Below is the revised version.
if (scanf("%i/%i/%i",&stuPtr->dateOfBirth.day,
&stuPtr->dateOfBirth.month,&stuPtr->dateOfBirth.year) != 3)
return false;
return true;
}
The code to input 100 students can now be revised as follows.
Student students[100];
int i;
for (i=0; i<100; i++)
{
while (!inputStudent(&students[i]))
{
printf("Invalid student details - try again!\n");
fflush(stdin);
}
}
As a nal example, consider a function to give s student a mark rise. The function takes two parameters.
The rst is an Student structure passed by reference, (a pointer to an Student structure) and the second is
the increase of mark.
DayOfWeek nameOfDay;
With variables of enumerated types we can do almost anything we could do with a variable of a basic data
type. For instance we can assign a value as follows.
nameOfDay = tue;
Note that tue is a literal value of type DayOfWeek and we do not need to place quotes around it.
DayOfWeek are ordered and each has an equivalent int value; sun==0, mon==1, and
The values in
so on. The value of sun is less than the value of wed because of the order they were presented in the list of
values when dening the type. We can compare two values of enumerated types as follows:
#include <stdio.h>
#include <conio.h>
enum TrafficLight {red, orange, green};
int main()
{
TrafficLight light;
printf("Please enter a Light Value: (0)Red (1)Orange (2)Green:\n");
scanf("%i", &light);
switch(light)
{
case red:
printf("Stop!\n");
break;
case orange:
printf("Slow Down\n");
break;
case green:
printf("Go\n");
}
getch();
}
9
2.8 Files
output occurs (probably) and this is to be expected. A le is simply a machine decipherable storage media
where programs and data are stored for machine usage.
Essentially there are two kinds of les that programmers deal with text les and binary les:
• Text les are any les that contain only ASCII characters. Examples include C source code les,
HTML les, and any le that can be viewed using a simple text editor.
• Binary les are any les that created by writing on it from a C-program, not by an editor (as with
text les). Binary les are very similar to arrays of records, except the records are in a disk le rather
than in an array in memory. Because the records in a binary le are on disk, you can create very large
collections of them (limited only by your available disk space). They are also permanent and always
available. The only disadvantage is the slowness that comes from disk access time.
A text le can be a stream of characters that a computer can process sequentially. It is not only processed
sequentially but only in forward direction. For this reason a text le is usually opened for only one kind of
operation (reading, writing, or appending) at any given time.
Similarly, since text les only process characters, they can only read or write data one character at a
time. (In C Programming Language, Functions are provided that deal with lines of text, but these still
essentially process data one character at a time). A text stream in C is a special kind of le. Depending
on the requirements of the operating system, newline characters may be converted to or from carriage-
return/linefeed combinations depending on whether data is being written to, or read from, the le. Other
character conversions may also occur to satisfy the storage requirements of the operating system. These
translations occur transparently and they occur because the programmer has signaled the intention to process
a text le.
A binary le is no dierent to a text le. It is a collection of bytes. In C Programming Language a
byte and a character are equivalent. Hence a binary le is also referred to as a character stream, but there
are two essential dierences.
• No special processing of the data occurs and each byte of data is transferred to or from the disk
unprocessed.
• C Programming Language places no constructs on the le, and it may be read from, or written to, in
any manner chosen by the programmer.
Binary les can be either processed sequentially or, depending on the needs of the application, they can be
processed using random access techniques. In C Programming Language, processing a le using random
access techniques involves moving the current le position to an appropriate place in the le before reading
or writing data. This indicates a second characteristic of binary les they a generally processed using read
and write operations simultaneously.
For example, a database le will be created and processed as a binary le. A record update operation
will involve locating the appropriate record, reading the record into memory, modifying it in some way, and
nally writing the record back to disk at its appropriate location in the le. These kinds of operations are
common to many binary les, but are rarely found in applications that process text les.
For all le operations you should always follow the 5-step plan as outlined below.
In C, we usually create variables of type FILE * to point to a le located on the computer.
FILE *file_pointer_name;
Example
First things rst: we have to open a le to be able to do anything else with it. For this, we usefopen function,
like all the I/O functions, is made available by the stdio.h library. The fopen() function prototype is as
follows.
• lename is a string containing the name of the le to be opened. So if your le sits in the same
directory as your C source le, you can simply enter the lename in here - this is probably the one
you'll use most.
• mode determines how the le may be accessed.
Mode Meaning
Table 2.25
So there are 12 dierent values that could be used: "rt", "wt", "at", "r+t", "w+t", "a+t" and
"rb", "wb", "ab", "r+b", "w+b", "a+b".
Character Type
Table 2.26
note: When work with the text le, you also can use only "r", "w", "a", "r", "w, "a", instead of
"rt", "wt", "at", "r+t", "w+t", "a+t" respectively.
Example
f1 = fopen("c:\\abc.txt", "r");
To open text le c:\list.dat for write only:
f2 = fopen("c:\\list.dat", "w");
To open text le c:\abc.txt for read-write:
f3 = fopen("c:\\abc.txt", "r+");
To open binary le c:\liststudent.dat for write only:
f4 = fopen("c:\\liststudent.dat", "wb");
The le pointer will be used with all other functions that operate on the le and it must never be altered or
the object it points to.
File checking
if (file_pointer_name == NULL)
{
printf("Error opening file.");
<Action for error >
}
else
{
<Action for success>
}
Before using an input/output le it is worth checking that the le has been correctly opened rst. A call to
fopen() may result in an error due to a number of reasons including:
If the operation is successful, fopen() returns an address which can be used as a stream. If a le is not
successfully opened, the value NULL is returned. An error opening a le can occur if the le was to be
opened for reading and did not exist, or a le opened for writing could not be created due to lack of disk
space. It is important to always check that the le has opened correctly before proceeding in the program.
Example 2.16
FILE *fp;
if ((fp = fopen("myfile", "r")) ==NULL){
printf("Error opening file\n");
exit(1);
}
Once a le has been opened, depending upon its mode, you may read and/or write bytes to or from it.
When writing data to text les, C provides three functions: fprintf(), fputs(), fputc().
The fprintf() function prototype is as follows:
Example 2.17
#include <stdio.h>
int main ()
{
FILE * fp;
int n;
char name [50];
fp = fopen ("myfile.txt","w");
for (n=0 ; n<3 ; n++)
{
puts ("Please, enter a name: ");
gets (name);
fprintf (fp, "Name %d [%-10.10s]\n",n,name);
}
fclose (fp);
return 0;
}
This example prompts 3 times the user for a name and then writes them to myle.txt each one in a line with
a xed length (a total of 19 characters + newline). Two format tags are used: %d : signed decimal integer,
%-10.10s : left aligned (-), minimum of ten characters (10), maximum of ten characters (.10), String (s).
Assuming that we have entered John, Jean-Francois and Yoko as the 3 names, myle.txt would contain:
myle.txt
Name 1 [John ]
Name 2 [Jean-Franc]
Name 3 [Yoko ]
Table 2.27
Example 2.18
Write the program that creates a le called alphabet.txt and writes ABCDEFGHI-
JKLMNOPQRSTUVWXYZ to it.
#include <stdio.h>
int main ()
{
FILE * fp;
char c;
fp = fopen ("alphabet.txt","w");
if (fp!=NULL)
{
for (c = 'A' ; c <= 'Z' ; c++)
{
fputc ((int) c , fp);
}
fclose (fp);
}
return 0;
}
Example 2.19
Write the program allows to append a line to a le called myle.txt each time it is run.
#include <stdio.h>
int main ()
{
FILE * fp;
char name [50];
When reading data from text les, C provides three functions: fscanf(),fgetc(), fgets().
The fscanf() function prototype is as follows.
Example 2.20
Read an integer number and a character from le associated with a le pointer fp and stores them
to two variables a and c.
Example 2.21
#include <stdio.h>
int main ()
{
char str [80];
float f;
FILE * fp;
fp = fopen ("myfile.txt","w+");
fprintf (fp, "%f %s", 3.1416, "PI");
rewind (fp);
fscanf (fp, "%f", &f);
fscanf (fp, "%s", str);
fclose (fp);
printf ("I have read: %f and %s \n",f,str);
return 0;
}
This sample code creates a le called myle.txt and writes a oat number and a string to it. Then, the
stream is rewinded and both values are read with fscanf. It nally produces an output similar to:
Example 2.22
Create a text le called fscanf.txt in Notepad with this content:
0 1 2 3 4
5 6 7 8 9
10 11 12 13
Remember how scanf stops reading input when it encounters a space, line break or tab character?
fscanf is just the same. So if all goes to plan, this example should open the le, read all the numbers
and print them out:
#include <stdio.h>
int main() {
FILE *fp;
int numbers[30];
/* make sure it is large enough to hold all the data! */
int i,j;
fp = fopen("fscanf.txt", "r");
if(fp==NULL) {
printf("Error: can't open file.\n");
return 1;
}
else {
printf("File opened successfully.\n");
i = 0 ;
while(!feof(fp)) {
/* loop through and store the numbers into the array */
fscanf(fp, "%d", &numbers[i]);
i++;
}
fclose(fp);
return 0;
}
}
scanf(), before using fscanf() to read the character or string from the le, we need use ush().The
Same as
ush() function prototype is as follows.
If the given le that specied by fp was open for writing and the last I/O operation was an output operation,
any unwritten data in the output buer is written to the le. If the le was open for reading, the behavior
depends on the specic implementation. In some implementations this causes the input buer to be cleared.
If the argument is a null pointer, all open les are ushed. The les remains open after this call. When a
le is closed, either because of a call to fclose or because the program terminates, all the buers associated
with it are automatically ushed.
Return Value: A zero value indicates success. If an error occurs, EOF is returned and the error indicator
is set (see feof ).
Example 2.23
Write the program reads an existing le called myle.txt character by character and uses the n
variable to count how many dollar characters ($) does the le contain.
#include <stdio.h>
int main ()
{
FILE * fp;
int c;
int n = 0;
fp=fopen ("myfile.txt","r");
if (fp==NULL) printf("Error opening file");
else
{
do {
c = fgetc (fp);
if (c == '$') n++;
} while (c != EOF);
fclose (fp);
printf ("File contains %d$.\n",n);
}
return 0;
}
Example 2.24
Write the program opens the le called myle.txt, and counts the number of characters that it
contains by reading all of them one by one. Finally the total amount of bytes is printed out.
#include <stdio.h>
int main ()
{
FILE * fp;
long n = 0;
fp = fopen ("myfile.txt","rb");
if (fp==NULL) printf ("Error opening file");
else
{
while (!feof(fp)) {
fgetc (fp);
n++;
}
fclose (fp);
printf ("Total number of bytes: %d\n",n);
}
return 0;
}
Example 2.25
Opens a le called input.txt which has some random text (less than 200 characters), stores each
character in an array, then spits them back out into another le called "output.txt" in reverse order:
#include <stdio.h>
int main() {
char c; /* declare a char variable */
char name[200]; /* Initialize array of total
200 for characters */
FILE *f_input, *f_output; /* declare FILE pointers */
int counter = 0; /* Initialize variable for counter to zero */
if(f_input==NULL) {
printf("Error: can't open file.\n");
return 1;
}
else {
while(1) { /* loop continuously */
c = fgetc(f_input); /* fetch the next character */
if(c==EOF) {
/* if end of file reached, break out of loop */
break;
}
else if (counter<200) { /* else put character into array */
name[counter] = c;
counter++; /* increment the counter */
}
else {
break;
}
}
if(f_output==NULL) {
printf("Error: can't create file.\n");
return 1;
}
else {
counter--; /* we went one too step far */
while(counter >= 0) { /* loop while counter's above zero */
fputc(name[counter], f_output);
/* write character into output file */
counter--; /* decrease counter */
}
Reading one character at a time can be a little inecient, so we can use fgets to read one line at a time.
The fgets() function prototype is as follows.
Example 2.26
Create a le called myle.txt in Notepad, include 3 lines and put tabs in the last line.
#include <stdio.h>
int main()
{
char c[10]; /* declare a char array */
FILE *file; /* declare a FILE pointer */
if(file==NULL)
{
printf("Error: can't open file.\n");
/* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
return 1;
}
else
{
printf("File opened successfully. Contents:\n\n");
Output:
The main area of focus is the while loop - notice how I performed the check for the return of a NULL
pointer. Remember that passing in char * variable, c as the rst argument assigns the line read into c, which
is printed o by printf. We specied a maximum number of characters to be 10 - we knew the number of
characters per line in our text le is more than this, but we wanted to show that fgets reads 10 characters
at a time in this case.
Notice how fgets returns when the newline character is reached - this would explain why 444 and 777
follow the word "String". Also, the tab character, \t, is treated as one character.
Table 2.28
This function sets the position indicator associated with the fp to a new position dened by adding oset
to a reference position specied by origin. The End-of-File internal indicator of the le is cleared after a call
to this function.
Return Value: If successful, the function returns a zero value. Otherwise, it returns nonzero value.
Example 2.27
#include <stdio.h>
int main ()
{
FILE * fp;
fp = fopen ( "myfile.txt" , "w" );
fputs ( "This is an apple." , fp );
fseek ( fp , -8 , SEEK_END );
fputs ( " sam" , fp );
fclose ( fp );
return 0;
}
This is a sample.
Example 2.28
#include <stdio.h>
int main ()
{
FILE * fp;
fp = fopen ( "myfile.txt" , "w" );
fputs ( "This is an apple." , fp );
fseek ( fp , 9 , SEEK_SET );
fputs ( " sam" , fp );
fclose ( fp );
return 0;
}
This is a sample.
Example 2.29
#include <stdio.h>
#include <conio.h>
int main ()
{
char str [80];
int n;
FILE * fp;
fp = fopen ("myfile.txt","w+");
for ( n='A' ; n<='Z' ; n++)
fputc ( n, fp);
rewind (fp);
n=0;
while (!feof(fp))
{
str[n]= fgetc(fp);
n++;
}
fclose (fp);
printf ("I have read: %s \n",str);
getch();
return 0;
}
A le called myle.txt is created for reading and writing and lled with the alphabet. The le is then
rewinded, read and its content is stored in a buer, that then is written to the standard output:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Example 2.30
#include <stdio.h>
int main()
{
FILE *file;
char sentence[50];
int i;
if(file==NULL) {
printf("Error: can't create file.\n");
return 1;
}
else {
printf("File created successfully.\n");
while(!feof(file)) {
printf("%c", fgetc(file));
}
printf("\n");
fclose(file);
return 0;
}
}
Output depends on what you entered. First of all, we stored the inputted sentence in a char array, since
we're writing to a le one character at a time it'd be useful to detect for the null character. Recall that the
null character, \0, returns 0, so putting sentence[i] in the condition part of the for loop iterates until the null
character is met.
Then we call rewind, which takes the le pointer to the beginning of the le, so we can read from it. In
the while loop we print the contents a character at a time, until we reach the end of the le - determined by
using the feof function.
Note that it is essential to have the include le stdio.h referenced at the top of your program in order
to use any of these functions: fscanf(), fgets(), fgetc(), ush(), fprintf(), fputs(), fputc(), feof(), fseek() và
rewind().
When a function returns EOF (or, occasionally, 0 or NULL, as in the case of fread and fgets respectively),
we commonly say that we have reached end of le but it turns out that it's also possible that there's been
some kind of I/O error. When you want to distinguish between end-of-le and error, you can do so with the
feof and ferror functions. feof(fp) returns nonzero (that is, true) if end-of-le has been reached on the le
pointer fp, and ferror(fp) returns nonzero if there has been an error.
Notice feof returns nonzero if end-of-le has been reached. It does not tell you that the next attempt to
read from the stream will reach end-of-le, but rather that the previous attempt (by some other function)
already did. (If you know Pascal, you may notice that the end-of-le detection situation in C is therefore
quite dierent from Pascal.) Therefore, you would never write a loop like
while(!feof(fp))
fgets(line, max, fp);
Instead, check the return value of the input function directly:
fp = fopen(filename, "r");
if(fp == NULL)
{
fprintf(stderr, "can't open file\n");
return;
}
or
if(ferror(fp))
fprintf(stderr, "error reading input\n");
or
fp = fopen(filename, "r");
if(fp == NULL)
{
fprintf(stderr, "can't open %s for reading: %s\n",
filename, strerror(errno));
return;
}
errno is a global variable, declared in <errno.h>, which may contain a numeric code indicating the reason
for a recent system-related error such as inability to open a le. The strerror function takes an errno code
and returns a human-readable string such as No such le or Permission denied.
An even more useful error message, especially for a toolkit program intended to be used in conjunction
with other programs, would include in the message text the name of the program reporting the error.
Return value: returns the number of items read. It returns 0 (not EOF) at end-of-le.
Example 2.31
#include <stdio.h>
int main() {
FILE *file;
char c[30]; /* make sure it is large enough to hold all the data! */
char *d;
int n;
if(file==NULL) {
printf("Error: can't open file.\n");
return 1;
}
else {
printf("File opened successfully.\n");
fclose(file);
return 0;
}
}
Output:
111
222
333
444
5ive
Characters read: 10
The above code: passing a char pointer reads in the entire text le, as demonstrated. Note that the number
fread returns in the char pointer case is clearly incorrect. This is because the char pointer (d in the example)
must be initialized to point to something rst.
An important line is: c[n] = '\0'; Previously, we put 10 instead of n (n is the number of characters read).
The problem with this was if the text le contained less than 10 characters, the program would put the null
character at a point past the end of the le.
There are several things you could try with this program:
• After reading the memory allocation section, try allocating memory for d using malloc() and freeing
it later with free().
• Read 25 characters instead of 10: n = fread(c, 1, 25, le);
• Not bother adding a null character by removing: c[n] = '\0';
• Not bother closing and reopening the le by removing the fclose and fopen after printing the char
array.
Binary les have two features that distinguish them from text les: You can jump instantly to any record in
the le, which provides random access as in an array; and you can change the contents of a record anywhere
in the le at any time. Binary les also usually have faster read and write times than text les, because a
binary image of the record is stored directly from memory to disk (or vice versa). In a text le, everything
has to be converted back and forth to text, and this takes time.
Besides reading and writing blocks of characters, you can use fread and fwrite to do binary I/O. For
example, if you have an array of int values:
int array[N];
you could write them all out at once by calling
struct somestruct x;
you could write it out all at once by calling
Example 2.32
#include <stdio.h>
int main ()
{
FILE * fp;
fp = fopen ("myfile.txt","wt");
fprintf (fp, "fclose example");
fclose (fp);
return 0;
}
Keywords are listed by the section with that keyword (page numbers are in parentheses). Keywords
do not necessarily appear in the text of the page. They are merely associated with that section. Ex.
apples, 1.1 (1) Terms are referenced by the page they appear on. Ex. apples, 1
Attributions
Module: "Functions"
By: Huong Nguyen
URL: http://cnx.org/content/m27749/1.1/
Pages: 116-124
Copyright: Huong Nguyen
License: http://creativecommons.org/licenses/by/3.0/
Module: "Strings"
By: Huong Nguyen
URL: http://cnx.org/content/m27761/1.1/
Pages: 124-128
Copyright: Huong Nguyen
License: http://creativecommons.org/licenses/by/3.0/
Module: "Structures"
By: Huong Nguyen
URL: http://cnx.org/content/m27764/1.1/
Pages: 128-136
Copyright: Huong Nguyen
License: http://creativecommons.org/licenses/by/3.0/
Module: "Files"
By: Huong Nguyen
URL: http://cnx.org/content/m27743/1.1/
Pages: 136-155
Copyright: Huong Nguyen
License: http://creativecommons.org/licenses/by/3.0/
About Connexions
Since 1999, Connexions has been pioneering a global system where anyone can create course materials and
make them fully accessible and easily reusable free of charge. We are a Web-based authoring, teaching and
learning environment open to anyone interested in education, including students, teachers, professors and
lifelong learners. We connect ideas and facilitate educational communities.
Connexions's modular, interactive courses are in use worldwide by universities, community colleges, K-12
schools, distance learners, and lifelong learners. Connexions materials are in many languages, including
English, Spanish, Chinese, Japanese, Italian, Vietnamese, French, Portuguese, and Thai. Connexions is part
of an exciting new information distribution system that allows for Print on Demand Books. Connexions
has partnered with innovative on-demand publisher QOOP to accelerate the delivery of printed course
materials and textbooks into classrooms worldwide at lower prices than traditional academic publishers.