Notes CCP U1 N
Notes CCP U1 N
Unit#01
Introduction to Programming Environment:
o Prog. Envt. is the envt, in which programs are created & tested.
o It is much closer to O.S. than the design of a programming language.
o Prog. Envt. consists of a set of support tools & a command language (instruction Set) for
invoking the tools.
o Each support tool is another program that may be used by programmer as an aid during
>1 stages of program-creation.
Tools example : - Editors, debuggers, verifiers, compilers, test data generators & pretty printers etc.
Software Hardware
(instruction, data, Application) (electronic components, electromech. Devices)
Digital Computer:
A computer is an electronic machine like any other machine which takes inputs (raw data) and performs
some processing giving out processed data (information).
A digital computer is a digital system that performs various computational tasks. The word
digital implies the info in computer is shown by variables that take a limited no. of discrete values.
Then these values are processed internally by components and discrete states are maintained. In this case
the discrete elements are digits. So from this place we got the term- “Digital Computer.”
These machines (m/c) cooperate efficiently if only ‘2’ states are used. Hence- Binary values are
being popular now.
Bit: Digital computers use the binary no. system, which has two digits : 0 & 1.
A binary digit ≡ a ‘bit’.
So now info is represented in digital computers in group of bits.
By this group of bits we can not only represent binary nos. but also other discrete symbols (decimal
digits/letter/alphabet) can be shown with the help of many coding techniques. Furthermore development
of symbols & sets of Instructions (program) are also possible for doing many computations with the
proper arrangement of bits. (Coding methods)
Binary numbers use a base 2 system with 2 digits 0 & 1. Decimal equivalent of Binary no. can be
found by expanding Binary no. into a power series with a base of 2. For example 101 as: 1 x 22 + 0 x
21 + 1 x 20 = 4 + 0 + 1 = 5
Block Diagram Of Digital computer
2. Storage Unit: The process of saving data and instructions permanently is task of storage unit. 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 first 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.
3. Processing Unit: 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. It(result) is then sent back to the storage unit.
4. Output Unit: Its task 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 or display.
5. Control unit: 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 inside the computer.
FUNCTIONAL UNITS
In order to carry out the operations mentioned in the previous section the computer allocates the task
between its various functional units. The computer system is divided into three separate units for its
operation. They are
Arithmetical Logical Unit (ALU):After you enter data through the input device it is stored in
the primary storage unit. The actual processing of the data and instruction are performed by
Arithmetic Logical Unit. The major operations performed by the ALU are addition, subtraction,
multiplication, division, logic and comparison. Data is transferred to ALU from storage unit
when required. After processing the output is returned back to storage unit for further processing
or getting stored.
Control Unit (CU)
The next component of computer is the Control Unit, which acts like the supervisor seeing that
things are done in proper fashion.
Control Unit is responsible for coordinating various operations using time signal.
The control unit determines the sequence in which computer programs and instructions are
executed. Things like processing of programs stored in the main memory, interpretation of the
instructions and issuing of signals for other units of the computer to execute them.
It also acts as a switch board operator when several users access the computer simultaneously.
Thereby it coordinates the activities of computer’s peripheral equipment as they perform
the input and output.
Central Processing Unit (CPU) The ALU and the CU of a computer system are jointly known as the
central processing unit. You may call CPU as the brain of any computer system.
Anurag Tewari, CS, KIET 2
Computer H/w : It can be divided into 3 major parts
1. CPU
2. Memory (RAM)
3. I/O Processor
CPU has
o Arithmetical & logical unit for data manipulation.
o No. of registers for data-storage
o Control circuits to fetch & execute Instructions.
Memory It contains storage for instructions & data. It is the primary memory, called as Random
Access memory (RAM).
CPU can access RAM at random & retrieve the binary information within a fixed interval of
time.
I/O Processor: It has electronic ckts. for communicating & controlling the transfer of
information b/w the computer & outside world.
I/p devices – Key board, mouse.
O/p devices – Monitor, printer.
Basic Hardware Components
Types of Software
Program: A sequence of instructions for the computer is called a program. The data that are
manipulated by the program create the database.
1. System software-It is the software used to manage and control the hardware components and
which allow interaction between the hardware and the other types of software. The most obvious
type of system software is operating system and device drivers.
Utility software-It is software such as anti-virus software, firewalls, disk defragmenters and so
on.
which helps to maintain and protect the computer system but does not directly interface with the
hardware.
Memory Hierarchy
Registers are embedded inside the CPU. They have very less capacity but
fastest speed. CPU uses some specific registers like PC (program counter), IR
Fastest Register (instruction register), MAR (memory address register), MDR (memory data
register), Ax(Accumulator register) and some GPR (general purpose
registers).
Cache memory is memory actually embedded inside the CPU. Cache memory
is very fast, typically taking only once cycle to access, but since it is
Faster Cache embedded directly into the CPU there is a limit to how big it can be. In fact,
there are several sub-levels of cache memory (termed L1, L2, L3) all with
slightly increasing speeds.
All instructions and storage addresses for the processor must come from
Slower RAM. Although RAM is very fast, there is still some significant time taken
than RAM for the CPU to access it (this time is latency). RAM is stored in separate,
Cache dedicated chips attached to the motherboard, meaning it is much larger than
cache memory.
We are all familiar with software arriving on a floppy disk or CDROM, and
saving our files to the hard disk. We are also familiar with the long time a
Slowest Disk program can take to load from the hard disk -- having physical mechanisms
such as spinning disks and moving heads means disks are the slowest form of
storage. But they are also by far the largest form of storage.
An operating system, or "OS," is a system software that communicates with the hardware and allows
other programs to run. It has the fundamental files your computer needs to boot up and function. Every
desktop computer, tablet, and smart phone includes an operating system that provides basic functionality
for the device.
e.g MS DOS, Win 95 ,win 2000,win xp, win 8, Unix, Linux, Android etc.
Important functions of an operating system:
Memory Management
Process Management
Device Management
File Management
Security Mgt
Control over system performance
Job accounting
Error detecting aids
Coordination between other software and users
1. Batch OS:The users of batch operating system do not interact with the computer directly. Each
user prepares his job on an off-line device like punch cards and submits it to the computer
operator. Main problems are - Lack of interaction between the user and job, CPU is often idle.
2. Real-time OS: A real-time operating system is a multitasking operating system that aims at
executing real-time applications. Real-time operating systems often use specialized scheduling
algorithms so that we can perform multiple tasks simultaneously. The main objective of real-time
operating systems is their quick and predictable response to events. They have an event-driven or
time-sharing design. Ex- VxWorks, MARS, RTLinux
3. Multi-user OS: A multi-user operating system allows multiple users to access a computer
system at the same time. Time-sharing systems and Internet servers can be classified as multi-
user systems. Single-user operating systems have only one user but may allow multiple programs
to run at the same time.Ex-UNIX,Main-Frame,Supercomputer.
4. Distributed OS: A distributed operating system manages a group of independent computers and
makes them appear to be a single computer. The development of networked computers that could
be linked and communicate with each other gave rise to distributed computing. Distributed
computations are carried out on more than one machine. When computers in a group work in
cooperation, they make a distributed system.Ex-NFS-Andrew,Amoeba
A Brief History of C:
C is a general-purpose language which has been closely associated with the UNIX operating
system for which it was developed - since the system and most of the programs that run it are
written in C.
Many of the important ideas of C originated from the language BCPL, developed by Martin
Richards. The influence of BCPL on C proceeded indirectly through the language B, which was
Programming Langs
C Program: Generally three basic phases occurs when we execute any C prog:
• Preprocessing
• Assembling & Compiling
Assembler: A computer will not understand any program written in a language, other than its machine
language. The programs written in other languages must be translated into the machine language. Such
translation is performed with the help of software. A program which translates an assembly language
program into a machine language program is called an assembler. If an assembler which runs on a
computer and produces the machine codes for the same computer then it is called self assembler or
resident assembler. If an assembler that runs on a computer and produces the machine codes for other
computer then it is called Cross Assembler.
Assemblers are further divided into two types: One Pass Assembler and Two Pass
Assembler. One pass assembler is the assembler which assigns the memory addresses to the variables
and translates the source code into machine code in the first pass simultaneously. A Two Pass
Assembler is the assembler which reads the source code twice. In the first pass, it reads all the variables
Interpreter Compiler
Scans the entire program and translates it as a whole
Translates program one statement at a time.
into machine code.
It takes large amount of time to analyze the source
It takes less amount of time to analyze the source
code but the overall execution time is comparatively
code but the overall execution time is slower.
faster.
No intermediate object code is generated, hence Generates intermediate object code which further
are memory efficient. requires linking, hence requires more memory.
Continues translating the program until the first It generates the error message only after scanning
error is met, in which case it stops. Hence the whole program. Hence debugging is
debugging is easy. comparatively hard.
Ex-Programming language like Python, Ruby use Ex-Programming language like C, C++ use
interpreters. compilers.
Syntax1:
printf("user defined message");
O/P:user defined message
Syntax2:
printf("Format specifers",value1,value2,..);
O/P:values of variables as per specifiers type
So In the statement printf(“area=%d\n”,area), the format string is “area=%d\n” & variable is area.
%d interprets the variable area occurring after the comma in the printf statement as an integer
Anurag Tewari, CS, KIET 14
& displays its value.
\nnew line advancement.
Input Statement:
To make the program generalized we have to add user interaction in it during program execution.
It means user can give different i/ps & observe the o/p accordingly.
Input unit is keyboard & o/p unit is monitor.
The input statement in C uses-scanf() as its library function.
The scanf function is the input equivalent of printf
It is a C library function in the <stdio.h> library
Takes a format string and parameters, much like printf
The format string specifiers are nearly the same as those used in printf
Examples:
scanf ("%d", &x); /* reads a decimal integer */
scanf ("%f", &rate); /* reads a floating point value */
The ampersand (&) is used to get the “address” of the variable
All the C function parameters are “passed by value”.
If we used scanf("%d",x) instead, the value of x is passed. As a result, scanf will not know where
to put the number it reads.
It returns integer value calculated by counting no. of characters entered thru keyboard on console.
Syntax:
scanf("format specifiers",&value1,&value2,. ...);
Example of scanf function list of address of variables
int a;
float b;
scanf("%d%f",&a,&b);
In the above syntax format specifier is a special character in the C language used to specify the
data type of value.so now program will be as follows
*Returned values of printf() and scanf()
In C, printf() returns the number of characters successfully written on the output and scanf() returns number of
items successfully read.
For example, below program prints jhello world 12
main()
{
printf("%d",printf("jhello world"));
}
Irrespective of the string user enters, below program prints 1.
main()
{
char a[50];
Anurag Tewari, CS, KIET 15
printf(" %d", scanf("%s", a));
}
Modulus operator (%) :
It returns the remainder as a result after division operation
4 %3 = 1, -5 % 2 = -1
7% 3 = 1, 5 % -2 = 1, 2 % 3 = 2
Hierarchy of operators:
1st priority Parenthesized value (value within bracket or paranthesis): a*(b/(c-d))
2nd priority *, /, %.
3rd priority +, -
4th priority =
Direction of solving is from left to right in an expression.
i = 2 * -3 / 4 + 4/4 + 8 -2 + 5%8
Result
6/4 + 4/4 +8 - 2 + 5/8
1 + 4/4 + 8 - 2 + 5/8
1 + 1 + 8 -2 + 5/8
10 – 2 + 0
8+08
/ *… .............. */ comment
int A = 10
float b =2.2
char c = ‘A’
C Tokens
Token Example :
C KEYWORDS
C keywords are the words that convey a special meaning to the c compiler. The keywords cannot be
used as variable names because by doing so, we are trying to assign a new meaning to the keyword
which is not allowed. There are total 32 keywords defined in C
Volatile While
Identifiers are used as the general terminology for the names of variables, functions and arrays.
These are user defined names consisting of arbitrarily long sequence of letters and digits with
either a letter or the underscore ( _ ) as a first character.
Structured programming
Structured programming, sometimes known as modular programming.
It is a subset of procedural programming that enforces a logical structure on the program being written
to make it more efficient and easier to understand and modify.
It is A technique for organizing and coding computer programs in which a hierarchy of modules is
used, each having a single entry and a single exit point.
In Struct.Prog., control is passed downward through the structure without unconditional branches to
higher levels of the structure.
Three types of control flows are used: sequential, conditional (test), and iterational (repeating).
Structured programming frequently employs a top-down design model, in which developers map out
the overall program structure into separate subsections.
A defined function or set of similar functions is coded in a separate module or sub module, which
means that the code can be loaded in to memory more efficiently and that modules can be reused
in other programs.
After a module has been tested individually, it is then integrated with other modules into the overall
program structure.
Top down approach and Stepwise refinement
With the top-down approach, we start with our top-level program, then divide and further sub-divide it
into many different modules. The division process is known as stepwise refinement.
As we design each module, we will discover what kind of sub modules we will need, and then continue
to program those.
After programming our smaller sub modules, we will group them together into the larger module.
temper ature
to
in degree Celsius degree Fahrenheit.
Input: Temp. in degree Celsius. Output: Temp. in degree Fahrenheit.
1. Start
2. Obtain value of temp. in °C.
3. Apply mathematical formula of conversion(Multiply this value by 1.8 & add 32)
4. Assign value obtained of temp. in °F.
5. Display result.
6. Stop.
to
Flowchart: to convert temperature in degree Celsius degree Fahrenheit.
Start
Read(i/p)
C
F = C x 1.8
F = F + 32
(Display)Write temp
in F
Stop
Example 2: Design Algo. for choosing the largest of 3 given positive integers.
Input : Three integers :– A, B, C. o/p : Largest no. among A, B, C.