1st Module
1st Module
Module 1
…… ……
int x = 5; 01100100010101000
printf(“x=%d\n”,x); 10010101110001100
return 0; 10000111010110110
…... ………
Compile
Source Code (Written by Executable Code (ready to
programmer run)
Run Program
on CPU 2
➢ Binary devices understand only meaningful sequences of 0’s and 1’s called machine
language.
➢ The CPU doesn’t understand programming languages, every program translated to its
corresponding machine language code before execution.
➢ A separate program called the compiler, translates or compiles this source code to its
equivalent machine code in binary.
➢ As shown in figure 1.1, eventually executed by the CPU.
➢ The CPU needs adequate memory to run programs.
➢ It fetches program instructions and data from memory and writes temporary and final data to
memory.
➢ Memory can be volatile or non- volatile.
➢ The CPU uses the appropriate type depending on how long the data need to be preserved.
1.3 The Background
➢ The computer made its beginning as a simple calculator.
➢ The device called the abacus comprised a number of rings or beads that could be moved
along rods enclosed by a wooden frame.
➢ Charles Babbage who used the concept in his 2 conceptual creations- the Difference engine and
Analytical engine.
➢ The holes in the cards of the Jacquard loom determined whether a thread would pass through it,
Babbage saw an opportunity to use them to encode instructions and data.
➢ The analytical engine was fully automatic and designed to run on steam power.
➢ It had a processing unit and memory – 2 essential components of any programmable device.
➢ Conditional Statement: Loops which allowed a set of statements to be repeated, the analytical
engine feature.
➢ Babbage also utilized the services of Ada Lovelace who designed a few algorithms for the
engine.
➢ Babbage is father of the computer while Lovelace is accepted as the first programmer
.
1.3.3 The Programmable Electronic Computer.
➢ Most computers used electro mechanical relays and switches to perform computation.
➢ The Z3 computer designed by Konard Zuse during the war used over 2000 relays.
➢ It was the first computer based on the binary system and program control was achieved by
punched film.
➢ ENIAC – Electronic Numerical Integrator and Computer
o It was represented by the state of its switches, so representing the machine for a different job
needed manual resetting of these switches.
o The difficulty of programming speed was an issue even with electronic computers.
➢ This problem was addressed by Alan Turing.
➢ Turing reasoned that computer speeds could be improved if both program and data were stored
in the same memory. Later reiterated by John Von Neumann in the concept of stored program
computer.
➢ A program fed through paper tape couldn’t fast enough because the computer could only load
one instruction at a time.
➢ Manchester Mark I became the first computer to implement the stored program concept.
➢ Vacuum Tube for performing computation.
o Generates lot of heat
o Consumed enormous power
o Unreliable.
➢ Transistors
o Smaller
o Generates less heat.
o Reliable.
o Made way for integrated circuits and it leads to microprocessor.
o Microprocessor which incorporated the entire CPU on a single chip.
4
➢ The computer of each generation is faster, smaller and more powerful than its counter part of
the preceding shown below.
➢ It used vacuum tubes for circuitry and magnetic drums for memory, and were often enormous,
taking up entire rooms.
➢ These computers were very expensive to operate and in addition to using a great deal of
electricity, the first computers generated a lot of heat, which was often the cause of malfunctions.
➢ First generation computers relied on machine language, the lowest-level programming language
understood by computers, to perform operations, and they could only solve one problem at a
time.
➢ The UNIVAC and ENIAC computers are examples of first-generation computing devices.
➢ The UNIVAC was the first commercial computer delivered to a business client, the U.S. Census
Bureau in 1951.
➢ The ENIAC used 18000 vacuum tubes occupied 1800Sq ft and consumed 180KW of Power.
➢ The transistor was invented at Bell Labs in 1947 but did not see widespread use in computers
until the late 1950s.
➢ The transistor was far superior to the vacuum tube, allowing computers to become smaller, faster,
cheaper, more energy-efficient and more reliable than their first-generation predecessors.
➢ Though the transistor still generated a great deal of heat that subjected the computer to damage, it
was a vast improvement over the vacuum tube.
➢ Second-generation computers still relied on punched cards for input and printouts for output.
➢ Second-generation computers moved from cryptic binary machine language to symbolic, or
assembly, languages, which allowed programmers to specify instructions in words.
➢ High-level programming languages were also being developed at this time, such as early versions
of COBOL and FORTRAN. These were also the first computers that stored their instructions in
their memory, which moved from a magnetic drum to magnetic core technology.
5
➢ 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 efficiency of computers.
➢ Instead of punched cards and printouts, users interacted with third generation computers through
keyboards and monitors and interfaced with an operating system, which allowed the device to
run many different applications at one time with a central program that monitored the memory.
➢ Computers for the first time became accessible to a mass audience because they were smaller and
cheaper than their predecessors.
➢ An integrated circuit (IC) is a small electronic device made out of a semiconductor material.
➢ Fifth generation computing devices, based on artificial 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 artificial intelligence a
reality.
➢ The goal of fifth-generation computing is to develop devices that respond to natural language
input and are capable of learning and self-organization.
➢ As complexity increases manifold the following things need to happen.
➢ Computer speed need to make an exponential jump, a feat that would be possible using
quantum computers. Quantum computation and molecular and nano technology will radically
change the face of computers in years to come. Example: Google’s D-Wave 2X
o Computers must be able to perform parallel processing so that multiple processors can
concurrently handle different aspects of a problem.
o Neural networks and expert systems have to be developed. These applications would be
able to make decisions and humans by analyzing data. 6
➢ Molecular Computing - Miniaturization to molecular levels.
1.5.1 Supercomputers
➢ The most powerful computers in terms of performance and data processing are the
Supercomputers.
➢ These are specialized and task specific computers used by large organizations.
➢ These computers are used for research and exploration purposes, like NASA uses
supercomputers for launching space shuttles, controlling them and for space exploration
purpose.
➢ Having most powerful and fastest processors.
➢ Super Computer use multiple CPU’s for parallel data processing.
➢ The fastest supercomputer operates at a speed of 34 petaflops (1 peta = 1000 tera = 1000000
giga).
➢ It is used for transaction processing.
➢ The supercomputers are very expensive and very large in size.
➢ It can be accommodated in large air-conditioned rooms; some super computers can span an
entire building.
➢ Example:
o Space Exploration
Supercomputers are used to study the origin of the universe, the dark-matters.
o Earthquake studies
Supercomputers are used to study the Earthquakes phenomenon. Besides that
supercomputers are used for natural resources exploration, like natural gas, petroleum,
coal, etc.
o Weather Forecasting
Supercomputers are used for weather forecasting, and to study the nature and extent
of Hurricanes, Rainfalls, windstorms, etc.
o Nuclear weapons testing
Supercomputers are used to run weapon simulation that can test the Range, accuracy
& impact of Nuclear weapons.
1.5.2 Mainframes
➢ Although Mainframes are not as powerful as supercomputers, but certainly they are quite
expensive.
➢ Many large firms & government organizations uses Mainframes to run their business
operations.
➢ Multi user machines that can support hundreds of users using the feature of time sharing
supported by Linux.
➢ The Mainframe computers can be accommodated in large air-conditioned rooms because
of its size.
7
➢ Super-computers are the fastest computers with large data storage capacity, Mainframes
can also process & store large amount of data.
➢ Example: Banks educational institutions & insurance companies use mainframe
computers to store data about their customers, students & insurance policy holders.
1.5.3 Minicomputers
➢ Minicomputers are used by small businesses & firms.
➢ Minicomputers are also called as “Midrange Computers”.
➢ These are small machines and can be accommodated on a disk with not as processing and
data storage capabilities as super-computers & Mainframes.
➢ The minicomputer which Digital Equipment Corporation(DEC) introduced in the 1970’s.
➢ These computers are not designed for a single user.
➢ Individual departments of a large company or organizations use Mini-computers for
specific purposes.
➢ Example: a production department can use Mini-computers for monitoring certain
production process.
1.5.4 Microcomputers
➢ Desktop computers, laptops, personal digital assistant (PDA), tablets & Smartphone’s are
all types of microcomputers.
➢ The micro-computers are widely used & the fastest growing computers.
➢ These computers are the cheapest among the other three types of computers.
➢ The Micro-computers are specially designed for general usage like entertainment,
education and work purposes.
➢ Well known manufacturers of Micro-computer are Dell, Apple, Samsung, and Sony &
Toshiba.
➢ Desktop computers, Gaming consoles, Sound & Navigation system of a car, Netbooks,
Notebooks, PDA’s, Tablet PC’s, Smartphone’s, Calculators are all type of
Microcomputers.
➢ The brain of the computer is the central Processing unit (CPU) represented by a single
chip on a PC.
➢ The CPU carries out every instruction stored in a program while interacting with other
agencies as and when necessary.
➢ Arithmetic & Logic Unit is a integral part of the CPU.
➢ RAM - Fast Memory.
➢ RAM is divided into a numbered of contiguously numbered cells.
➢ The number represents the addresses of the cells.
➢ The CPU access a memory location by sending its address to the memory unit.
➢ Primary memory is used for storing instructions and data of the currently in execution
(online storage).
➢ Secondary memory is used for storing data not required concurrently.
➢ A program is executed by loading instructions and data from secondary memory to
primary memory.
➢ It is a “super calculator” apart from carrying out all arithmetic operations it also performs
Boolean operations (like AND OR NOT) on the numbers.
10
Control Unit
➢ Control unit does not perform any computational task. But they control the way the data
gets transferred with different part in the computer using signals.
➢ The CPU uses high speed registers to store the current instruction and its data.
➢ One of the register called program counter maintains the address of the next instruction to
execute in the primary memory.
CPU Clock
➢ Both ALU and CU uses the services of clock use the services of the clock for sequencing
and synchronizing their operations.
➢ All the instructions are executed based on the principle of fetch-decode-execute.
➢ An instruction is fetched from the primary memory using the address stored in the program
counter.
➢ The program counters incremented to point to the next instruction.
➢ The fetched instruction is decoded to generate the signals required for operations.
➢ The converted instruction is executed by the CPU and the result is written back to register
and then to the main memory.
➢ The next cycle of fetch-decode-execute would now begin.
➢ All these activities requires clock which generate pulses at regular intervals of time.
➢ The CPU can execute only one instruction in one cycle.
➢ The instruction may take several cycles to get executed.
➢ The faster the clock, faster the CPU.
1) Static
2) Dynamic
➢ A Static RAM (SRAM) uses multiple transistors in each cell to store one bit of data.
Hence it takes more space.
➢ A Dynamic RAM (DRAM) uses one transistor and a leaky capacitor for each cell.
Capacitors hold the charge but due to inherent leakage, they have to get refreshed often.
➢ SRAM is faster, bigger and expensive than DRAM.
➢ RAM is slowest all primary memory types.
➢ Read only memory is a permanent memory that can be read but cannot be written.
➢ The contents of ROM are written during its manufacturing.
➢ Memory access is faster in ROM compared to RAM.
➢ ROM’s are non-volatile that is their contents are retained after the loss of power.
➢ ROM’s store special programs called BIOS (Basic Input Output Systems) this is a small
program who take care of the computer before control is handed over to OS.
o Programmable Read Only Memory (PROM)
• Left blank at the time of manufacturing.
• The device called the PROM programmer burns customer data in to the PROM.
But once written the contents of the ROM cannot be changed.
o Erasable Programmable Read only memory (EPROM)
• Can be rewritten even though it was previously burned. It can be erased by
exposing it to ultra-violet radiations.
o Electrically Erasable Programmable Read only memory (EPROM)
• Erased and rewritten multiple times.
• Flash drive and Pen drive are type of EEPROM.
➢ Cache memory stores those parts of the program that are frequently used by CPUs
➢ This is a small but a fast memory used as a buffer between RAM and CPU.
➢ CPU itself contains a Level 1 (L1) cache which is very fast.
➢ L2 cache is located out of CPU but close to it slower than L1 cache.
➢ L3 cache is smaller than the L2 and L1 cache.
➢ Cache memories are limited in size, expensive.
1.9.4 Registers
➢ The following are the forms of secondary memories and their sizes.
1. Hard disk including the portable disk (500 GB to 4 TB)
2. Magnetic tape(20 TB)
3. CD-ROM (700 MB-less than 1 GB)
4. DVD-ROM(4.7 GB and 8.5 GB)
5. Blu-ray disk(27 GB and 50GB)
6. Flash memory based on the EEPROM(1 GB to 128 GB)
7. The obsoleted floppy disk(1.2 MB and 1.44MB)
➢ Every disk contains spindle that holds one or more platters made of nonmagnetic materials
like glass or aluminium as shown in the below figure.
➢ Information is written on to or read from the platters using the read-write heads.
➢ Eight surfaces require eight heads and are mounted on a single arm & cannot be controlled
individually.
➢ Each surface has concentric or serially numbered tracks.
➢ Each track is further broken in to sectors or blocks.
➢ The disk spins constantly at speed of 5400 to 7200 rpm (rotation per minutes) the disk head
moves radially from track to track and when the head is positioned above a particular track,
all of its block passes through the head in a very short time.
➢ Access time is larger than RAM.
➢ The heads are not meant to touch the plotter surface, and if that happens, then the disk crash
results and data become unrecoverable.
o A parallel port, on the other hand, is an interface through which the communication
between a computer and its peripheral device is in a parallel manner i.e. data is
transferred in or out in parallel using more than one communication line or wire.
o Printer port is an example of parallel port.
RJ-45
o Ethernet is a networking technology that is used to connect your computer to Internet and
communicate with other computers or networking devices.
o The interface that is used for computer networking and telecommunications is known as
Registered Jack (RJ) and RJ – 45 ports in particular is used for Ethernet over cable.
o RJ-45 connector is an 8 pin – 8 contact (8P – 8C) type modular connector.
o The latest Ethernet technology is called Gigabit Ethernet and supports a data transfer rate of
over 10Gigabits per second.
PS/2
o PS/2 connector is developed by IBM for connecting mouse and keyboard.
o It was introduced with IBM’s Personal Systems/2 series of computers and hence the
name PS/2 connector.
o PS/2 connectors are color coded as purple for keyboard and green for mouse.
HDMI
15
1.12.1The Keyboard
➢ Keyboard is the most common and very popular input device which helps to input data to the
computer.
➢ The layout of the keyboard is like that of traditional typewriter, although there are some
additional keys provided for performing additional functions.
➢ Keyboards are of two sizes 84 keys or 101/102 keys, but now keyboards with 104 keys or
108 keys are also available for Windows and Internet.
Typing Keys
1 These keys include the letter keys (A-Z) and digit keys (09) which generally give the same
layout as that of typewriters.
Numeric Keypad
It is used to enter the numeric data or cursor movement. Generally, it consists of a set of
2
17 keys that are laid out in the same configuration used by most adding machines and
calculators.
Function Keys
The twelve function keys are present on the keyboard which is arranged in a row at the top
3
of the keyboard. Each function key has a unique meaning and is used for some specific
16
purpose.
Control keys
These keys provide cursor and screen control. It includes four directional arrow keys.
4
Control keys also include Home, End, Insert, Delete, Page Up, Page Down, Control(Ctrl),
Alternate(Alt), Escape(Esc).
1.12.2 Scanner
➢ Graphical user interfaces (GUI) like windows need a pointing device to control the
movement of the cursor on the screen.
➢ Mouse in the desktop.
➢ Touchpad in the laptop.
➢ The earliest form of the mouse that is connected to a computer port has a rotating ball at
the bottom and 2 buttons at the top.
➢ The buttons are generally used in the following ways:
o Double clicking of the same button activates a program. A text section can be
selected by dragging the mouse with left button pressed (no clicking here). Left
click on a text segment is also required to commence text entry.
17
o Right Clicking button activates a context sensitive menu used to change the
attributes of the selected object. Supports options for copying and pasting text and
change the properties of the selected object.
➢ Visual Display Unit (VDU), are the main output device of a computer.
➢ It forms images from tiny dots, called pixels that are arranged in a rectangular form. The
sharpness of the image depends upon the number of pixels.
➢ There are two kinds of viewing screen used for monitors.
• Cathode-Ray Tube (CRT)
• LCD Monitors.
➢ Cathode-Ray Tube (CRT) Monitor
• The CRT display is made up of small picture elements called pixels. The smaller the
pixels, the better the image clarity or resolution.
• It takes more than one illuminated pixel to form a whole character, such as the letter
‘e’ in the word help.
➢ LCD Monitors
• LCD screen comprises thousands of liquid crystals, don’t generate light but generate
light allow or block the passage of light through them.
• An image is formed by selectively applying a voltage to these crystals and using a
separate light source for light to pass through them.
1.13.2 Impact Printer
➢ Impact printers print the characters by striking them on the ribbon, which is then pressed
on the paper.
➢ Characteristics of Impact Printers are the following
• Each character printed is in the form of pattern of dots and head consists of a
Matrix of Pins of size (5*7, 7*9, 9*7 or 9*9) which comes out to form a character
which is why it is called Dot Matrix Printer.
• Advantages
▪ Inexpensive
▪ Widely Used
▪ Other language characters can be printed
• Disadvantages
▪ Slow Speed.
▪ Poor Quality.
➢ Daisy Wheel
• Head is lying on a wheel and pins corresponding to characters are like petals of
Daisy (flower) which is why it is called Daisy Wheel Printer.
• These printers are generally used for word-processing in offices that require a few
letters to be sent here and there with very nice quality.
• Advantages
• Advantages
• Disadvantages
▪ Very expensive.
▪ Characters fonts cannot be changed.
o Laser Printers
o Inkjet Printers
➢ Characteristics of Non-impact Printers
o Faster than impact printers
o They are not noisy
o High quality
o Supports many fonts and different character size
➢ Laser Printers
o They use laser lights to produce the dots needed to form the characters to be printed
on a page.
o It works like a photocopier.
o A laser beam creates an image of the page to be printed on a light sensitive drum.
o The Charged area attracts black magnetic powder, called the toner.
o The image is created in the form of dots then transferred from the drum to the paper
by actual contact.
o A separate roller heats up the paper to melt the toner which then gets fused on to the
paper.
o Laser printers have built in RAM for storing documents.
o The RAM acts as a buffer which can free up the computer after a job has been
submitting for printing.
o The resolution varies from 300dpi to 1200dpi (dots per inch).
o Speeds of 20ppm while fastest printers operate at 200ppm (pages per minute). 20
o Advantages
▪ Expensive
▪ Cannot be used to produce multiple copies of a document in a single printing.
o Disadvantages
▪ Expensive as the cost per page is high.
▪ Slow as compared to laser printer.
1.13.4 Plotters
➢ A plotter is a printer that interprets commands from a computer to make line drawings on
paper with one or more automated pens.
➢ The plotter can draw continuous point-to-point lines directly from vector graphics files or
commands.
➢ There are a number of different types of plotters: a drum plotter draws on paper wrapped
around a drum which turns to produce one direction of the plot, while the pens move to
provide the other direction; a flatbed plotter draws on paper placed on a flat surface; and 21
an electrostatic plotter draws on negatively charged paper with positively charged toner.
➢ They are also slow and expensive.
➢ Plotters can handle large paper sizes, and thus suitable for creating drawings of buildings
and machines.
➢ A computer network is a system in which multiple computers are connected to each other
to share information and resources.
➢ Share resources from one computer to another.
➢ Create files and store them in one computer, access those files from the other computer(s)
connected over the network.
➢ Connect a printer, scanner, or a fax machine to one computer within the network and let
other computers of the network use the machines available over the network.
➢ All networked nodes need a common addressing scheme that would ensure delivery of
data to the right node.
➢ Ethernet- a hardware standard that defines the addressing, cabling and signaling
requirements of the network.
➢ A network topology is the arrangement of a network, including its nodes and connecting
lines.
➢ There are a number of ways or topologies of connecting computers, each having its own
merits and demerits.
➢ Bus Topology
o Every workstation is connected to a main cable called the bus. Therefore, in
effect, each workstation is directly connected to every other workstation in the
network.
o The failure of a single node doesn’t disrupt the communication between the
remaining nodes.
➢ Star Topology
o There is a central computer or server to which all the workstations are directly
connected.
o Every workstation is indirectly connected to every other through the central 22
computer.
o If hub fails entire network fails.
o New node can be added without disrupting the service.
➢ Ring Topology
o The workstations are connected in a closed loop configuration.
o Adjacent pairs of workstations are directly connected.
o Other pairs of workstations are indirectly connected, the data passing through one
or more intermediate nodes.
o In Uni-directional rings, failure of on node causes the network to shut down.
➢ Mesh Topology
o Employs either of two schemes, called full mesh and partial mesh.
o In the full mesh topology, each workstation is connected directly to each of the
others.
o In the partial mesh topology, some workstations are connected to all the others,
and some are connected only to those other nodes with which they exchange the
most data.
o When a node breaks down the packet simply changes its route.
o Expensive of all the topologies.
➢ Network transmission begins from Network Interface Card that is housed inside the
computer.
➢ It is referred as Ethernet Card.
➢ The Functionality is implemented in the following ways:
1. Built into the motherboard of laptops and current desktops.
2. As a separate card inserted into a slot on the motherboard.
3. As a wireless card inserted needs no cable to connect to the network.
4. As a USB adapter that runs on the USB port.
➢ Ethernet card has an address hard- coded into the board by the card manufacturer
➢ This address known as MAC (Machine Access Control) address unique to the card.
➢ Ethernet cards can have the same MAC address. The card is connected to a hub or switch
via a cable
➢ Single network and using star topology need a central device to connect to a and this
device is either a hub or a switch.
➢ A hub accepts network data from a computer and simply broadcasts the data to all nodes.
➢ Since the data contain the MAC address of the destination, only the node having that 24
address will accept the data.
➢ The technology is wasteful because data have to travel to all nodes even though only one
among them will accept it.
➢ A Switch is intelligent because it has a table of all MAC addresses of the connected
devices.
➢ The switch extracts the MAC address from the data and then looks up the table to
determine the node the packet is meant for.
➢ The packet is then sent only to that node.
➢ A switch is efficient and is able to maintain the speed of a network.
➢ Bridge- As electrical signals travel along a cable, they weaken, so Ethernet sets a limit on
the maximum size of the cable (100 meters).
➢ Generally only one device is able to transmit at a time, too many nodes can lead to
network congestion.
➢ A network supporting many nodes spilt into a number of segments with a bridge
connecting them.
➢ A bridge maintains a table of all MAC addresses machines in the segments connected to
it.
➢ It connects 2 networks using the same protocol (Ethernet).
➢ Functionality has been over by the switch.
➢ Router- It connects 2 similar or dissimilar networks which may be separated by long
distance.
➢ Routing looks up its routing table to determine the path a packet has to travel to reach its
destination.
➢ If the destination node is on the same network then the router sends the packet directly to
the node otherwise it sends it to another router that takes it closer to its destination.
➢ A router is smarter than a bridge because it can choose the best route for guiding the
packet.
➢ The variables that are used in more than one function are called global variables and are
declared in global declaration section.
➢ The global declaration section is out of all user defined functions.
➢ The global declaration section also declares the user defined functions.
➢ Every C program must have one main ( ) function section.
➢ The main function has two parts.
1. The declaration section and the executable section.
2. The declaration part declares all the variables used in the executable part. There
should be at least one statement in the executable part.
➢ Declaration and executable parts should be within the opening and closing braces of the
main function.
➢ The execution of the program starts at opening braces and ends at the closing braces.
Closing braces is the logical end of the C program.
➢ All statements other than conditional branching and looping statements should end with
semi colon.
➢ The sub program section contains all the user defined functions that are called in the main
function.
➢ All sections, except main function section can be absent when they are not required.
Programming Style
An example considers the C program for addition of two numbers and the value of π.
#include<stdio.h> //includes functions for input and output operations. 27
/* definition section*/
#define PI 3.14 //defined the symbolic constant
/*global declaration section*/
OUTPUT:
Executing a Program.
➢ Executing a C program involves the following steps.
1. Creating the program.
2. Compiling the program.
3. Linking the program with the functions that are needed from the C library.
4. Executing the program.
➢ Figure 1.2 shows the steps involved in compiling and running a program.
1. cc hello.c
➢ Each instruction is examined for its correctness and translated to the form suitable for
execution.
➢ The translated source program is stored under file with name filename.o (hello.o). This
program is known as object file/code.
➢ Linking is the process of putting together other required program files and functions to
our program. For , when we use mathematical functions like sine( ), cos( ), expo( ) in our
program, we use the extension –lm with cc command as below,
1. cc hello.c –lm
➢ To compile and link multiple source program files we must append all the files names to
the cc command.
▪ cc filename- 1.c …. filename- n.c
➢ These files will be separately compiled into object files called
▪ filename- i.o
➢ And then linked to produce an executable program file a.out as shown in figure 1.11
29
.C .C .C
.O .O Library
.O
a.out
30
31
32
A single character constant contains a single character enclosed with in a pair of single quote
marks.
Examples are as follows, ‘5’ ‘X’ ‘;’ ‘’
Character constant ‘5’ is not same as the digit 5.
‘’ is a blank space constant.
Every character constant is associated with a integer value called as ASCII value. For example
o printf (“%d”,a); would print the number 97, the ASCII value of a.
o Since each character is associated with a integer value, it is possible to perform
arithmetic operations on character constants.
2.3 VARIABLES
A variable is a data name that may be used to store a data value.
A variable may take different values at different times during the execution of the program.
The variable names should be of 31 characters. Some compilers consider only 8 characters out
of 31.
Variables are case sensitive. That is, Total and total are not same.
Unsigned integers use all the bits for magnitude of the numbers and are always positive.
Table 1.8 shows the range of int, short int and long int.
To increase the accuracy of the floating point number, double data type can be used.
Declaration of variable must be done before they are used in the program.
It tells the compiler what the variable name is
It specifies what type of data the variable will hold.
Floating point
Double-precision floating point
Extended double precision floating point
Type definition is a feature that that allows user to define an identifier that would represent
an existing data type.
The user defined data type identifier can later be used to declare variables.
The syntax of type definition is as follows,
o typedef type identifier;
Example,
In the above example, unit symbolizes int and marks symbolizes float. They can be
used to declare variables as follows,
The main advantage of typedef is to create meaningful data type names for increasing the
readability of the program.
Enumerated data type is another form of user defined data type which is defined as
follows,
o enum identifier {value1,value2,…valuen};
The identifier in the above syntax is an user defined enumerated data type which can be
used to declare variables that can have one of the values enclosed in the braces known as
enumeration constants.
After the definition, enumeration variables can be declared as
o enum identifier v1,v2,v3,…vn;
o v1=value3;
As an example,
The compiler automatically assigns integer digits beginning with 0 to all the enumeration
constants. i.e, 1st enumeration constant has the value 0 and the counting follows.
The automatic assignments of values to the enumeration constant can be overridden
assigning values explicitly.
enum day { Monday=1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
The storage class decides the portion of program with in which the variables are
recognized.
{
int i; Global variables are also called as
float balance;
external variables.
……..
….....
Function1( ); The variables i, balance and sum are
} local variables declared with in main
Function1( ) and function1.
{
When a variable is declared as volatile, the compiler will examine the value of the variable
each time it is encountered to see if any alterations had been made to the value.
The variable declared as volatile can also be changed by its own program.
If we do not want a variable to be changed by its own program but wants to get changed by
external programs then we need to declare the variable both as constant and volatile as
shown below.
3.1Arithmetic Operators
Prof Kavya M S & Prof Shridevi Desai
Dept of CSE, TJIT
Module1
Computer Fundamentals & C Programming
1
Table 1.11 show the list of arithmetic operators
The unary minus operator multiplies its single operand by -1. Therefore, a number preceded by
minus sign changes its sign.
The integer division truncates any fractional part.
The modulo division operator produces the remainder of an integer division.
o Examples of usage of arithmetic operators are as follows,
a-b a*b a%b
a+b a/b -a*b
Note that a and b are variables and are called as operands.
The modulo division operators cannot be used on floating point data.
When both the operands in a single arithmetic expression such as a+b are integers, the
expression is called an integer expression. And the operation is called integer arithmetic.
Integer arithmetic always yields an integer value. If a and b are integers, then a=14 and b=4 we
have the following results.
During modulo division, the sign of the result is always the sign of the first operand(the
dividend).
Example, -14%3=-2
When one of the operands is real and the other is integer, the expression is called Mixed
mode arithmetic expression.
If either operand is of real type, then only the real operation is performed and the result is
always a real number. Thus 15/10.0=1.5.
Precedence of operator
The order in which different operators are used to evaluate an expression is called
precedence/hierarchy of operators.
The operators are evaluated from higher precedence to lower precedence.
Associativity of operator
Associativity determines how operators with the same precedence are evaluated in an expression.
o Precedence is applied before Associativity and Associativity is applied if
operators have same precedence.
Associativity has two types.
o Left Associativity: In an expression if two or more operators have same priority and are
evaluated from left to right then operators are called left to right associative. Example ,
4/2*3
o Right Associativity: In an expression, if two or more operators have same priority and are
evaluated from right to left then operators are called right to left associative. Example,
assignment operator.
o High priority * / %
o Low priority + -
The evaluation of arithmetic expressions involve two left to right passes through the expression.
In the first pass, the high priority operators are applied as they are encountered.
In the second pass the low priority are applied as they are encountered.
Example, x=a-b/3+c*2-1 where a=9, b=12 and c=3 the statements become
o X=9-12/3+3*2-1
o And it is evaluated as follows,
o First pass
o Step 1:x=9-4+382-1
o Step 2:x=9-4+6-1
o Second pass
o Step 3: x=5+6-1
o Step 4: x=11-1
o Step 5: x=10
Problem: evaluate the expression 2*((a%5)*(4+(b-3)/(c+2))) Assuming a=8,b=15 and c=4,
Solution:
Relational operators
Relational operators are the operators used to compare two operands.
A simple relational expression takes contains only one relational operator and takes the following
form.
o ae-1 relational operator ae-2
Where, ae-1 and ae-2 are arithmetic expressions, which may be simple constants,
variables or combination of them. The table 1.13 shows the list of relational operators.
Logical operator
The logical operators are the operators that combine two or more relational expressions.
o An expression which combines two or more relational expressions using logical
operators is called as logical expressions or compound relational expressions.
o The following table 1.14 shows the available relational operators and their
meaning, priority and associativity in C.
Logical expressions also yield a value of one or zero according to the truth table 1.15.
o Evaluate braces
o 11+2>6&&1||11!=7&&11-2<=5
o 13>6&&1||11!=7&&9<=5
o 1&&1||11!=7&&9<=5
o 1&&1||11!=7&&0
o 1&&1||1&&0
o 1||1&&0
o 1||0
o 1
Assignment operators
Assignment operators are used to assign the result of an expression to a variable.
o Simple assignment
o Multiple assignments.
A statement using which a value or a set of values are assigned to different variables is called
multiple assignment statement.
a=b=c=10;
The operators such as +=, -=, *=,/=,%= are called as shorthand assignment statements.
Prof Kavya M S & Prof Shridevi Desai
Dept of CSE, TJIT
Module1
Computer Fundamentals & C Programming
1 associative.
Short hand assignment operators are right to left
Syntax: v op=exp;
o Example, x+=y+1; x = x
+ (y+1) X+=3; x=x+3
o What appears on the left-hand need not be repeated and therefore it becomes easier to write.
o Evaluate braces
o Evaluate unary operator
c=c-5 c=2
o a+=b*=2
a+=6
b=b*2
a=a+6
b=3*2
Prof Kavya M S & Prof Shridevi Desai
Dept of CSE, TJIT
Module1
Computer Fundamentals & C Programming
a=1+6 b=6 1
a=7
Post increment
Operand value is used first and then the operand value is incremented by 1.
Pre increment
Increment operator is placed before (pre) the variable.
o a=1;
Pre decrement
Operand value is decremented by 1 first and then the Operand value is used.
o a=3;
Operand value is used first and then the operand value is decremented by 1.
o a=3;
Increment and decrement operators are unary operators and they require variables as their operands.
When postfix ++ or -- is used with a variable in an expression, the expression is evaluated first
using original value of the variable and the variable is incremented or decremented by 1.
The precedence and associativity of ++ and – operator are the same as those of unary + and unary -
Syntax: (exp1)?exp2:exp3;
Bitwise operator
Bitwise operators are used to manipulate data at the bit level.
Special operators
C supports some special operators such as,
o Comma operator
o Sizeof operator
Example, printf(“%d”,”%d”,a,b);
o Sizeof is a compile time operator when used with an operand it returns the number of bytes
of memory occupied by the operands. Operands may be a variable, a constant or a data type.
o Example
m=sizeof(sum);
n=sizeof(long
int);
k=sizeof(235L);
o Implicit conversion
o Explicit conversion
Implicit conversion
The process of conversion of data from lower rank data type to higher rank data type automatically by
the C
o compiler is called as implicit conversion.
o If both the operands are of same rank no conversion takes place and the results is same as the
operands.
If one operand is of low rank and the other is of higher rank then the low rank operand is converted in
to high rank and the result is high ranked.
Explicit conversion
The process of forcible conversion of data from one data type to another is called as explicit type
conversion.
Syntax: (type_name) expression;
Type can be any required data type and expression may be operand such as variable or
a constant. int p=7;
o int q=2; float r; r=p/q;
o Without explicit conversion r=7/2=3.00
o With explicit conversion r=(float)7/2=3.500000
Example Action
x= (int) 7.5 7.5 is converted to integer by truncation
a= (int) 21.3/(int) 4.5 Evaluated as 21/4 and the result would be 5
b= (double)sum/n Division is done in floating point mode
y= (int) (a+b) The result of a+b is converted to integer
Prof Kavya M S & Prof Shridevi Desai
Dept of CSE, TJIT
Module1
Computer Fundamentals & C Programming
z= (int)a+b 1 a is converted to integer and then added to b
p= cos((double)x) Converts x to double before using it.