PSPC Notes - Unit - 1
PSPC Notes - Unit - 1
UNIT -1
Input: this is the process of entering data and programs into the computer system.
Control Unit (CU): The process of input, output, processing and storage is performed
under the supervision of a unit called 'Control Unit'. It decides when to start receiving
data, when to stop it, where to store data, etc. It takes care of step -by-step
processing of all operations inside the computer.
Memory Unit: Computer is used to store data and instructions either temporarily or
permanently.
Arithmetic Logic Unit (ALU): The major operations performed by the ALU are
addition, subtraction, multiplication, division, logic and comparison.
Output: This is the process of producing results from the data for getting useful
information.
The ALU and the CU of a computer system are jointly known as the central
processing unit (CPU). You may call CPU as the brain of any computer system.
1.3 IO Devices
1.4 SOFTWARE:
As you are aware, computer cannot do anything on its own. It is the user who
instructs computer; what to do, how to do and when to do. In order to perform any
task, you have to give a set of instructions in a particular sequence to the computer.
These sets of instructions are called Programs. Software refers to a set of programs
that makes the hardware perform a particular set of tasks in particular order.
Software can be classified mainly into following categories and sub-categories are
shown in Figure:
System Software:
When you switch on the computer the programs stored in ROM are executed which
activates different units of your computer and makes it ready for you to work on it.
This set of programs can be called system software.
System software‟s are sets of programs, responsible for running the computer,
controlling various operations of computer systems and management of computer
resources. Operating System (OS) falls under this category.
An operating system is system software that provides an interface for a user to
communicate with the computer, manages hardware devices (disk drives, keyboard,
monitor, etc), manages and maintains disk file systems and supports application
programs. Some popular Operating systems are UNIX, Windows and Linux.
Although operating system provides all the features users need to use and
maintain their systems, inevitably, they still do not meet everyone‟s expectations.
Dept of CSE/IT, VVIT Nambur
Problem Solving & Programming using C
This has led to another type of system software called "Utilities". These are programs
that bridge the gap between the functionality of an OS and the needs of users. Utility
programs are a broad category of software such as compress (zip)/uncompress
(unzip) files software, antivirus software, split and join files software, etc.
Application Software
Languages are a means of communication. Normally people interact with each other
through a language. On the same pattern, communication with computers is carried
out through a language. This language is understood both by user and the machine.
Just as every language like English, Hindi has its grammatical rules; every computer
language is bound by rules known as SYNTAX of that language. The user is bound by
that syntax while communicating with the computer system.
1. Low Level Language: The term low level means closeness to the way in which
machine understand. The low level languages are:
a. Machine Language: This is the language (in the form of 0‟s and 1‟s, called
binary numbers) understood directly by the computer. It is machine
dependent. It is difficult to learn and even more difficult to write programs.
b. Assembly Language: This is the language where the machine codes
comprising of 0‟s and 1‟s are substituted by symbolic codes (called mnemonics)
to improve their understanding. It is the first step to improve programming
structure.
Assembly language programming is simpler and less time consuming than
machine level programming, it is easier to locate and correct errors in assembly
language than in machine language programs. It is also machine dependent.
Programmers must have knowledge of the machine on which the program will
run.
BASIC (Beginners All Purpose Symbolic Instruction Code): It is widely used, easy to
learn general purpose language. BASIC is mainly used in microcomputers in earlier
days.
C++: Popular object oriented programming language, used for general purpose.
Python: It is an interpreted language mostly used for Data science, machine learning
applications.
As you know that High Level language is machine independent and assembly
language though it is machine dependent yet mnemonics that are being used to
represent instructions are not directly understandable by machine. Hence to make
the machine understand the instructions provided by both the languages, Compiler
and Assembler are required to convert these instructions into machine language.
Definition of a Compiler: a translator (set of programs) that converts a program
written in high level language into an equivalent program in machine language is
called as Compiler. A compiler converts all instructions at a time.
An interpreter is also like a compiler. It also converts a high level language into
machine language. The difference is: a compiler converts all instructions at time
where as an interpreter performs line by line translation.
The program written by the programmer in high level language is called source
program and the program generated by the compiler after translation is called as
object program.
A translator (or software which is a set of programs) that reads a program written in
assembly language and translates it into an equivalent program in machine language
is called as Assembler.
Memory: Memory is a device where user data can be stored either temporarily or
permanently for further reference. It is a collection of bytes addressed in a sequence.
Data can be stored in form of bits (0/1). The word BIT is extracted from Binary Digit.
1. Primary Memory
a. RAM (Random Access Memory)
1. Static RAM
2. Dynamic RAM
b. ROM(Read Only memory)
1. PROM (Programmable ROM)
2. EPROM(Erasable PROM)
Secondary Memory: This type of memory is also known as external memory or non-volatile.
It is slower than the main memory. These are used for storing data/information permanently.
CPU directly does not access these memories; instead they are accessed via input-output
routines. The contents of secondary memories are first transferred to the main memory, and
then the CPU can access it. For example, disk, CD-ROM, DVD, etc.
Cache Memory: Cache memory is a very high speed semiconductor memory which
can speed up the CPU. It acts as a buffer between the CPU and the main memory. It
is used to hold those parts of data and program which are most frequently used by
the CPU. The parts of data and programs are transferred from the disk to cache
memory by the operating system, from where the CPU can access them.
The software Development Lifecycle is a process of building good software and its
lifecycle stages provides Quality and correctness of good software. All the stages of
Lifecycle are important in itself. One wrong step in Lifecycle can create a big mistake
in the development of software. Being an owner or stakeholder of a software company
you must know the development lifecycle of the software.
There are different types of Software development Life Cycle models. They are
waterfall, Agile, Spiral etc. This entire SDLC model must follow these 6 steps for
developing errorless software. Software Development Life cycle has totaled 6 steps.
A design approach clearly defines all the architectural modules of the product along
with its communication and data flow representation with the external and third
party modules (if any). The internal design of all the modules of the proposed
architecture should be clearly defined with the minutest of the details in DDS.
Stage 4: Building or Developing the Product
In this stage of SDLC the actual development starts and the product is built. The
programming code is generated as per DDS during this stage. If the design is
performed in a detailed and organized manner, code generation can be accomplished
without much hassle.
Developers must follow the coding guidelines defined by their organization and
programming tools like compilers, interpreters, debuggers, etc. are used to generate
the code. Different high level programming languages such as C, C++, Pascal, Java
and PHP are used for coding. The programming language is chosen with respect to
the type of software being developed.
Stage 5: Testing the Product
This stage is usually a subset of all the stages as in the modern SDLC models, the
testing activities are mostly involved in all the stages of SDLC. However, this stage
refers to the testing only stage of the product where product defects are reported,
tracked, fixed and retested, until the product reaches the quality standards defined
in the SRS.
Stage 6: Deployment in the Market and Maintenance
Once the product is tested and ready to be deployed it is released formally in the
appropriate market. Sometimes product deployment happens in stages as per the
business strategy of that organization. The product may first be released in a limited
segment and tested in the real business environment (UAT- User acceptance
testing). Then based on the feedback, the product may be released as it is or with
suggested enhancements in the targeting market segment. After the product is
released in the market, its maintenance is done for the existing customer base.
1.9 Algorithm
ii) Definiteness: Each step of the algorithm is precisely defined, i.e., the actions to
be carried out should be specified unambiguously.
iii) Effectiveness: All the operations used in the algorithm are basic (division,
multiplication, comparison, etc.) and can be performed exactly in
a fixed duration of time.
iv) Input: An algorithm has certain precise inputs, i.e. quantities, which are
specified to it initially, before the execution of the algorithm begins.
v) Output: An algorithm has one or more outputs, that is, the results of
operations which have a specified relation to the inputs.
The algorithm terminates after 4 steps. This explains the features of finiteness. Action
of each step is precisely defined. Input of our algorithm is two integers and output is
the „sum‟.
1.10 FLOWCHART
Flowchart Symbols
Flowcharts are usually drawn using some standard symbols; however, some special
symbols can also be developed when required. Some standard symbols, which are
frequently required for flowcharting many computer programs are shown.
Terminator: An oval flow chart shape indicates the start or end of the process,
usually containing the word “Start” or “End”.
Decision: A diamond flow chart shape indicates a branch in the process flow. This
symbol is used when a decision needs to be made, commonly a Yes/No question or
True/False test.
Connector: A small, labeled, circular flow chart shape used to indicate a jump in the
process flow. Connectors are generally used in complex or multi-sheet diagrams.
Direction: used to show the flow of control in a process. An arrow coming from one
symbol and ending at another symbol represents that control passes to the symbol
the arrow points to.
The flowchart should be neat, clear and easy to follow. There should not be any
room for ambiguity in understanding the flowchart.
The flowchart is to be read left to right or top to bottom.
A process symbol can have only one flow line coming out of it.
Ex: Flow Chart for Adding 3 numbers & Flow chart to find largest of two
numbers
Introduction to C
C is the mid level language invented by Dennis Ritchie in early 1970s at AT&T Bell labs. Initially there was a
language called BCPL(Basic Combined Programming Language), from which C was extracted. Further
BCPL was renamed as lanauge B with updations. Further some more modifications have done the language
& a complete version of C was released in 1976.
C is a case sensitive language & every statement is to be written in lower case only. Moreover it is a
function dependent language. However, it is most flexible & portable language. It is also known to be a
structured programming language as it provides support for complex control structures & modular
programming concepts.
Features of C:
1. Simple to learn
2. Flexible
3. Machine language programs may be developed
4. Portable
5. Compact
6. User-friendliness
7. Case sensitive
8. Rich set of operators
9. Function dependent
10. Structure programming
Structure of a C program
Similar to many other languages, C language also possess certain structure. The following is the structure.
Documentation Section
Link Section
Function prototype declaration section
Global variable declaration section
void main()
{
Local variable declaration section;
------ // body of main()
------
other function calls
}
function definition-1.
function definition –2.
…………….
…………….
Dept of CSE/IT, VVIT Nambur
Problem Solving & Programming using C
Documentation section: It includes the comments. A comment is a statement, which is ignored by the
compiler at the time of compilation. Usually a statement can be made as a comment when it is enclosed in
between /* and */. These delimiters are used for single line commenting & multi line commenting.
However this section is not mandatory.
Link Section: Link section includes preprocessor directive statements like #include, # define, #if, #ifndef.
These statements are used to provide directions to the complier. The statements in the link section do not
get complied rather they intimate directly to the compiler that include the specified header files like stdio.h
& conio.h in the program before the actual processing gets started. This section is the most vital section in
every C program
Function Prototype declaration Section: This section also intimates the compiler that the user is going to
define the specified functions in the same program itself. The declaration includes return type, function
name & arguments type and order.
Void main(): It is the function that consists of the actual program body. It provides the necessary
code to solve a particular problem. It consists of variable declarations, input/output statements,
expressions and other function calls. The entire C program is based on main() itself. All the
statements of the program must be within { and }. This section is needed for every C program.
Local variable declaration section: This section is also vital for every C program. It allows the user to
declare & define certain data items required for the program.
Sample C program:
For any C program, we require a translator called compiler. Especially for C, there are several C
compilers in which some of the compilers follow:
Turbo C compiler
ANSI C compiler
Microsoft C compiler
Borland C compiler
Quick C compiler
GCC Compiler
Among all the above compilers, ANSI & GCC C compiler is the user-friendliest. Hence many
programmers use GCC/ANSI C only.
C character set:
Alphabets: a to z and A to Z.
Digits: 0 to 9
Special symbols: #,+,-,*,/,&,%,!,?,:,; >,<,= etc.,
C Tokens
Keywords
Identifiers or Data names
Literals
Variables
Constants
Data types
Operators
Format specifiers
Escape Sequences
Keywords: A keyword is a predefined or predetermined word defined by the compiler. C provides a rich
set of keywords. Totally 32 keywords are there in C. The following is the list of keywords:
Constants: A constant is an identifier whose value never changes during the program execution. In other
words, a constant is an identifier whose value remains unchanged during the program execution. In order to
define a constant, use „const‟ keyword.
Variable: A variable is an identifier whose value may or may not be changed during the program execution.
Data Types: Data type tells about the type of data being assigned to an identifier. C supports
several data types. The basic category of data types follows:
Primary Data types
Secondary Data types
Primary Data types: Primary data types are the basic data types that are regularly used in programs. The
following table illustrates the same.
Note: signed & unsigned are known as sign qualifiers. Unsigned means „no sign‟ i.e., variable doesn‟t hold
negative values.
Secondary Data types: Secondary data types are used occasionally depends on the program type. The
following is the list of secondary data types:
o Arrays
o Functions
o Structures & unions
o Pointers
o enum
o typedef
Data Name or Identifier: A data name is nothing but a reference to a data item or an identifier is used
to identify a value. For example
int stno = 101;
char str[20] = “ Krishna”;
Operators: An operator is a symbol that performs an operation over one or more operands. C supports a
rich set of operators. The following is the category of operators:
1. Unary operator
2. Binary operator
3. Ternary operator
Unary operators perform operation on single operand. Ex: ++, --, !, ~ (tilde) and unary -
Binary operators perform operation on two operands. Ex: +, -, *, /, %, <, >, ==, != etc.,
Ternary operator performs operation on three operands. Ex: ?:
Arithmetic operators: Arithmetic operators perform all arithmetic operations like addition, subtraction,
multiplication & division. The operators are +, -, *, / and %.
Assignment operators: Assignment operators are used in expressions. The operators are =, +=, -=, *=,
/= and %=.
Ex: A=5;
A += 4; // A = A+4
A -=3; // A = A-3
A *= 5; // A = A*5
A /= 2; // A = A/2
A %= 3; // A = A%3
Conditional operator: It is also known as ternary operator. It is used as an alternative to if..else control
structure/statement. The symbol for the operator is „ ?: ‟
Ex: c = (a>b)? a: b;
Relational Operators: Relational operators are used to perform evaluations on expressions. Regularly used
relational operators are less than (<), greater than (>), less than or equal to (<=) and greater than or equal to
(>=).
Equality Operators: Equality operators are used to check whether two different entities are equal or not.
Usually numeric data & single character data may be evaluated using equality operators. The operators are
equal to (==) and not equal to (! =).
Logical Operators: Logical operators are to evaluate more than one condition or expression. Regularly
used logical operators are: and (&&), or (||), not (!).
Bit-wise operators: Bit-wise operators perform low-level operation on elementary data items. The
operators are: left shift (<<), right shift (>>), bit-wise &, bit-wise |, complement (~), ex-OR (^).
Pointer Operators: pointer operators are & (address of operator) and * (indirection operator). They are
used to handle memory addresses and the values at that addresses.
Sizeof operator: In order to obtain the no. of bytes occupied by an object (data item/ data type/ identifier)
in a program, use „sizeof‟ operator.
Ex: printf(“%d”, sizeof(int));
O/p: 2
Note: The result of relational, equality & logical operators will always be TRUE/FALSE i.e., 1/0.
Programming Style in C:
A programming style is a set of guidelines used to format programming instructions. It is useful to follow
a style as it makes it easier for programmers to understand the code, maintain it, and assists in reducing the
likelihood of introducing errors. C follows the given rules to maintain a better programming style:
1. Valid identifier can have letters (both uppercase and lowercase letters), digits and underscores.
2. The first letter of an identifier should be either a letter or an underscore.
3. You cannot use keywords as identifiers.
4. There is no rule on how long an identifier can be. However, you may run into problems in some
compilers if the identifier is longer than 31 characters.
The rule of priority of operators is called operator precedence. The following table demonstrates the
operator priorities in C. it determines which operator is performed first in an expression with more than one
operators with different precedence. The operators not only have priority but also associativity too.
For example-
10 + 20 * 30 is calculated as 10 + (20 * 30) and not as (10 + 20) * 30
Operators Associativity is used when two operators of same precedence appear in an expression.
Associativity can be either Left to Right or Right to Left. The table is given below:
Escape Sequences
In order to customize out put of a program C provides a special character set. Such special characters are
known to be escape sequences.
Format specifiers
A format specifier tells about the type of the data to be viewed. It is also called as format code or control
string. The following is the list of format specifiers in C.
Usually the input is given & output is viewed on the screen with the help of input & output functions in C.
The standard or regularly used I/O functions are:
1. printf () – sends formatted output to stdout or console
2. scanf() – accepts formatted input from stdin or keyboard.
Ex:
printf(“Krishna”);
printf(“abcdefghijklmnopqrstuvwxyz”);
int a=34;
Dept of CSE/IT, VVIT Nambur
Problem Solving & Programming using C
float b = 11.23;
char c= „z‟;
printf(“%d%f%c”,a,b,c);
The above two input & output functions are also known as formatted I/O functions.
Type casting:
It refers to changing a variable of one data type into another. The compiler will automatically change one
type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point
variable, the compiler will convert the int to a float.
#include <stdio.h>
main() {
Ex-2:
#include <stdio.h>
main() {
Ex-3:
#include <stdio.h>
main() {
int i = 17;
char c = 'c'; /* ascii value is 99 */
sum = i + c;
printf("Value of sum : %f\n", sum );
}
Header Files: A header file is like a library with a predefined set of functions that help the programmer to
design codes quickly. The developer can use the library (header files) any number of times. There are more
than 20 header files in C library.
Ex: stdio.h, math.h, ctype.h, string.h, stdlib.h, dos.h, graphics.h, alloc.h etc.,
Math I/O: It includes all mathematical functions related to logarithms, trigonometric and statistical
functions in math.h. the list of functions is given below: Sqrt(), pow(), log10(), tan(), cos(), sin(), max(), sqr(),