R18 PPS Unit-I
R18 PPS Unit-I
R18 PPS Unit-I
COMPUTER
SYSTEM
HARDWARE SOFTWARE
Computer Hardware :
These are the physical components of a computer
It consists of 5 Parts
1) Input Devices 4) Primary Storage
2) Output Devices 5) Secondary Storage
3) CPU
Computer Software:
The software is used to make the hardware of the computer to function
Software is collection of programs (or) instructions
Computer software is divided into 2 categories
1) System Software
2) Application Software
Charles Babbage (16 December 1791 –18 October 1871) was an English polymath.
He is known as father of computers. He was invented first analytical computer in
year 1811.
The term computer is derived from the Latin term “computare”, this means to
calculate or programmable machine. Computer cannot do anything without a
Program. Charles Babbage is called the "Grand Father" of the computer.
1.2. Software
There are two different ways that a large computer can be shared by many different
users. The computing environments are classified into 4 types. They are
1) Personal computing environment
2) Time sharing environment
3) Client – server environment
4) Distributed computing environment
1) Personal computing environment
In a personal computer, all the basic elements of the CPU are
combined into the microprocessor and all the hardware components
are tied together.
The whole computer can be used by the user to perform his task
independently
Disadvantage
Much of the CPU time is wasted
Advantage :
Computer is busy always reducing CPU idle time
Disadvantage:
Response becomes slow as the number of users connected to the central computer
increases
Terminals
Fig : Time-sharing environment
3) Client – Server environment
This environment splits the computing function between a central
computer and user computer
Users are given personal computer (PC’s) on workstations so that
some of computation responsibility can be moved from central
computer and assigned to the workstation
User’s workstations (or) micro computers are called as “Clients”
The powerful central mainframe (or) micro computer is known as
“Server”
Advantages:
- Response time is faster
- Users are more productive
There are three levels of programming languages are available. They are:
Machine Languages
A language that is directly understood by the computer without any
translation is called machine languages. A machine language is string of 0s and
1s. Machine language are usually referred to as the first generation languages.
Advantages:
Execution speed is very fast
Efficient use of primary memory
It does not require any translation because machine code is directly
understood by the computer.
Disadvantages
Machine dependent: The machine language is different for different
types of computers.
Difficult to write program: because it requires memorizing dozens
of opcodes
for different commands.
Error prone:
Difficult to modify
4GLs are designed to reduce the overall time, effort and cost of software
development. The main domains and families of 4GLs are: database queries,
report generators, data manipulation, analysis and reporting, screen painters and
generators, GUI creators, mathematical optimization, web development and
general purpose languages. also known as a 4th generation language, a
domain specific language, or a high productivity language.
1.5 Algorithm
Advantages:
1. It is step-by-step solution to a given problem which is very easy to
understand.
2. It has got a definite procedure.
3. It is easy to first develop an algorithm, then convert into a flowchart and
then into a computer program.
4. It is easy to debug as every step has got its own logical sequence.
5. It is independent of programming languages.
Disadvantages:
1. It is time consuming and cumbersome as an algorithm is developed first
which is converted into flowchart and then into a computer program
2. Algorithm lacks visual representation hence understanding the logic
becomes difficult.
Examples:
1. start
2. Read the 3 variables of a,b&c
3. Compute sum = a+b+c
4. compute avg = sum/3
5. Print sum & avg values
6. Stop
The example of a pseudo code to add two numbers and display the result as
shown below:
Advantages
Logical Error
Improper coding of individual statements either or sequence of
statements causes this type of computer program. It does not stop the program
execution but gives wrong results.
Runtime Error
This error in a computer program stops its execution. It may be caused
by an entry of invalid data.
Computer understands machine language only. So, need to convert the given HLL
code in to machine language by using compiler. First, we need to write the
program then execute it to get the desired output.
Text editor software is used to write, change and store the program.
Every file in C must be stored with .c extension. Like “add.c”.
The object file is not only enough to get the output. The object code of the
keywords used in the program is needed to be combined with the object file. For
example in any program, We use printf keyword and the object code of printf is
needed to be combined with object file.
Compiler
It is a program which translates a high level language program into a machine
language program.
Interpreter
An Interpreter is a program which translates statements of a program into
machine code. It translates only one statement of the program at a time.
Linker
The machine code relating to the source code you have written is
combined with some other machine code to derive the complete program in an
executable file. This is done by a program called the linker.
Loader
Loader is a program that loads machine codes of a program into the
system memory. In Computing, a loader is the part of an Operating System that
is responsible for loading programs.
1.10. Introduction to C
C language is not high level language and not a low level language. It is a
middle level language with high level and low level features.
C is a general purpose, structured programming language. C language is
one of the most popular computer languages today because it is a structured, high
level, machine independent language. It allows software developers to develop
software without worrying about the hardware platforms where they will be
implemented.
Importance of C
It is a robust language whose rich set of built in functions and operators can be
used to write any complex program. The C compiler combines the capabilities of
an assembly language with the features of a higher level language and therefore it
is well suited for both system software and business packages.
Clarity refers to the overall readability of the program, with particular emphasis on
its underlying logic. If a program is clearly written, it should be possible for another
programmer to follow the program logic without undue effort.
Simplicity. The clarity and accuracy of a program are usually enhanced by keeping
things as simple as possible, consistent with the overall program objectives.
#include"stdio.h"
int main() {
/* my first program in C */
return 0;
}
Output :
Hello, World!
similar way we will use the available characters while writing programs in any
programming language.
1. Source Characters
These are characters that are used to create source text file. Source characters
include
Alphabets A to Z, a to z
Digits 0,1,1,3,4,5,6,7,8,9
Special Characters , . ; : ? ‘ “ ! | / \ ~ _ $ % # & ^ * + - <> ( ) { } [ ] and blank
2 Execution Characters
1.12 Comments
C supports different types of data. The type or Data type refers a type of
data that is going to be stored in variable. Data types can be broadly classified as
shown in figure
C provides a standard minimal set of data types. Sometimes these are also called
as ‘Primitive types’. They are:
‘char’ is used to store any single character.
‘int’ is used to store integer value.
‘float’ is used to store floating point value and
‘double’ is used for storing long range of floating point number.
Type Qualifiers
In addition, C has four type qualifiers, also known as type modifiers which
precede the basic data type. A type modifier alters the meaning of basic data type
to yield a new data type. They are as follows:
short
long ( to increase the size of an int or double type)
signed
unsigned
Size qualifiers: alters the size of basic data type. The keywords long and short are
two size qualifiers. For example:
long int i;
The size of int is 1 bytes but, when long keyword is used, that variable will be
either 4 bytes or 8 bytes.
Sign qualifiers: Whether a variable can hold only positive value or both values is
specified by sign qualifiers. Keywords signed and unsigned are used for sign
qualifiers.
void type
This type holds no value and thus valueless. It is primarily used in three cases:
1. To specify the return type of a function (when the function returns no value)
2. To specify the parameters of the function (when the function accepts no
arguments from the caller.)
3.To create generic pointers.
Constants
Constant can be defined as a value that can be stored in memory and cannot be
changed during execution of the program. Constants are used to define fixed
values like pi. C has four basic types of constants. They are:
Integer Constant
An integer constant must have at least one digit and should not have a decimal
point. It can be either positive or negative.
Examples for integer constants
1 9 134 999
A floating point constant is decimal number that contains either a decimal point or
an exponent. In the other words, they can be written in 1 forms: fractional and
exponential.
When expressed in fractional form, note the following points.
1.There should be at least one digit, and could be either positive or
negative value. A decimal point is must.
2.There should be no commas or blanks.
Examples for fractional form
11.33 -19.56 +113.89 -0.7
When expressed in exponential form, a floating point constant will have 1
parts. One is before e and after it. The part which appears before e is known as
mantissa and the one which follows is known as exponent. When expressed in this
format, note the following points.
Variables/Identifiers
Variable is named memory location that can be used to store values. These
variables can take different values but one value at a time. These values can be
changed during execution of a program.Identifiers are basically names given to
program elements such as variables, arrays and functions.
1. The only characters allowed are alphabets, digits and underscore (_).
2. They must start with an alphabet or an underscore (_).
3. It can not include any special characters or punctuation marks (like #, $, ^, ?,
., etc.) except underscore (_).
4. They can be of any reasonable length. They should not contain more than 31
characters. But it is preferable to use 8 characters.
5. There cannot be two successive underscores.
6. Reserved words/keywords cannot be identifiers.
7. Lower case or uppercase letters are significant.
C Keywords
1.16 Delimiters
Delimiters are used for syntactic meaning in C. These are given below:
Link section
The link section provides instructions to the compiler to link functions from
the system library.
#include directive
The #include directive instructs the compiler to include the contents of the
file "stdio.h" (standard input output header file) enclosed within angular brackets.
Definition section
The definition section defines all symbolic constants. #define PI 3.1413
Subprogram section
The subprogram section contains all the user-defined functions that are
called in the main () function. User-defined functions are generally placed
immediately after the main () function, although they may appear in any order.
#include<stdio.
h> int main( )
{
printf(“Welcome to C
Programming\n”); return 0;
}
1. Arithmetic operators
#include<stdio.h
> int main()
{
int a=9,b=3; printf("%d+%d=
%d\n",a,b,a+b); printf("%d-
%d=%d\n",a,b,a-b);
printf("%d*%d=%d\
n",a,b,a*b); printf("%d/%d=
%d\n",a,b,a/b); printf("%d%%
%d=%d\n",a,b,a%b); return 0;
}
Output
9+3=11
9-3=6
9*3=17
9/3=3
9%3=0
3. Relational operators
4. Logical operators
Operators which are used to combine two or more relational expressions are
known as logical operators. C language supports three logical operators – logical
AND(&&), logical OR(||), logical NOT( !).
A B A&&
B
0 0 0
0 1 0
1 0 0
1 1 1
Logical OR
It is used to simultaneously evaluate two conditions or expressions with
relational operators. If one or both the expressions on the left side and right side
of logical operators is true then the whole expression is true otherwise false. The
truth table of logical OR operator is given below:
A B A||B
0 0 0
0 1 1
1 0 1
1 1 1
Logical NOT
It takes single expression and negates the value of expression. The truth table of
logical NOT operator is given below
A !
A
0 1
1 0
For example: x and y are two variables. The following equations explain the use of
logical operators.
z1 = x&&y …
1 z1 = x||y …
1
z3 =!x …3
Equation1 indicates that z1 is true if both x and y is true. Equation1 indicates z1
is true when x or y or both true. Equation3 indicates z3 is true when x is not true.
The increment operator is unary operator that increases value of its operand
by 1. Similarly, the decrement operator decreases value of its operand by 1. These
operators are unique in that they work only on variables not on constants.These
are used in loops like while, do-while and for statements.
There are two ways to use increment or decrement operators in expressions.
If you put the operator in front of the operand (prefix), it returns the new value of
the operand (incremented or decremented). If you put the operator after the
operand (postfix), it returns the original value of the operand (before the increment
or decrement).
18
n",x--);
printf("x=%d\n",--x);
printf("x=%d\
n",-x); return 0;
}
Outpu
t x=5
x=7
x=7
x=5
x=-5
5 . Bitwise operators
As the name suggests, the bitwise operators operate on bits. These operations
include:
1. bitwise AND(&)
2. bitwise OR(|)
3. bitwise X-OR(^)
4. bitwise NOT(~)
5. shift left(<<)
6. shift right(>>)
Bitwise AND (&)
When we use the bitwise AND operator, the bit in the first operand is
ANDed with the corresponding bit in the second operand. If both bits are 1, the
corresponding bit in the result is 1 and 0 otherwise. For example:
In a C program, the & operator is used as
follows. int a=4,b=1,c;
c=a&b;
printf(“%d”,c); //prints 0
Bitwise OR (|)
When we use the bitwise OR operator, the bit in the first operand is ORed
with the corresponding bit in the second operand. If one or both bits are 1, the
corresponding bit in the result is 1 and 0 otherwise. For example:
In a C program, the | operator is used as
follows. int a=4,b=1,c;
c=a|b;
printf(“%d”,c); //prints 6
For example:
In a C program, the | operator is used as
follows. int a=4,b=1,c;
c=a^b;
printf(“%d”,c); //prints 6
110
Bitwise NOT (~)
One’s complement operator (Bitwise NOT) is used to convert each 1 to 0
and 0 to 1, in the given binary pattern. It is a unary operator i.e. it takes only one
operand.
101
Shift operators
C supports two bitwise shift operators. They are shift-left (<<) and shift-
right (>>). These operations are simple and are responsible for shifting bits either
to left or to the right. The syntax for shift operation can be given as:
operand operator num
where the bits in operand are shifted left or right depending on the operator (<<,
>>) by the number of places denoted by num.
20
Left shift operator
When we apply left-shift, every bit in x is shifted to left by one place. So,
the MSB (Most Significant Bit) of x is lost, the LSB (Least Significant Bit) of x is
set to 0.
13 11 11 10
8 4 1 1
x=4 0 1 0 0
0 1 0
x>>1 = 0 0 1 0 the result of x>>1
is 1
13 11 11 10
8 4 1 1
x=4 0 1 0 0
0 1 0
x>>1 = 0 0 1 0 the result of x>>1
is 1 Right-shift is equals to division by 1.
21
6.Assignment operators
Assignment operator
(=)
In C, the assignment operator (=) is responsible for assigning values to
variables. When equal sign is encountered in in an expression, the compiler
processes the statement on the right side of the sign and assigns the result to
variable on the left side.
For example,
int
x;
x=1
0;
Assigns the value 10 to variable x. if
we have, int x=1,y=3,sum=0;
sum = x +
y; then
sum=5.
The assignment has right-to-left associativity, so the expression
int a=b=c=10;
is evaluated as
(a=(b=(c=10)))
#include<stdio.h>
int main()
{
int k=5;
i=k++, j =k++, k++, ++k; printf(i=%d \t j=%d”, i,j); return 0;
}
Output i=5 j=6
24
sizeof Operator
The operator sizeof is a unary operator used calculates the size of data types
and variables. The operator returns the size of the variable, data type in bytes. i.e.
the sizeof operator is used to determine the amount of memory space that the
variable/data type will take. The outcome is totally machine-dependent.
For example:
#include<stdio.
h> int main()
{
print("char occupies %d bytes\n",sizeof(char)); printf("int occupies %d bytes\
n",sizeof(int)); printf("float occupies %d bytes\n",sizeof(float));
25
II
char occupies 1
bytes int occupies 1
bytes float occupies
4 bytes double
occupies 8 bytes
long double occupies 10 bytes
1.19 Expressions
In C programming, there are mainly two types of expressions are available. They
are as follows:
1.Simple expression
2.Complex expression
Operator Precedence
It definesthe order in which operators in an expression are evaluated
depends on their relative precedence. Example: Let us see x=1+1*1
1st pass- 1+1*1
1nd pass- 1+4
3rd pass- 6 that is x=6.
Associativity defines the order in which operators with the same order of
precedenceare evaluated. Let us see x=1/1*1
The function accepts two arguments - control string, which controls what gets
printed, and list of variables.
The control string is all-important because it specifies
o The type of each variable in the list and how user wants it printed.
26
II
It is also called as format specifier.
o Characters that are simply printed as they are.
o Control string that begins with a % sign.
o Escape sequences that begin with a \ sign
List of variables must be separate by comma.
The number of format specifiers must exactly match the number of
variables.
Flag Meanin
g
− Left justify the display
+ Display the positive or negative sign of
value
0 Pad with leading zeros
27
II
spac Display space if there is no sign
e
The simplest printf statement is
printf(“Welcome to the world of C language”);
When executed, it prints the string enclosed in double quotes on screen.
Examples: printf(“Result:%d%c
%f\n”,11,’a’,1.3); Result:11a1.3
printf(“Result:%d\t%c\t%f\n”,11,’a’,1.3);
Result:11 a 1.3
printf(“%6d\n”,1134);
1 1 3 4
printf(“%06d\n”,1134);
0 0 1 1 3 4
printf(“%-6d\n”,1134);
1 1 3 4
printf(“%-06d\n”,1134);
- 0 1 1 3 4
printf(“%1d\n”,1134);
1 1 3 4
printf(“%9.1f\n”,113.456);
0 0 1 1 3 4 . 4 5
Input : In any programming language input means to feed some data into
program. This can be given in the form of file or from command line. C
programming language provides a set of built- in functions to read given input and
feed it to the program as per requirement.
1. Formatted I/O
scanf()
28
II
printf()
2. Unformatted I/O
gets(), puts()
getch(), getche(), putch()
getchar(), putchar()
The functions used for input and output are stored in the header file stdio.h. If
programmer use any of the above function it is necessary to include header file.
The function used to get input from the user during execution of program and
stored in a variable of specified form is called scanf() function. This function
should have at least two parameters. First parameter is control string which is
conversion specification character. It should be within double quotes. This may be
one or more, it depends on the number of variables. The other parameter is
variable names.Each variable must preceded by ampersand (&) sign. This gives
starting address of the variable name in memory. scanf ( ) uses all the format
specifiers used in printf( ) function.
Note: comments are not allowed in scanf()
Syntax:
scanf(“format string”,& variable name);
Types:
Single input e.g scanf(“%d”,&a);
Multiple input scanf(“%d %c %d”, &a,&op,&b);|
1. #include <stdio.h>
int main()
{
int x;
printf(“Enter number:”);
scanf(“%d”, &x);
printf(“The value of x is %d\n”, x);
return 0;
}
29
II
Output
Enter a number: 45 The value of x is 45
Example : 2
#include<stdio.h>
#include<conio.h>
void main ()
{
int a;
clrscr();
printf("Enter integer value:");
scanf("%d",&a);
printf("The value you enter = %d",a);
getch();
}
Output
Enter integer value:5 The value you enter =5
2.Printf () Function
This function is used to display text, constant or value of variable on screen in
specified format.
Syntax:
printf(“format string”, argument list);
Example:1
#include<stdio.h>
#include<conio.h>
void main ()
{
printf(“HELLO WORLD!”);
getch();
}
Output
HELLO WORLD!
Example:2
#include <stdio.h>
main()
{
int dec = 5;
char str[] = "abc";
char ch = 's';
float pi = 3.14;
30
II
This special function is used to input string values from the user during
execution of the program. As the user press enter key when typing after the
string. A null character (\0) is automatically entered at the end of the string.
Syntax:
gets(variable name);
Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
char name[15];
clrscr();
printf("enter your name:");
gets(name);
printf("your name is:");
puts(name);
getch();
}
Output:
enter your name: somebody your name is: somebody
(iii) getch()
getch() is used to get a character from console but does not echo to the screen. It
31
II
reads a single character directly from the keyboard, without echoing to the screen.
Syntax
char ch;
ch = getch();
Program
#include <stdio.h>
void main()
{
char ch;
ch = getch();
printf("Input Char Is :%c",ch);
}
(iii) getche()
getche() is used to get a character from console, and echoes to the screen. getche
reads a single character from the keyboard and echoes it to the current text
window, using direct video or BIOS.
Syntax
char ch;
ch = getche();
Program
#include <stdio.h>
void main()
{
char ch;
ch = getche();
printf("Input Char Is :%c",ch);
}
(iv) putch()
Syntax
putch(variable_name);
Example char ch = 'a'; putch(ch)
Program
32
II
#include <stdio.h>
void main()
{
char ch='a';
putch(ch);
}
(vi) getchar()
The getchar function is a part of the standard C input/output library. It returns a
single character from a standard input device (typically a keyboard). The function
does not require any arguments, though a pair of empty parentheses must follow
the word getchar.
Syntax
character variable = getchar( );
where character variable refers to some previously declared character variable.
(vii) putchar()
The putchar function like getchar is a part of the standard C input/output library. It
transmits a single character to the standard output device (the computer screen).
Syntax
putchar(character variable)
where character variable refers to some previously declared character variable.
Program
#include <stdio.h>
int main( )
{
int c;
printf( "Enter a value :");
c = getchar( );
printf( "\nYou entered: ");
putchar( c );
return 0;
}
Enter a value : this is test
You entered: t
33
II
Unit1
Long answers:
Short answes:
1. What are various computer languages?
2. Write a “C” statement to find the value of “n” when divided by 32 Without
using arithmetic operator and store the result in variable “a”
3. What are the various phases of SDLC?
34
II
35
II
36