PPS UNIT I (3)
PPS UNIT I (3)
UNIT I
INTRODUCTION
Introduction
Characteristics of Computer
● Speed: The computer can process data very fast, at the rate of millions of instructions
per second. Eg., Calculation and generation of salary slip, weather forecasting, etc.,
● Accuracy: Provides a high degree of accuracy.
21CSS101J-PPS
2
● Diligence: The computer does not get tired or fatigued. It can perform long and
complex calculations with the same speed and accuracy from the start till the end.
● Storage Capability: Large volumes of data and information can be stored in the
computer and also retrieved whenever required.
● Versatility: At one moment you can use the computer to prepare a letter document
and in the next moment you may play music or print a document.
History of Computer
Generations of Computer
⮚ The computer has evolved from a large-sized simple calculating machine to a smaller but
much more powerful machine.
⮚ There are five generations of computer.
21CSS101J-PPS
3
21CSS101J-PPS
4
21CSS101J-PPS
5
6. Examples: 1981-IBM introduced first computer for home use, 1984- Apple
introduced Machintosh.
⮚ Smaller and Cheaper.
⮚ Portable and more reliable.
⮚ Less heat and less maintenance.
⮚ GUI and pointing device facilitates easy use.
⮚ Networking – resource sharing and Communication.
Fifth Generation (Present and Next): Using Artificial Intelligence
⮚ To develop computers capable of learning and self-organization.
⮚ The computer uses Super Large Scale Integrated (SLSI) chips that are able to store
millions of components on a single chip.
⮚ Have large memory requirements.
⮚ Computer uses parallel processing, which allows several instructions to be executed in
parallel.
⮚ Faster processing speed.
⮚ Intel dual-core microprocessor – parallel processing
⮚ Computers are based on artificial Intelligence.
⮚ Simulate the human way of thinking and reasoning
⮚ AI includes areas like Expert System (ES), Natural Language Processing (NLP), Speech
Recognition, Voice Recognition, Robotics, etc….
Problem Formulation
To solve the problem using the computer, we must follow the steps below:
● Problem must be analyzed thoroughly.
● Solution method is broken down into a sequence of small tasks.
● Based on this analysis an algorithm must be prepared to solve the problem.
● The algorithm must be expressed in precise notation.
● Design a computer program in any high level language.
● The computer program is fed into the computer.
● The instruction in the program executes one after another and output the expected result.
21CSS101J-PPS
6
Problem Solving
● Write Algorithm – Explanation of solution
● Draw flowchart – Diagrammatic representation of solution
● Write Pseudo code – Written for selected solution, uses the structured programming
constructs.
Algorithm
● Algorithm is an ordered sequence of finite, well defined, unambiguous instructions for
completing a task.
● Step – by – step procedure to write a program.
Characteristics
● Precise and unambiguous.
● Should not be repeated.
● Should be written in sequence.
Step 1 : Start
Step 2 : Read the three numbers A, B, C
Step 3 : Compare A & B, If A is greater, perform step 4 else perform step 5.
Step 4 : Compare A & C, If A is greater, output “A is greatest” else output “C is greatest”.
Perform step 6
Step 5 : Compare B & C, If B is greater, output “B is greatest” else output “C is greatest”.
21CSS101J-PPS
7
Step 6 : Stop
Control Structures
Flowchart
● A flowchart is a diagrammatic representation of the logic for solving a task.
Characteristics
● Program preparation can be simplified.
● Easier to understand.
● Easy to analyze and compare.
● Assist in reviewing and debugging
● Provide effective programming documentation.
Symbols
Symbol Name Symbol Use
21CSS101J-PPS
8
* Only one flow line should enter a decision symbol. However, two or three flow lines may
leave the decision symbol.
* Sequence
21CSS101J-PPS
9
* Selection
* Iteration or loop
Top tested loop.
21CSS101J-PPS
10
Advantages
✔ Understand logic clearly
✔ Better communication
✔ Effective analysis
✔ Effective coding
✔ Effective program maintenance
Disadvantages
✔ Complex logic
✔ Alterations and Modifications.
✔ Reproduction.
✔ Cost.
Pseudocode
✔ Pseudocode is written in normal English and cannot be understood by the computer.
✔ Only concentrate on logic of the program without worrying the syntax of the instructions.
21CSS101J-PPS
11
ASSIGN A to MAX
ELSE
ASSIGN B to MAX
END IF
IF MAX is greater than C THEN
PRINT MAX is greatest
ELSE
PRINT C is greatest
END IF
STOP
Advantages
✔ Easily modified.
✔ Written easily.
✔ Read and understood easily.
✔ Converting Pseudocode to programming language is very easy.
Disadvantages
✔ It is not visual.
✔ We do not get picture of design.
✔ No standardize style or format.
Introduction to C Programming
● C is a general purpose, block structured, procedural, case-sensitive, free flow, portable and
high-level programming language.
● Designed and Developed by Dennis Ritchie at Bell Laboratories in 1972.
● It is the offspring of the “Basic Combined Programming Language” (BCPL) and B.
● C Language is a middle-level computer language.
● It reduces the gap between high-level language and low-level language that is why it is called
as middle level language.
● It is well suited for writing both application software and system software.
● C is a structured language.
● A structured language allows variety of programs in small modules.
● It is easy for debugging, testing, and maintenance.
● It is highly portable.
● Allow dynamic memory allocation.
● Fast and efficient.
C Standards
● Kernighan & Ritchie(K&R)
● ANSI C/Standard C/C89 Standard.
● ISO C/C90 Standard
● C99 Standard.
21CSS101J-PPS
12
Structure of a C Program
Documentation Section
Include Header File Section
Definition Section
Global Declaration Section
main( )
{
Declaration Part;
Executable Part;
}
Sub Program Section
{
Body of the sub program;
}
● Documentation Section: It consists of a set of comment lines used to specify the name of
program, the author and other details etc.
o Single line comment starts with //.
o Multiline comment starts with /* and ends with */.
● Include header file section or Preprocessor section: It is used to link system library files,
header files, for defining macros and for defining the conditional inclusion.
o #include<stdio.h>
o #define A 10
o #if def
o #endif
● Global Declaration Section: The variables that are used in more than one function
throughout the program are called global variables and declared outside of all the function.
i.e., before main ( ).
● Function main:
o Every program written in C language must contain main ( ) function.
o The function main ( ) is a starting point of every C program.
21CSS101J-PPS
13
o The program execution starts from the opening brace { and ends with the closing
brace }.
● Declaration part: This part is used to declare all the variables that are used in the
executable part of the program and these are called local variables.
● Executable part: It contains atleast one valid ‘C’ statement. The execution of a program
begins with opening brace ‘{‘ and ends with closing brace ‘}’. The closing brace of the main
function is the logical end of the program.
21CSS101J-PPS
14
Escape Sequence
Escape Use
Sequence
\’ Single Quotation Mark
\” Double Quotation Mark
\? Question Mark
\\ Backslash character
\a Alert
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Horizontal Tab
\v Vertical Tab
\0 Null Character
C Tokens
● Identifiers
● Keywords
● Constants
● Strings
● Operators
● Special symbols
Identifiers
● Name of an object
● It can be variable name, a label name, a function name, a typedef name, a macro name, etc.
Rules
● Can have letters, digits or underscores.
● First character of an identifier name must be a letter or an underscore.
● First character cannot be a digit.
● No space and special symbols are allowed between the identifier.
● Identifier cannot be a keyword.
Example
● #define N 5
# define MAX 10
N and MAX are identifiers
● main( )
● stud_name, sum, height…
Keyword
● Keyword is a reserved word that has a particular meaning in the programming language.
21CSS101J-PPS
15
List of Keywords
Data Types
● Data type is the type of the data that are going to process within the program.
● Data types are classified as
char Array
int Pointer
typedef Void
float Structure
double Union
Type Qualifiers
● short
21CSS101J-PPS
16
● long
● signed
● unsigned
Variables
● Value of variable can be changed.
● Can hold data of different data types.
● Its value may be changed during the program execution.
● A variable can be assigned different values at different times during the execution of a
program.
Rules
● Can have letters, digits or underscores.
● First character of an identifier name must be a letter or an underscore.
● First character cannot be a digit.
● No space and special symbols are allowed between the identifier.
● Identifier cannot be a keyword.
Variable Declaration
● Declaration will tell the compiler what the variable name and type of the data that the
variable will hold.
● Syntax
o data_type v1, v2, v3,….. , vn;
● Example
o int code;
o char sex;
o float price;
Initializing variables
● Initialization of variables can be done using the assignment operator(=).
● Syntax
o variable=constant; (or)
o data_type variable=constant;
● Example
o A=10; (or)
o int A=10;
21CSS101J-PPS
17
Constants
● A constant is an entity whose value remains the same throughout the execution of a
program.
● Constants are classified as
o Literal Constant
o Qualified Constant
o Symbolic Constant
Literal Constant
21CSS101J-PPS
18
Qualified Constant
● Qualified constant are created by using const qualifier.
● Syntax
o const data_type variable=value;
● Example
o const int a=10;
● The value of the variable a cannot be modified.
Symbolic Constant
● Symbolic constant are created with the help of the ‘define’ preprocessor directive.
● For example, #define PI 3.14
o PI is symbolic constant with value 3.14
21CSS101J-PPS
19
● Arithmetic Operator
● Relational Operator
● Logical Operator
● Assignment Operator
● Increment & decrement Operator
● Conditional Operator
● Bitwise Operator
● Special Operator
Relational Operator: Relational operators are used to compare two or more operands. Operands
may be variables, constants or expressions. The various relational operators are
Operator Meaning
< Less than
> Greater than
21CSS101J-PPS
20
Example Program
#include<stdio.h>
void main()
{
clrscr();
printf("\nCondition : Return Value");
printf("\n5!=5 : %d",(5!=5));
printf("\n10<11 : %d",(10<11));
printf("\n12>9 : %d",(12>9));
printf("\n55>=90 : %d",(55>=90));
printf("\n45<=91 : %d",(45<=91));
printf("\n78==78 : %d",(78==78));
getch();
}
Output:
Condition : Return Value
5!=5 : 0
10<11 : 1
12>9 : 1
55>=90 : 0
45<=91 : 1
78==78 : 1
Example Program:
#include<stdio.h>
void main()
{
21CSS101J-PPS
21
clrscr();
printf("\nCondition : Return Value");
printf("\n5!=5&&6<9 : %d",(5!=5)&&(6<9));
printf("\n10<11||10==10 : %d",(10<11)||(10==10));
printf("\n!(12>9) : %d",!(12>9));
getch();
}
Output:
Condition : Return Value
5!=5&&6<9 : 0
10<11||10==10 : 1
!(12>9) : 0
Example Program:
#include <stdio.h>
void main()
{
int a,b;
clrscr();
a=10; //Assignment Statement
b=6;
a+=b; // Compound Statement
b-=a;
printf("\nThe value of a is %d",a);
printf("\nThe value of b is %d",b);
getch();
}
Output:
The value of a is 16
21CSS101J-PPS
22
Increment & Decrement Operators: These operators are called Unary operators. They are used
for incrementation & decrementation operation.
Operato
Meaning
r
++x Pre-increment
X++ Post increment
--y Pre-decrement
Post
Y--
decrement
Example Program:
#include <stdio.h>
void main()
{
int a,b;
clrscr();
a=10;
b=6;
printf("\nThe value of a is %d",a++);
printf("\nThe value of a is %d",++a);
printf("\nThe value of b is %d",b--);
printf("\nThe value of b is %d",--b);
getch();
}
Output:
The value of a is 10
The value of a is 12
The value of b is 6
The value of b is 4
Conditional Operator: It checks the condition and executes the statements depending on the
condition.
Syntax:
condition?exp1:exp2
The ? operator acts as a ternary operator. If the condition is true, it evaluate the first expression
otherwise it evaluates the second expression.
Sample Program:
#include <stdio.h>
void main()
{
int a,b,big;
21CSS101J-PPS
23
clrscr();
a=10;
b=6;
big=a>b?a:b;
printf("\nThe biggest value is %d",big);
getch();
}
Output:
The biggest value is 10
Bitwise Operators: They are used to manipulate the data at bit level. They operate on integers only.
Operato
Meaning
r
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
One’s
~
Complement
>> Right Shift
<< Left Shift
Example Program:
#include <stdio.h>
void main()
{
int a,b,c,d,e;
clrscr();
a=10;
b=6;
c=a&b;
printf("\nThe value of c(AND) is %d",c);
d=a|b;
printf("\nThe value of d(OR) is %d",d);
e=a^b;
printf("\nThe value of e(XOR) is %d",e);
getch();
}
Output:
The value of c(AND) is 2
The value of d(OR) is 14
The value of e(XOR) is 12
21CSS101J-PPS
24
Operato
Meaning
r
, Comma operator
Size of the
Sizeof
Operands
& and * Pointer operator
Example Program:
#include <stdio.h>
void main()
{
int a;
char b;
clrscr();
printf("\nThe size of a is %d",sizeof(a));
printf("\nThe size of b is %d",sizeof(b));
getch();
}
Output:
The size of a is 2
The size of b is 1
21CSS101J-PPS
25
Step 3: 53 – 35
Step 4: 18
The result is 18
21CSS101J-PPS