Unit I
Unit I
Introduction: Block diagram of a computer and it’s components, binary number system,
algorithms and Flowcharts.
C Preliminaries: Keywords, Identifiers, Constants, Variables, Data Types, Formatted I/O
functions and the Structure of a C Program with suitable illustrative programs.
Operators: Arithmetic Operators, Assignment Operators, Relational Operators, Logical
Operators, Increment and Decrement Operators, Bitwise Operators, Ternary Operator and
Special Operators.
SAMPLE QUESTIONS
Computer System
In the year of 1822 ‘Charless Babbage ‘ invented the computer . So, he is called as
father of computer.
Computer is an electronic device which accepts the input, process the input and
displays the output.
Computer Human
Central Processing Unit(CPU) Brain
Input units(Keyboard,Mouse,etc) Eye,Ear
Output units(Monitor,Printer , etc.) Mouth , Pen (in hand)
Storage device(RAM,HardDisk,etc.) Writing in the book
Computer Hardware
The collection of electronic, electrical and mechanical components is called
“Computer Hardware “.
Input Unit
Secondary storage
Eg: Keyboard,Mouse.etc Memory Unit Eg: Hard Disk,CD,DVD,etc
1)Input Unit
2)Output Unit
3)Central Processing Unit &
3)Memory Unit.
1)Input Unit:- The Unit which accepts the data from the user , convert the data into
computer acceptable form and finally submit the converted data to the computer system
for further processing, is called “Input Unit”.
2)Output Unit:- The Unit which accepts the results produced by the computer , convert
these results into human understandable form and finally displays the results to the user ,
is called “Output Unit”.
The data stored in the computer memory is always in the binary form. That is the data will be
in 0`s and 1`s.
4 bits =1 Nibble
8 bits= 1Byte(B)
1024 Bytes= 1 KiloBytes(KB)
1024 KB=1 MegaBytes(MB)
1024 MB= 1GigaBytes (GB)
3)Central Processing Unit(CPU):
The functionalities are,
1)Interprets the operations
2)coordinates the operations
3)supervises the instructions
4)controls all internal as well as external peripherals
5)controls all the computer`s functions
6)performs all calculations
7)process all the user-entered data
The components of CPU are
-Arithmetic logic unit
-control unit
-Memory unit
-Registers(PC,AC,IR,MAR,etc)
Computer software
”The collection of the programs is called software. here, the program is nothing but collection
of instructions.Computer software is classified into the following ways
1)System software
2)Application software
3)Packages and
4)Utilities
1)SYSTEM SOFTWARE:-
The system software is designed to control the operation of a computer system.It supports the
following activities:
ASSEMBLER:It is the system software which translate the assembly language program in to
a machine language program.
COMPILER:-It is the system software which translate the high-level language entire
program into a machine language program.
Utilities:
Utility software is designed to perform maintenance work on the system.
Eg: Text Editor , Memory Dump Program, Debugging tool, Sort & Merge, Loader, Linker,
etc.
Binary Number System
A binary number system is a number system that is used to represent various numbers using
only two symbols “0” and “1”. The word binary is derived from the word “bi” which means
two. Hence, this number system is called the Binary Number System. Thus, the binary
number system is a system that has only two symbols.
There are generally various types of number systems and among them the four major ones
are,
Binary Number System (Number system with Base 2)
Octal Number System (Number system with Base 8)
Decimal Number System (Number system with Base 10)
Hexadecimal Number System (Number system with Base 16)
1 001 11 1011
2 010 12 1100
3 011 13 1101
4 100 14 1110
5 101 15 1111
6 110 16 10000
7 111 17 10001
8 1000 18 10010
9 1001 19 10011
10 1010 20 10100
Algorithm
Definition:-step-by-step representation of a problem procedure is called an algorithm.
It is used for performing a task in finite amount of time.
Criteria’s/Conditions/Properties of an algorithm:
1)Finiteness
FLOW CHART
Oval:- or
Parallellogram:-
Rectangle:-
Diamond:-
Arrow:- , , ,
Circle:-
start
Read x , y values
Sum=x+y
1)A flow chart can easily explain the program logic to the program development team.
2)It is useful to prepare detail program documentation.
3)It helps to detect and remove the errors in a program.
COMPUTER LANGUAGES
Assembler
Object program
For example:-
ADD X,Y,R1
MOVE R1,SUM
ASSEMBLER
1001 010110100
11110 110101010
Source program|
Compiler/Interpreter
Object program
-------------------------
User-definedfunction(list of arguments)
{
----------------------
-----------------------
-------------- user-defined function section.
----------------------
}
5 %ld long int Used for I/O long signed integer value
S
Formatted I/O functions Unformatted I/O functions
No.
printf(), scanf, sprintf() and sscanf() getch(), getche(), gets() and puts(), are some
5
are examples of these functions. examples of these functions.
IDENTIFIERS
Constants
Which are not changed during the execution of the program called constants.Constants are
classified as
1) Numeric constants:- Here, the constant values are numbers.
2) Integer constants:-Here the constant value is integer . Integer constants do not have
the decimal point. Examples are,
Valid Invalid
48597 13,436
-433127 10.235
+25 10 244
6 75-976
3)Real constants:-The real constants are also know as floating constants.These are of two
types,
1.fractional form and 2.exponential form
Examples for fractional form are,456.78, -123.50, invalid examples are 456,456 and 456
The valid examples for exponential form are 3E4, 2e-6,0.34E6 , invalid are 23,4e5 and 456
*e8
4) String constants:- The sequence of alpha numeric characters which are in between
the pair of double quotes is called string constant.
Example:”my name is computer”
“programing with c”
“salary is 1800.00”
Invalid examples are,
‘ My name is computer”
“my name is computer”
Variables
which is changing at the time of execution of a program is called variable
variable names
variable name are identifiers used to name the variables. So variable names follows the
identifier rules are :
1) First character must be an alphabet or underscore
2) Must consist of only letters, digits or underscore
3) Only first 31 characters are considered.
4) Cannot use a key word.
5) Should not contain white space
Variable decleration
Syntax:
data-type variable name 1, variable name 2,--------,variable name n;
Example:
1) int x;
iInt x, y, z;
2) float avg,sum;
3) double length,breadth;
Variable initialisation:
Syntax:
1)datatype variable name=value;
Example:
1)int x=30;
2)float y=6.5;
1)Assignment Operators(=,+=,-=,/=,%=….)
2)Arithmetic Operators ( + , - , * , / , %)
3)Comparision Operators
a)Relational Operators ( < , <= , > , >= )
b)Equality Operators ( = = , ! =)
4)Logical Operators (&& , || , ! )
5)Bitwise Operators(&,|, ^ ,~,<<,>>)
6)Unary Operators
a)Increment Operator
B)Decrement Operator
7)Ternary Operator or Conditional Operator ( ?: )
8)Other operators
1)Assignment Operator ( = ):- Assignment Operator(=) stores the right side
i)Value into the left side variable &
Ii) Expression result value into left side variable.
y=20;
z=x+y;
1 Assign the
value of the
= Simple a=b
right operand
Assignment
to the left
operand.
3 Subtract the
right operand
and left
-= Minus and a -= b
operand and
assign
assign this
value to the
left operand.
4 Multiply the
right operand
and left
*= Multiply and a *= b
operand and
assign
assign this
value to the
left operand.
the division of
left operand
with the right
operand to the
left operand.
2)Arithmetic Operators ( + , - , * , / , %):- The Operators which are used to perform the
arithmetic perations are called Arithmetic Operators.Here arithmetic operations means
addition(+),substraction(-),
multiplication(*),division(/),and modulo division(%).
Syntax:-
Operand 1 operator operand 2 ;
Consider the following table with the declarations x=2 and y=4;
2)Comparison Operators:-
a)Relational Operators ( < , <= , > , >= ):-
The Operators which are used to form the relational expressions are called Relational
Operators.
Syntax:-
Operand 1 operator operand 2 ;
If the expression is true then the value is1 otherwise 0.
Consider the following example , x=6 and y=7.
b)Equality Operators ( = = , ! =):- The Operators which are used to check whether
they are equal or not, called Equality Operators.
Syntax:-
Operand 1 operator operand 2 ;
If the expression is true then the value is1 otherwise 0.
Consider the following example , x=6 and y=7.
3)Logical Operators (&& , || , ! ):- These Operators are used to combine the two
conditional expressions.
a)Logical AND(&&):-
Syntax:-
cond-exp1 operator cond-exp 2 ;
E.g.:- (x>y) && (z>t) ;
Truth table:- If both conditional expressions are true then only result is true.
b)Logical OR(||):-
Syntax:-
cond-exp1 operator cond-exp 2 ;
E.g.:- (x>y) || (z>t) ;
Truth table:- If both conditional expressions are false then only result is false.
Syntax:-
operator cond-exp ;
E.g.:- ! (x>y) ;
Truth table:- If both conditional expressions are true then only result is true.
Cond-exp !(Cond-exp)
1 0
0 1
int a=4,b=5;
double c=6.5,d=3.5;
char ch=’R’;
5)Bitwise Operators
In C, the following 6 operators are bitwise operators (also known as bit operators as they
work at the bit-level). They are used to perform bitwise operations in C.
1. The & (bitwise AND) in C takes two numbers as operands and does AND on every
bit of two numbers. The result of AND is 1 only if both bits are 1.
2. The | (bitwise OR) in C takes two numbers as operands and does OR on every bit of
two numbers. The result of OR is 1 if any of the two bits is 1.
3. The ^ (bitwise XOR) in C takes two numbers as operands and does XOR on every bit
of two numbers. The result of XOR is 1 if the two bits are different.
4. The << (left shift) in C takes two numbers, the left shifts the bits of the first operand,
and the second operand decides the number of places to shift.
5. The >> (right shift) in C takes two numbers, right shifts the bits of the first operand,
and the second operand decides the number of places to shift.
6. The ~ (bitwise NOT) in C takes one number and inverts all bits of it.
Let’s look at the truth table of the bitwise operators.
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
6)Unary Operators:- The Operator which adds or substract the value by 1 is called
Unary operator.
a) Increment Operator
b) Decrement Operator
a) Increment Operator:- (++)
++operand; Pre-increment
++x; Pre-increment
b) Decrement Operator:- (- -)
- -operand; Pre-decrement
--x; Pre-decremen
x++; x=6
++x; x=7
x--; x=8
--x; x=7
Example
Finds the maximum of x and y.
max=(x>y)?x:y;
From the above syntax we can say that if exp1 is true then exp2 will be stored in exp other
wise exp3 will be stored in exp . According to example if x>y is true then the exp2 like ‘x’
will be stored in max otherwise exp3 like ‘y’ will be stored .
Eg program
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,max;
x=20;
y=10;
max=(x>y)?x:y;
printf(“%d”,max);
}
Output:-
20
8. Other Operators
Apart from the above operators, there are some other operators available in C used to
perform some specific tasks. Some of them are discussed here:
sizeof Operator
Comma Operator ( , )
The comma operator (represented by the token) is a binary operator that evaluates its
first operand and discards the result, it then evaluates the second operand and returns
this value (and type).
The comma operator has the lowest precedence of any C operator.
Comma acts as both operator and separator.
Syntax
operand1 , operand2
Conditional Operator ( ? : )
Member operators are used to reference individual members of classes, structures, and
unions.
The dot operator is applied to the actual object.
The arrow operator is used with a pointer to an object.
Syntax
structure_variable . member;
and
structure_pointer -> member;
To know more about dot operators refer to this article and to know more about arrow(->)
operators refer to this article.
Casting operators convert one data type to another. For example, int(2.2000) would
return 2.
A cast is a special operator that forces one data type to be converted into another.
The most general cast supported by most of the C compilers is as follows −
[ (type) expression ].
Syntax
(new_type) operand;
Pointer operator & returns the address of a variable. For example &a; will give the
actual address of the variable.
The pointer operator * is a pointer to a variable. For example *var; will pointer to a
variable var.
To know more about the topic refer to this article.