Module-1 New
Module-1 New
Module-1 New
Introduction to C
OBJECTIVES
To introduce computers and its parts.
To introduce to C language.
To understand Files used in a C program.
To understand the Compilers, Compiling and executing C programs.
Discuss the variables, constants, Input/output statements in C.
1.1Introduction to Computers
A computer is a programmable machine designed to perform arithmetic and logical
operations automatically and sequentially on the input given by the user and gives the
desired output after processing. Computer components are divided into two major categories
namely hardware and software. Hardware is the machine itself and its connected devices such as
monitor, keyboard, mouse etc. Software are the set of programs that make use of hardware for
performing various functions.
Generally, the term is used to describe a collection of devices that function together as a
system. It performs the following three operations in sequence.
1. It receives data & instructions from the input device.
2. Processes the data as per instructions.
3. Provides the result (output) in a desired form.
Data: It is the collection of raw facts, figures& symbols. Ex : Names of students and their marks in
different subjects listed in random order.
Information: It is the data that is processed & presented in an organized manner. Ex: When the
names of students are arranged in alphabetical order, total and average marks are calculated &
presented in a tabular form, it is information.
Program: Set of instructions that enables a computer to perform a given task.
Characteristics of Computers
The characteristics of computers that have made them so powerful and universally useful
are speed, accuracy, diligence, versatility and storage capacity. Let us discuss them briefly.
Speed:Computers work at an incredible speed. A powerful computer is capable of performing
about 3-4 million simple instructions per second.
Accuracy:In addition to being fast, computers are also accurate. Errors that may occur can usually
be attributed to human error (inaccurate data, poorly designed system or faulty
instructions/programs written by the programmer).
Diligence:Unlike human beings, computers are highly consistent. They do not suffer from human
traits of boredom and tiredness resulting in lack of concentration. Computers, therefore, are better
than human beings in performing voluminous and repetitive jobs.
Versatility: Computers are versatile machines and are capable of performing any task as long as it
can be broken down into a series of logical steps. The presence of computers can be seen in almost
every sphere – Railway/Air reservation, Banks, Hotels, Weather forecasting and many more.
Storage Capacity: Today’s computers can store large volumes of data. A piece of information
once recorded (or stored) in the computer, can never be forgotten and can be retrieved almost
instantaneously.
Cost effectiveness:Computers reduce the amount of paper work and human effort, thereby
reducing costs.
Automation: Computers can be instructed to perform complex tasks automatically (which
increases the productivity).
Limitations of computers:
1. Computers need clear & complete instructions to perform a task accurately. If the instructions
are not clear & complete, the computer will not produce the required result.
2. Computers cannot think.
3. Computers cannot learn by experience.
Computer Organization
A computer system (Fig.1.1) consists of mainly four basic units; namely input unit, storage unit,
central processing unit and output unit. Central Processing unit further includes Arithmetic logic
unit and control unit, as shown in Figure 1.2.
Fig 1.1: Computer System
A computer performs five major operations or functions irrespective of its size and make. These
are:
It accepts data or instructions as input,
It stores data and instruction
It processes data as per the instructions,
It controls all operations inside a computer, and
It gives results in the form of output
Functional Units:
Input Unit: This unit is used for entering data and programs into the computer system by the user
for processing.
Storage Unit: The storage unit is used for storing data and instructions before and after processing.
Output Unit: The output unit is used for storing the result as output produced by the
computer after processing.
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. CPU includes Arithmetic logic unit (ALU) and
control unit (CU)
Arithmetic Logic Unit: All calculations and comparisons, based on the instructions provided,
are carried out withinthe ALU. It performs arithmetic functions like addition, subtraction,
multiplication, division and logical operations like greater than, less than and equal to etc.
Control Unit:Control unit performs controlling of all operations like input, processing and
output. It takes care of step by step processing of all operations inside the computer.
Memory
Computer’s memory can be classified into two types; primary memory and secondary memory
a.Primary Memory can be further classified as RAM and ROM.
RAM or Random Access Memory is the unit in a computer system. It is the place in a computer
where the operating system, application programs and the data in current use are kept
temporarily so that they can be accessed by the computer’s processor. It is said to be ‘volatile’
since its contents are accessible only as long as the computer is on. The contents of RAM are no
more available once the computer is turned off.
ROM or Read Only Memory is a special type of memory which can only be read and contents
of which are not lost even when the computer is switched off. It typically contains manufacturer’s
instructions. Among other things, ROM also stores an initial program called the ‘bootstrap
loader’ whose function is to start the operation of computer system once the power is turned on.
b. Secondary Memory
RAM is volatile memory having a limited storage capacity. Secondary/auxiliary memory is
storage other than the RAM. These include devices that are peripheral and are connected and
controlled by the computer to enable permanent storage of programs and data. Secondary
storage devices are of two types; magnetic and optical. Magnetic devices include hard disks and
optical storage devices are CDs, DVDs, Pen drive, Zip drive etc.
Hard Disk
Hard disks are made up of rigid material and are usually a stack of metal disks sealed in a box. The
hard disk and the hard disk drive exist together as a unit and is a permanent part of the
computer where data and programs are saved. These disks have storage capacities ranging from
1GB to 80 GB and more. Hard disks are rewritable.
Compact Disk
Compact Disk (CD) is portable disk having data storage capacity between 650-700 MB. It can hold
large amount of information such as music, full-motion videos, and text etc. CDs can be either read
only or read write type.
Digital Video Disk
Digital Video Disk (DVD) is similar to a CD but has larger storage capacity and enormous clarity.
Depending upon the disk type it can store several Gigabytes of data. DVDs are primarily used to
store music or movies and can be played back on your television or the computer too.
These are not rewritable.
Flash Drive
It is a small, portable device that can be used to store, access and transfer data. Due to its small
size, it is commonly called Pen drive. It is also called USB drive. We can read, write, copy, delete,
and move data from computer to pen drive or pen drive to computer. It comes in various storage
capacities of 2GB, 4GB, 8GB etc. It is popular because it is easy to use and small enough to be
carried in a pocket. This device is plugged into the USB port of the computer and the computer
automatically detects this device.
b) Output Devices:
Output device receives information from the CPU and presents it to the user in the desired from.
The processed data, stored in the memory of the computer is sent to the output unit, which then
converts it into a form that can be understood by the user. The output is usually produced in one of
the two ways – on the display device, or on paper (hard copy).
Monitor: is often used synonymously with “computer screen” or “display.” Monitor is an output
device that resembles the television screen It may use a Cathode Ray Tube (CRT) to
display information. The monitor is associated with a keyboard for manual input of
characters and displays the information as it is keyed in. It also displays the program or application
output. Like the television, monitors are also available in different sizes.
Printer: Printers are used to produce paper (commonly known as hardcopy) output. Based on
the technology used, they can be classified as Impact or Non-impact printers.
Impact printers use the typewriting printing mechanism wherein a hammer strikes the paper
through a ribbon in order to produce output. Dot-matrix and Character printers fall under this
category.
Non-impact printers do not touch the paper while printing. They use chemical, heat or
electrical signals to etch the symbols on paper. Inkjet, Deskjet, Laser, Thermal printers fall
under this category of printers.
Plotter: Plotters are used to print graphical output on paper. It interprets computer commands
and makes line drawings on paper using multicoloured automated pens. It is capable of
producing graphs, drawings, charts, maps etc.
Facsimile (FAX): Facsimile machine, a device that can send or receive pictures and text over a
telephone line. Fax machines work by digitizing an image.
Sound cards and Speaker(s): An expansion board that enables a computer to manipulate and
output sounds. Sound cards are necessary for nearly all CD-ROMs and have become
commonplace on modern personal computers. Sound cards enable the computer to output
sound through speakers connected to the board, to record sound input from a microphone
connected to the computer, and manipulate sound stored on a disk.
1.4. Introduction to C
Programming is the process of taking an algorithm and encoding it into a notation, a programming
language, so that it can be executed by a computer. Although many programming languages and
many different types of computers exist, the important first step is the need to have the solution.
Without an algorithm, there can be no program.
Computer science is not the study of programming. Programming, however, is an important part of
what a computer scientist does. Programming is often the way that we create a representation for
our solutions. Therefore, this language representation and the process of creating it becomes a
fundamental part of the discipline.
Algorithms describe the solution to a problem in terms of the data needed to represent the problem
instance and the set of steps necessary to produce the intended result. Programming languages must
provide a notational way to represent both the process and the data. To this end, languages provide
control constructs and data types.
Control constructs allow algorithmic steps to be represented in a convenient yet unambiguous way.
At a minimum, algorithms require constructs that perform sequential processing, selection for
decision-making, and iteration for repetitive control. As long as the language provides these basic
statements, it can be used for algorithm representation.
All data items in the computer are represented as strings of binary digits. In order to give these
strings meaning, we need to have data types. Data types provide an interpretation for this binary
data so that we can think about the data in terms that make sense with respect to the problem being
solved. These low-level, built-in data types (sometimes called the primitive data types) provide the
building blocks for algorithm development.
Programming paradigms:
A Programming paradigm is a style or “way” of programming.
1. Imperative: The language provides statements, such as assignment statements, which explicitly
change the state of the memory of the computer. In this the emphasize is on the idea of
computation as being about evaluating mathematical functions combined in expressions. While all
languages try to provide a representation of basic functions like addition, functional languages
support a functional way of expressing computations on large and complex structures, although
some such as Scheme also have imperative features. In a pure functional language a mathematical
identity like:fred(x) + fred(x) = 2*fred(x) should hold.
2. At times the programmer may want to change or add the subroutines and reflect those changes in
all the programs. For doing this, he will have to only change the source file for the subroutines,
recompile the source code and then recompile and re-link the program.
Standard header files
C provides us with some standard header files which are available easily.
The header files are added at the start of the source code so that they can be used by more than one
function of the same file.pile the source code and then recompile and re-link the program.
Object files
• They are the files that are generated by the compiler as the source code file is processed.
• These files generally contain the binary code of the function definitions.
• The object file is used by the linker for producing an executable file for combining the
object files together. It has a '.o' extension.
Executable file
• This file is generated by the linker.
• Various object files are linked by the linker for producing a binary file which will be
executed directly.
• They have an '.exe' extension.
Table1.1:DatatypessupportedinC
FORMA
MEMORY
DATATYPE RANGE T
(BYTES) SPECIFIER
shortint 2 -32,768to 32,767 %hd
unsignedshort int 2 0 to 65,535 %hu
unsignedint 4 0 to 4,294,967,295 %u
-2,147,483,648to
int 4 2,147,483,647 %d
-2,147,483,648to
long int 4 2,147,483,647 %ld
unsignedlong int 4 0 to 4,294,967,295 %lu
long longint 8 -(2^63)to(2^63)-1 %lld
unsigned 0 to
8 %llu
longlongint 18,446,744,073,709,551,615
signedchar 1 -128to 127 %c
unsignedchar 1 0 to 255 %c
float 4 %f
double 8 %lf
long double 12 %Lf
Standard header files
C provides us with some standard header files which are available easily.
The header files are added at the start of the source code so that they can be used by more than one
a)Input Devices
function of the same file.