Module 1 POP Notes
Module 1 POP Notes
Module 1: Introduction to C
Computer: A computer in simple terms can be defined as an electronic device that is designed to
accept data, perform the required mathematical and logical operations at high speed and output
the result.
Characteristics of computer:
1. Speed:
2. Accuracy:
A computer is a very fast, reliable and robust electronic device. It always gives accurate results,
provided the correct data and set of instructions are input to it. Hence in the event of an error,
only the user who has fed the incorrect data/program needs be held responsible. If the input is
wrong, then the output will also be erroneous, In computer terminology, this is known as
garbage-in garbage-out(GIGO).
3. Automation:
Computers are automatable devices that can perform a task without any user intervention. The
user just needs to assign the task to the computer, after which it automatically controls different
devices attached to it and executes the program instruction.
4. Diligence:
Unlike humans, a computer never gets tired of a repetitive task. It can continually work for hours
without creating errors.
5. Versatile:
Versatility is the quality of being flexible .Today, computers are used in our daily life in different
fields. For example they are used as personal computers (PCs)for home use, for business-
oriented tasks ,weather forecasting, space exploration,teaching,railways,banking,medicine, and
so on, indicating that computers can perform different tasks simultaneously.
6. Memory:
similar to humans, computers also have memory .Just the way we cannot store everything in our
memory and need secondary media, such as a notebook to record certain important things,
computers also have internal or primary memory(storage space) a s well as external or secondary
Principles of Programming using C Module 1
memory. While the internal memory of computers is very expensive and limited in size the
secondary storage is cheaper and of bigger capacity.
7. No IQ:
Although the trend today is to make computers intelligent by including artificial intelligence (AI)
in them, they still do not have any decision making abilities of their own. That is, their IQ level is
zero. They need guidance to perform various tasks.
8. Economical:
Using computers also reduce man power requirements and leads to an elegant and efficient way
of performing various tasks. Hence, computers save time, energy, and money. When compared
to other systems, computers can do more work in lesser time.
This generation computer is used thousands of vacuum tubes for circuitry for CPU
(Central processing unit)
Memory requirement were met by magnetic drums.
Took lot of space consumed enormous amount of power and generated lot of heat and
also was very expensive.
These computers used machine language.
Some computers of this generation were: ENIAC, EDVAC, UNIVAC etc..
Program input was provided by punch cards and outputs were produced on paper.
Transistors were used that were cheaper, consumed less power, more compact in size,
more reliable and faster.
Magnetic cores were used as the primary memory and magnetic tape and magnetic disks
as secondary storage devices.
These computers used assembly language (programmer could specify words and symbols
as instructions instead of 0’s and 1’s).
Some computers of this generation were: IBM 1620, IBM7094.
A single IC has many transistors, resistors and capacitors along with the associated
circuitry.
This development made computers smaller in size, reliable and efficient.
In this generation remote processing, times sharing multi-programming operating system
were used.
High level language (FORTRAN, COBOL, PASCAL, BASIC, ALGOL) were used during this
generation.
Keyboard and monitor were used to interact with user.
Memory capacity increased substantially by the use of hard disks for secondary memory.
Some computers of this generation were: IBM360 series, Honeywell-6000 series, and
personal data processor.
Used very large scale integrated (VLSI) circuits having about 5000 transistors and other
circuit elements with their associated circuits on a single chip.
The microprocessor launched the fourth generation of computer, with thousands of
integrated circuits built onto a single silicon chip.
Computers became more powerful, compact, reliable and affordable.
As a result, it gave rise to personal computer revolution, laptops and smart phones
offering gigabytes of memory compared to few megabytes.
In this generation, time sharing, real time networks, distributed operating system were
used the high level languages like C,C++,DBASE etc..Were used in this generation
Fourth generation computers also saw the development of graphical user interface, the
mouse, and handled devices.
With ULS (ultra large scale integration) technology, microprocessor chips having ten
million electronic components were manufactured.
This generation is based on parallel processing hardware and AI.
AI is an emerging branch in computer science which interprets the means and method of
making computers think like human beings.
All the high-level languages like C and C++,java ,.Net etc..are used in this generation .
Neural networks and experts systems have to be developed.
Classification of computer:
Computers are broadly classified into 4 types based on speed, amount of data that they can hold
and price.
Principles of Programming using C Module 1
1. Super computer:
The super computer is the fastest, most powerful and most expensive computer.
Supercomputers were first developed in the 1980’s to process large amounts of data and
to solve complex scientific problems.
A single supercomputer can support thousands of users at the same time.
Computers are mainly used for weather forcasting, nuclear energy research, aircraft
design, automotive design, online banking, controlling industrial units, etc.
1. Dumb terminal:
Principles of Programming using C Module 1
Dumb terminals consist of only a monitor and a keyboard (or mouse).They do not have their own
CPU, memory and use the mainframe systems CPU and storage devices.
2. Intelligent terminals:
Intelligent terminals have their own processor and thus can perform some processing
operations..They do not have their own storage space.
3. Mini computer:
4. Micro computers:
Displaying data
Controlling and coordinating all operations inside a computer.
Input:
This is the process of entering data instructions into the computer system.
The data and instructions can be entered by using different input devices such as
keyboard, mouse, scanner, trackball, etc...
Note that the computers understand binary language, which consist of only two symbols
(0 and 1), so it is the responsibility of the input devices to convert the input data into
binary codes.
Storage:
Storage is the process of saving data and instructions permanently in the computer so that they
can be used for processing.
1. Primary storage:
Primary storage also known as the main memory is the storage area that is directly
accessible by the CPU at very high speed.
Drawback of main memory is that it is volatile in nature that is as soon as the
computer is switched off, the information stored gets erased.
RAM and ROM are the examples of primary storage
RAM:
ROM:
It stands for read only memory the data stored in these devices are non-volatile i.e. once
the data stored in the memory cannot be modified or deleted.
The memory form which will only read but cannot write.
The information is stored permanently.
There are 3 types
1. PROM (programmable ROM) :Data cannot be altered
2. EPROM(Erasable Programmable ROM): Possible to erase the information
3. EEPROM (electrically Erasable ROM): non-volatile memory used in computers,
usually integrated in microcontroller
1. Secondary storage:
Also known as auxiliary memory this is just the opposite of primary memory.
It basically overcomes all the drawbacks of the primary storage area.
It is cheaper, non-volatile and used to permanently store data and programs of those jobs
that are not being currently executed by the CPU.
Processing:
The process of performing operations on the data as per the instructions specified by the
user(programs) is called processing.
Data and instructions are taken from the primary memory and transferred to the
arithmetic and logical unit, which performs all sorts of calculations.
Principles of Programming using C Module 1
The intermediate results of processing may be stored in the main memory as they be
required again.
When processing completes the final result is then transferred to the main memory
.Hence the data may move from main memory to the ALU multiple times before the
processing is over.
Output:
Output is the processing of giving the result of data processing to the outside world.
The results are given through output devices such as monitor, printer etc...
The output devices therefore convert the results available in binary codes into a human-
readable language before displaying it to the user.
Control:
The control unit is the central nervous system of the entire computer system. It manages and
controls all the components of the computer system.
CPU
Memory
unit/storage unit
ALU (arithmetic
and logical unit
An input device is used to fees data and instructions into the computer. In the absence of an input
device, a computer would have only been a display device.
Principles of Programming using C Module 1
Input devices
Track pad
Joystick
OCR
OMR
MICR
Keyboard:
The keyboard is the main input device for computers. Computer keyboards look very
similar to the keyboards of typewriters with some additional keys.
Most keyboards have between 80 and 110 keys.
The layout of a keyboard is known as QWERTY for its first six letters.
Types of keys:
Principles of Programming using C Module 1
Pointing devices:
Pointing input devices enable the users to easily control the movement of the pointer to
select items on a display screen to select commands menu, to draw graph, etc...
Some examples of pointing devices include mouse, track ball, light pen joystick and
touch pad
Mouse:
The mouse is an input device that was invented by Douglas Engelbart in 1963.
It is the key input device used in a graphical user interface
It can be used to handle the pointer easily on the screen to perform various functions such
as opening a program or file.
The mouse has two buttons and a scroll wheel. It can be held in the hand and easily
moved without lifting, along a hard flat surface to move the cursor to the desired
location-up, down, left, right.
Once the mouse is placed at appropriate postion,the user may perform the following
operation:
Principles of Programming using C Module 1
o Point: Placing the mouse pointer over the word or the object on the screen by
moving the mouse on the desk is termed as pointing.
o Click: pressing either the left or the right button of the mouse is known as
clicking.
o Drag: Pointing to a desired location while pressing the left button.
1. Mechanical mouse:
This type of mouse has a rubber or metal ball at its bottom and an electronic circuit
containing sensors.
When the mouse is moved over a flat surface, the sensor detects the direction of
movement of the ball.
The electronic circuit translates the movement into signals and feed it as input to the
computer.
2. Optical mouse:
3. Cordless mouse:
A cordless or wireless mouse is not connected to the computer. The movement of the
mouse is detected using radio waves or infrared light.
Principles of Programming using C Module 1
Track ball:
A trackball is a pointing device that is used to control the position of the cursor on the
screen. It is usually used in notebook computers, where it is placed on the keyboard.
Touchpad:
A touchpad (track pad) is a small, flat, rectangular stationary pointing device with a
sensitive surface of 1.5-2 square inches.
The user has to slide his or her fingertips across the surface of the pad to point to a
specific object on the screen.
The surface translates the motion and position of the users fingers to relative position on
the screen.
There are also button around the edge of the pad that work like mouse buttons.
Touch pad are widely used in laptops and are in built on the laptop keyboard.
Handheld device:
Joystick:
A joystick is a cursor control device widely used in computer games and computer aided design
(CAD)/computer aided manufacturing (CAM) applications.
Principles of Programming using C Module 1
Stylus:
A stylus is a pen-shaped input device used to enter information or write on the touch
screen of a handheld device.
It is a small stick that can also be used to draw lines on a surface as input into a device
choose an option from a menu, move the cursor to another location on the screen.
Touch screen:
A touch screen is a display screen that can identify the occurrence and position of a touch
inside the display region.
The user can touch the screen either by using a finger or a stylus.
The touch screen facilitates the users to interact with what is displayed on the screen in a
straightforward manner, rather than in an indirect way by using a mouse or a touchpad.
Principles of Programming using C Module 1
Optical devices:
Optical devices also known as data-scanning devices use light as a source of input for
detecting or recognizing different objects such as characters, marks, codes and images.
The optical device converts these objects into digital data and sends it to the computer for
further processing
Barcode reader:
A barcode reader is a handheld input device that ais used to capture and read information
stored in a barcode
It consists of a scanner a decoder and a cable used to connect the reader to a computer.
The function of the barcode reader is to capture and translate the barcode into numerical
and/or alphabets.
It is connected to computer for further processing of the captured information.
Principles of Programming using C Module 1
Image scanner:
A scanner is a device that captures images, printed text and handwriting from different sources
such as photographic prints, posters and magazines and converts them into digital images for
editing and displaying on computer.
Optical character recognition is the process of converting printed materials into text or word
processing files that can be easily edited and stored.
Optical mark recognition is the process of electrically extracting data from marked fields
such as checkboxes and fill in fields on printed forms.
The optical mark reader is fed with an OMR sheet that has pen or pencil marks is pre-
defined positions to indicate each selected response marks and space and stores the
interpreted data in a computer for storage analysis and reporting
Principles of Programming using C Module 1
Magnetic ink character reader is used tto verify the legitimacy of paper documents
especially bank checks.
It consists of magnetic ink printed characters that can be recognized by high speed
magnetic recognition devices.
Audio devices:
Principles of Programming using C Module 1
Video input devices are used to capture video from the outside world into the computer.
Digital camera and webcam are the popular examples of video input device.
A digital camera is a handheld and easily portable device used to capture images or
videos.
The digital cameras digitalize images or video and store them on a memory card.
Web cameras too capture videos that can be transferred via the internet in real-time.
Web cameras are widely used for video conferencing
Digital camera
Web came
Principles of Programming using C Module 1
Output devices:
Output devices
Monitor: The monitor is an integral part of computer which displays both text and
graphics. The performance is measured in terms of image quality, resolution, energy
consumption.
Printer: Printer is an output device that prints any data, report, document, picture, diagram
etc. Printers are classified into:
Principles of Programming using C Module 1
1. Dot-Matrix Printer: The print head of the dot-matrix printer has either 9 or 24
pins. When the pins fire against the ribbon, an impression is created on the paper.
The speed is 300 cps and doesn’t produce high quality output. It can produce
multiple copies.
3. Line Printer: For heavy printing, the line printer is used. It uses a print chain
containing the characters. Hammers strike the paper to print and it rotates at the
speed of 1200 lpm. It is also noisy and is of low-quality.
Non-Impact Printers: They are fast, quiet and of high resolution. The most
commonly used non- impact printers are laser and ink-jet printers. The thermal printer is
not discussed here because it uses heat to print on high-sensitive paper.
1. Laser Printer: It works like a photocopier and uses toner i.e. black magnetic
powder. The image is created in the form of dots and passed from drums on to the
paper. It has built-in RAM which acts as buffer and RAM to store fonts. The resolution
varies from 300 dpi to 1200 dpi and the speed is about 20 ppm.
2. Ink-jet Printer: These are affordable printers. It sprays tiny drops of ink at high
pressure as it moves along the paper. The separate cartridges are available for
different colors. The resolution is about 300 dpi, can print1 to 6 pages/min.
Principles of Programming using C Module 1
Printer:
Printers
Plotters: The plotter can make drawings. It uses one or more automated pens. The
commands are taken from special file called vector graphic files. Depending on type of
plotter either paper or pen moves. It can handle large paper sizes and it is used for
creative drawings like buildings and machines. They are slow and expensive.
Speakers: The speaker is an output device which produces the voice or sound waves.
Principles of Programming using C Module 1
Projector: The projector is the by-pass device for the monitor where it produces larger
images or pictures
Applications of computers:
Internet:
The internet is a network of networks that connects computers all over the world.
It gives users access to an enormous amount of information, much more than available in
any library.
Government:
Principles of Programming using C Module 1
Music:
All computers today have musical instrument digital interface facility, which links
musical instruments to a PC, thereby enabling the computers to generate a variety of
sounds.
Movies:
Computers are used to create sets, special effects, animations, cartoons, imaginary characters,
videos and commercials.
Computers are used to prepare tickets, monitor the trains or airplanes route and guide the plane
to a safe landing.
In business and industry, computers are used and mainly for data processing, which includes
tasks such as word processing, analyzing data entering records, pay roll processing, personnel
record keeping and inventory management.
Hospitals:
Hospitals use computers to records every information about patients from the time of their
admission till their exit.
Weather forecasting:
When computers are fed with mathematical equations along with data about air pressure,
temperature, humidity, and other values, the solution of these equations given an accurate
prediction of the weather of a particular area.
Online banking:
Introduction to C programming:
C is a powerful language that provides fast program execution. Its power and fast
program execution come from its ability to access low commands similar to assembly
language but with high level syntax.
History of C language:
Features of C language:
Simple robust:
C is a general purpose, structured programming language. It has the simplicity of a high level
language as well as the power of low level language. This aspect of c makes it suitable for
writing both application programs and system programs. Hence it is an excellent, efficient and
general purpose language for most of the application such as mathematical, scientific, business
and system software applications.
C languages consist of 32 English words known as keywords. The power c lies in a rich set of
built in library functions and variety of operation.
Modularity:
C programs are modular in nature it supports compartmentalization of code and data. This
capability makes it very easy for c-programs to share sections of code. We can divide c programs
to several modules that will combine in a single module to build the final program.
Extensibility:
The c language is extensible since it allows the users to add their own library function to the
library function .section of code or modules can be stored in libraries for re-use them in future.
Portability:
C program written on one computer or operating system can be compiled and run on another
computer or operating system with little or no modification.
The compilation and execution of c programs are faster than modern programming languages.
This is due to its variety of data type and powerful operators.
Bit manipulation:
It provides wide variety of bitwise operators to manage data at bit level. We can perform
different operations at bit level
Documentation Section: It consists of a set of comment line giving the name of the
program, theauthor and other details. Compiler ignores these comments. Comment is a
short description of the problem to be solved which is written at beginning of each
program. There are two formats:
Syntax: //
b) Block comments /*Multi line comment*/: It is used when comments as several line.
Link Section: It provides instruction to the compiler to link functions from system
library.
#include<stdio.h>
#include<conio.h>
#include<math.h>:
Every ‘C’ program has one main ( ) function and it contains two parts:
Sub-program Section: It contains all the user defined functions that are called by main( )
function.
Documentation section
Link section
Definition section
Global declaration section
main( ) Section
{
Declaration part
Executable part
}
Sub program section
Function 1
Function 2
-
-
Function n
Principles of Programming using C Module 1
Example:
The c program is written using vi editor or text editor .Most language compilers have their
built-in editors. The program or source code should be written in the syntax provided by that
language. In c a source code file has extensions.
Linker assembles the entire libraries which are necessary for the program but existing
somewhere else. The system uses object code as input and translates it to executable code.
4. Executing programs(file.exe) :
Principles of Programming using C Module 1
Executable file is ready to run file. Loader loads the executable file into the main/primary
memory and run program.
program using
Compilers:
A compiler is a program that translate the instruction of high level language (source code)
into machine language (object language)
A compiler is a program or set of programs that converts source code written in a high
level language to low level language (assembly language or machine language)
A programming language can have many compilers. For example, GCC,turbo C,Quick C
etc.. are different compilers for C programming language.
Need of computer:
A computer understands only binary language and executes instructions coded in binary
language. It cannot execute a single instruction given in any other form. Therefore we
must provide instructions to the computer in binary language.
Means we must write computer programs entirely in binary language and it is not
possible to write the program in binary language.’
So there was a need of a translator that translates the compiler instructions given in
English language to binary language.
Compiler interpreter
Compiler takes entire program as input Interpreter takes single instructions as input
Compiler generates object code Interpreter is not generates object code.
No source code is required in executable file Source code is required in executable file
Programming language like C,C++,C# uses Programming language like python ,ruby uses
compilers interpreters.
Compiler translates entire program at a time Interpreter translates program line by line
Principles of Programming using C Module 1
Letter: A-Z,a-z
Digits: All decimal digit 0-9.
Special characters: . ; : , $ # ^&-+=
Whitespace: enter space, horizontal tab.
C tokens:
Tokens: token are the smallest unit of a program which are made up of one or more characters.
1. keywords:
Keywords are meant for some special purpose called as reserved words.
User cannot change the meaning of keywords.
Example:
Identifiers:
Identifiers are the names given to the elements of the program such as variable name ,
name or a function name.
An identifier is a word consisting of sequence of one or more letters or digits or a special
symbol called as _(underscore)
Rules:
Constants:
Constants are the data values that cannot be change during the execution of a program.
Operators:
Special symbol:
Special symbols have some special meaning in the c programming language for the compiler and
it is used to perform some special function\task in c.
Data types:
Data types: Data type defines the types of data that is stored in a variable.
There are 3 types:
i. Primary/ Built-in/ Fundamental data type int , float, double, char, void
ii. Derived data type array, structure
iii. User defines data type enum , type def
Primary/ Built-in/ Fundamental data type: These are the built in data types
available in C. There are5 types. Namely:
1. Integer (int)
2. Floating point (float)
Principles of Programming using C Module 1
3. Character (char)
4. Double precision floating point (double)
5. Void (void)
1. Integer type:
It is used to store whole numbers.
The keyword int is used to declare variable of integer type.
Int type takes 2B or 4B of storage depending on machine size.
The classes of integer are:
Unsigne Signe
d d
Data type Keyword Size Data type Keyword Size
Short Integer short int 1B Signed short integer signed short int 1B
Integer int 2B Signed integer signed int 2B
Long Integer long int 4B Long integer long int 4B
3. Double:
It is used to store double precision floating point numbers.
The keyword double is used to declare variable of floating point data type.
Double type takes 8B of storage.
Double precision is related to accuracy of data.
Principles of Programming using C Module 1
4. Char:
It is used to store character type of data.
The keyword char is used to declare variable of character type.
Char type takes 1B of storage.
5. Void:
It is a special data type that has no value.
It doesn’t have size.
It is used for returning the result of function that returns nothing.
Variable A variable is a name given to memory location whose value changes during
execution.
Types of variables:
1. Numeric variable: numeric variable can be used to store either integer values or floating
point values.
2. Character variable:
Character variable can include any letter from the alphabet or from the ASCII chart and
number from 0-9 that are given within single quotes.
Declaration:
Where,
datatype Any built in data type
variable_list Identifiers that specifies variable name.
Example: int a;
Where,
int is the built in data type
a is variable of type int
1. Name should only consist of alphabets (both upper and lower case ),digits and underscore
sign.
2. First characters should be alphabet or underscore.
3. Name should not be a keyword.
4. Since C is a case sensitive the upper case and lower case considered differently for
example code ,Code<CODE etc..Are different identifiers.
5. As C defined up to 32 significant characters can be used and will be considered
significant by most compilers. If more than 32 are used they will be ignored by the
compiler.
6. No white space is used.
Initialization:
Syntax: datatype variable_name = value;
Where,
datatype Any built in data type
variable_name Identifier that specifies variable name.
value The data which will be stored in variable name.
Example: int a = 5;
Where,
int data type
a variable name
5 value
Identifiers Variables
All identifiers are not variables All variables are identifiers
Identifiers may not have any All variables have memory.
memory unless it is a variable
Mentioning the type of an Type of the variable must be
identifier it not needed unless it is defined.
a variable
Constants:
Constants or a literal refers to fixed values that do not change during the execution of a
program.
Principles of Programming using C Module 1
Constants are treated just like regular variables except that their values cannot be
modified after their definition.
C supports several types of constants as shown
Numeric constants:
Numeric constants consist of numeric digits they may or may not have decimal point.
The rules for defining numeric constants are as follows:
1. Numeric constant should have at least one digit.
2. No comma or space is allowed within the numeric constant.
3. Numeric constant can either be positive or negative but default sign is always positive.
4. The value of a constant cannot exceed specified minimum and maximum bounds.
There are two types of numeric constants namely, integer constant floating point constant.
1. Integer constants:
Integer constants are whole numbers without any fractional part or decimal point. It must have at
least one digit and may contain either + or – sign. A number with no sign is assumed to be
positive.
There are 3 types of integer constants:
1. Decimal integer constants (base 10):
The interpretation of a floating point constant with an exponent is essentially the same as
scientific notation, except that the base 10 is replaced by the letter E or e.
Principles of Programming using C Module 1
2. Character constants:
A character constant contains one single character enclosed within single quotes (‘’)
Example:
‘a’, ’Z’,’\n’
3. String constants:
String constant are sequence of character enclosed within double quotes (‘””).
Example:
“hello”,”1234”,”abc”,”\n”
Declaring constants:
1. To declare a constant precede the normal variable declaration with const keyword and
assign it a value.
Syntax: const data type constant value
Example: const float Pi=3.142
2. Another way is to use the preprocessor command define.
Syntax: #define const value
Example:
#define PI 3.142
Constants
Files used in C
Source file:
The source code file contains the source code of the program.
The file extension of any C source code file is ‘.c’. This file contains C source code that
define the main function and may be other functions. The main () function is the starting
point of execution when you successfully compile and run the program.
Header file:
When working with large projects, it is often desirable to separate out certain sub routines
from the main () of the program.
There also may be a case that the same subroutine has to be used in different programs. In
the latter case one option is to copy the code of the desired subroutine from one program
to another. However copying the code is often tedious as well as error-prone and makes
maintainability more difficult.
Example:
Object file:
Object files are generated by the compiler as a result of processing the source code file. They
contain compact binary code of the function definition. Linker uses this object file to produce an
executable file (.exe file) by combining the object files together. Object file have a ‘.o’
extension.
We use the scanf() function for getting the formatted inputs or standard inputs so that the printf ()
function can provide the program with numerous options of conversion.
Principles of Programming using C Module 1
We use this function for displaying a single or multiple values in the form of output for the user
end at the console.
Syntax:
Example:
#include <stdio.h>
int main()
{
int i;
float f;
char c;
printf("You entered:\n");
printf("i = %d, f = %f, c = %c\n", i, f, c);