0% found this document useful (0 votes)
19 views

PPS UNIT I (3)

The document provides an introduction to programming and the C language, covering the evolution of computers, their characteristics, and the generations of computer technology. It discusses problem-solving techniques, including algorithms, flowcharts, and pseudocode, as well as the structure and standards of the C programming language. Key concepts include the hardware and software components of a computer, the role of users, and the importance of structured programming for effective coding and maintenance.

Uploaded by

Srinivasan P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

PPS UNIT I (3)

The document provides an introduction to programming and the C language, covering the evolution of computers, their characteristics, and the generations of computer technology. It discusses problem-solving techniques, including algorithms, flowcharts, and pseudocode, as well as the structure and standards of the C programming language. Key concepts include the hardware and software components of a computer, the role of users, and the importance of structured programming for effective coding and maintenance.

Uploaded by

Srinivasan P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

1

UNIT I
INTRODUCTION

Evolution of Programming & Languages - Problem solving through programming - Writing


algorithms & Pseudo code - Single line and multiline comments - Introduction to C: Structure of
the C program - Input and output statements. Variables and identifiers, Constants, Keywords -
Values, Names, Scope, Binding, Storage Classes - Numeric Data types: integer, floating point,
Non-Numeric Data types: char and string - L value and R value in expression, Increment and
decrement operator - Comma, Arrow and Assignment operator, Bitwise and Size-of operator -
Arithmetic, Relational and logical Operators - Condition Operators, Operator Precedence -
Expressions with pre / post increment operator.

Introduction

● The term computer is derived from the word compute.


● The word compute means calculate.
● A Computer is an electronic machine that accepts data from the user, processes the
data by performing calculations and operations on it, and generates the desired
output results.
● Computer performs both simple and complex operations, with speed and accuracy.

Input --------🡪 Process------🡪 Output

● The computer system consists of 4 parts.


1. Hardware: Mechanical parts that makeup the computer as a machine.
● Example: Keyboard, Monitor, Hard disk drive, Printer, Floppy disk
drive, Processor, etc….
2. Software:
● Set of instructions that tell the computer about the tasks to be
performed.
● Program: Set of instructions, written in a language understood by the
computer, to perform a specific task.
● Software: Set of programs and documents.
3. Data: Isolated values or raw values.
4. Users:
● People who write computer programs or interact with the computer.
● Also called skinware, liveware, humanware, or peopleware.
● Examples: Programmers, Data entry operators, System analyst and
computer hardware engineers.

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

1. Calculating Machines: ABACUS


● First mechanical calculating device.
● Used for counting large numbers.
2. Napier’s Bones
● Mechanical device
● Used for multiplication in 1617 by John Napier.
3. Slide rule
● Developed by Edmund Gunter.
● Can perform addition, subtraction, multiplication and division.
● Used till late 1970’s
4. Pascal’s Adding and Subtraction Machine
● Developed by Blaise Pascal.
● Can perform addition and subtraction.
● Consist of wheel, gears, and cylinders.
5. Leibniz’s Multiplication and Division Machine
● Developed by Gottfried Leibniz in 1673.
● Can perform multiplication and division.
6. Punch card system
● Developed by Jacquard to control the power loom in 1801.
● He invented a punched card reader, which recognizes the presence of hole in the
punch card as binary one and the absence of hole as binary zero.
7. Babbage’s Analytical Engine
● Developed by Charles Babbage in 1823.
● Charles Babbage is called as father of computer.
● Used to do complex mathematical calculations.
● Charles Babbage and Lady Ada Lovelace developed a general-purpose
calculating machine called analytical engine.
8. Hollerith’s Punched card Tabulating Machine
● Developed by Herman Hollerith.
● The machine could read the information from a punched card and process it
electronically.
The first computer was developed in the year 1940.

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

⮚ They are discussed in terms of


o The technology used by them.(hardware and software)
o Computing characteristics(speed)
o Physical Appearance.
o Applications

First Generation (1940 to 1956): Using Vacuum Tubes


1. Hardware Technology: The computers used
⮚ Vacuum tubes for circuitry
⮚ Magnetic drums for memory
⮚ Input- through punched cards & paper tapes.
⮚ Output- displayed as printouts.
2. Software Technology:
⮚ Instructions were written in machine language.
⮚ Machine language uses 0’s and 1’s for coding.
⮚ Computers can solve one problem at a time.
3. Computing Characteristics: Milliseconds
4. Physical Appearance: Large size and occupies one large room.
5. Applications: Scientific applications
6. Examples:
⮚ UNIVersal Automatic Computer(UNIVAC)
⮚ Electronic Numerical integrator & Calculator(ENIAC)
⮚ Electronic Discrete Variable Automatic Computer(EDVAC)
Disadvantages:
1. Generates lot of heat since it uses large number of vacuum tubes.
2. Consume more electricity.
3. Very expensive to operate.
4. Difficult to program.

Second Generation (1956 to 1963): Using Transistors


1. Hardware Technology: The computers
⮚ Used transistors for circuitry.
⮚ Become faster, smaller, cheaper, energy efficient and reliable.
⮚ Used magnetic core technology for primary memory.
⮚ Used magnetic tapes and magnetic disk for secondary memory.
⮚ Input – through punched cards.
⮚ Output – Printouts.
2. Software Technology:
⮚ Instructions were written in assembly language.
⮚ Assembly language uses mnemonics like ADD for addition and SUB for
subtraction.
⮚ High level programming languages like COBOL and FORTRAN were
developed.
3. Computing Characteristics: Microseconds
4. Physical Appearance: Smaller compared to first generation.
5. Applications: Scientific and Commercial
6. Examples: PDP-8, IBM 1401 and CDC 1604.

21CSS101J-PPS
4

⮚ Produce lot of heat but less compared to first generation.


⮚ Required less maintenance.

Third Generation (1964 to 1971): Using Integrated Circuits


1. Hardware Technology: The computers
⮚ Uses Integrated Circuits(IC) chips.
⮚ IC chip- multiple transistors are placed on a silicon chip.
⮚ Silicon- type of semiconductor.
⮚ Increases the speed and efficiency.
⮚ Input – Keyboard
⮚ Output - Monitor
2. Software Technology:
⮚ Keyboard and Monitor were interfaced through the operating system.
⮚ Operating System allows different applications to run at the same time.
⮚ High level languages were used.
3. Computing Characteristics: Nanoseconds.
4. Physical Appearance: Smaller compared to second generation.
5. Applications: Commercial.
6. Examples: IBM 370, PDP 11.
⮚ Computers used less power and generate less heat than second generation computers.
⮚ Computer costs get reduced.
⮚ Maintenance cost was less.

Fourth Generation (1971 to present): Using Microprocessors


1. Hardware Technology: The computers
⮚ Uses Large Scale Integration (LSI) and the Very Large Scale Integration
(VLSI) technology.
⮚ Thousands of transistors are integrated on a small silicon chip using LSI
technology.
⮚ VLSI allows hundreds of thousands of components to be integrated in a
small chip.
⮚ This generation of computers gave rise to Personal Computer(PC).
⮚ Primary Memory: Semiconductor memory.
⮚ Secondary Memory: Magnetic disk- smaller in physical size and larger in
capacity.
⮚ Linking of computers – network – leads to emergence of Internet.
⮚ Development of mouse and hand-held devices.
2. Software Technology:
⮚ New operating systems like MS-DOS, MS-WINDOWS were developed.
⮚ Computers support GUI (Graphical User Interface).
⮚ GUI- user friendly interface that allows user to interact with the computer via
menus and icons.
⮚ Used high level programming language.
3. Computing Characteristics: Picoseconds
4. Physical Appearance: Smaller compared to previous generation. Some even fit into
the palm of the hand.
5. Application: Commercial purposes. PC became available for home user.

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.

Program Development Cycle

21CSS101J-PPS
6

1. Review the Specifications


● Collect the requirements
● Proper understanding of problem.
● Get sufficient instructions
2. Informal design
● List the major tasks.
● List the subtasks.
● Met all requirements.
3. Formal design
● Recognized format
● Rough sketch
● Part of the programmers documentation
4. Code and compile the program
● Translate each design structure into programming language
● Compile
5. Test and Debug the program
● Execute with test data
● Identify bugs
● Debug the program
6. Use and Maintain the program
● Bug free
● Make changes if necessary
● Satisfy user acceptance criteria

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.

Algorithm to find greatest among three numbers

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

1. Sequential – executed in linear order.


2. Selection (branch or conditional) – it asks a true/false question and then selects the next
instruction based on answer.
3. Iterative (Loop) – it repeats the execution of a block of instructions.

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

Flow lines Flow of control of instruction

Terminal Start symbol and End symbol

Input/Output To read and write

Calculations and initialization of


Process
memory location

Represents a decision to be made.


Decision symbol Branch to one of two or more
alternative point.

Connectors Used to join different flow lines.

21CSS101J-PPS
8

Rules for drawing flowchart


* The standard symbols should only be used
* Arrow head represents the direction of flow of control.
* Usual direction – top to bottom or left to right
* The flowchart should be clear, neat and easy to follow.
* The flowchart must have a logical start and finish.
* Only one flow line should come out from a process symbol.

* Only one flow line should enter a decision symbol. However, two or three flow lines may
leave the decision symbol.

* Only one flow line is used with a terminal symbol.

* The flow lines should not cross each other.


* Be consistent in using names and variables.
* Keep the flowchart as simple as possible.

Basic Design Structures

* Sequence

21CSS101J-PPS
9

* Selection

* Iteration or loop
Top tested loop.

Bottom 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.

Rules for writing Pseudocode


✔ Write one statement per line.
✔ Capitalize initial keyword.
✔ Indent to show hierarchy.
✔ End multiline structure.
✔ Keep statement language independent.

Example: Pseudocode to find maximum of any three numbers


READ values of A, B, C
IF A is greater than B THEN

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.

Overview of Compilers and Interpreters


● A program is a set of instructions for performing a particular task.

21CSS101J-PPS
12

● The written program is called source code.


● The source program is to be converted to the machine language, called object program with
the help of either interpreter or a compiler.
● Interpreter
o It reads only one line of a source program at a time and converts to object code.
o It checks the error line by line.
o It is easier to modify the source code with interpreters than compilers.
o The disadvantage is that it consumes more time for converting a source program to
an object program
● Compiler
o It reads the entire program and converts it to the object code.
o It provides errors of entire program.
o Only error free programs are executed.
o It consume little time for converting a source program to an object program.

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.

Programming Rules of ‘C’ Language


● All statements should be written in lower case letters.
● Upper case letters are used only for symbolic constants.
● Blank spaces are not used while declaring a variable, keyword, constant and function.
● All the statements should end with ;

Compilation and Linking Process


Execution is the process of running the program, to execute a ‘C’ program we need to
follow the steps given below.
1. Creating the program: Creating the program means entering and editing the program in
standard ‘C’ editor and save the program with .C as an extension.
2. Compiling the program: This is the process of converting the high level language program
into machine understandable form, which is called as Compilation. Here there is a possibility
to show errors i.e., Syntax errors, means the statements written in program are not in proper
syntax. If there is no error, compilation proceeds and translated program are stored in
another file with the same file name with extension “.obj”.
3. Linking the program with system library: ‘C’ Language program is a collection of
predefined functions. These functions are already written in some standard ‘C’ header files.
Therefore before executing a ‘C’ program, we need to link with system library. This can be
done automatically at the time of execution.
4. Executing the program: After the compilation the executable object code will be loaded in
the computer’s main memory and the program is executed. There is a possibility to show 2
types of errors. Logical and Data error.

‘C’ Character Set


● C Character Set is divided into
o Source Character Set
▪ Alphabets
▪ Digits
▪ Special Characters
▪ White Spaces
o Execution Character Set
▪ Escape Sequence

Source Character Set


Source Character Sets Symbols
Alphabets A to Z and a to z
Digits 0 to 9

21CSS101J-PPS
14

White Spaces Blank Space, Horizontal tabs, Vertical


tabs, New line, Form Feed.
Special Characters . ,“^ %$#@[]{}()/\!~

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

● The meaning of a keyword is predefined.

List of Keywords

S.N Keyword S.No Keyword S.N Keyword S.N Keyword


o o o
1 auto 9 double 17 int 25 struct
2 break 10 else 18 long 26 switch
3 case 11 enum 19 register 27 typedef
4 char 12 extern 20 return 28 union
5 const 13 float 21 short 29 unsigned
6 continue 14 for 22 signed 30 void
7 default 15 goto 23 sizeof 31 volatile
8 do 16 if 24 static 32 while

Data Types
● Data type is the type of the data that are going to process within the program.
● Data types are classified as

‘C’ Data Types

Primary User Defined Derived Empty

char Array

int Pointer
typedef Void
float Structure

double Union

Basic/Primitive/Primary Data Type

Data Description Memory Range Control Example


Type Bytes String
Int Integer quantity 2 bytes -32,768 to +32,767 %d or %i int a=39;
Char Single Character 1 byte -128 to +127 %c char s=’n’;
Float Single Precision 4 bytes 3.4E-38 to %f or %g float f=29.77
floating point 3.4E+38
number
Double Double Precision 8 bytes 1.7E-308 to %lf double
floating point 1.7E+308 d=2977177077
number 6

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;

Difference between declaration and definition


● Declaration only introduces the name of an identifier along with its type to the compiler
before it is used. During declaration no memory space is allocated to an identifier.
● Definition of an identifier means the declaration of an identifier plus reservation of space
for it in the memory. The amount of memory space reserved for an identifier depends upon
the data type of identifier.

L-value and R-value

21CSS101J-PPS
17

● L-value: It is the data object locator. variable refers to l-value.


● R-value: It refers to ‘read value’. 20 refers to r-value.

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

● Literal constant denotes a fixed value.


● It can be
o Integer Literal Constant
o Floating point literal Constant
o Character literal constant
o String literal constant

Integer Literal Constant


● Integer Literal constants are integer value.
● Example
o Mark=90;
o Discount=15;
● Rules for writing integer literal constants are as follows
o It must have atleast one digit
o Decimal point is not allowed
o It can be either positive or negative
o If it is negative, the sign must be preceded, for positive the sign is not necessary.
o No commas or blank spaces are allowed

Floating point literal constant


● Floating point literal constants can be written in fractional form or in exponential form.
● Example
o Discount=1.5;
o height=2.5;
● Rules for writing floating point literal constants are as follows
o It must have atleast one digit
o Must have decimal point.

21CSS101J-PPS
18

o It can be either positive or negative


o If it is negative, the sign must be preceded, for positive the sign is not necessary.
o No commas or blank spaces are allowed

Character literal constant


● A character literal constant can have one or at most 2 characters enclosed within quotes.
● Character literal constant is classified into
o Printable Character constant
▪ char c=‘s’;
o Non-Printable Character constant
▪ Escape sequences ‘\t’ , ‘\n’ , etc.

String literal constant


● A string literal constant consists of a sequence of characters enclosed within double quotes.
● Eg, “Hai”

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

Operators and Expressions


● An operator is a symbol that specifies an operation to be performed on the operands.
● The data items that operators act upon are called operands.

Classification based on Number of operands


● Unary Operators: A unary operator operates on only one operand.
● Example: a++;
● Binary Operators: A binary operator operates on two operands.
● Example: a+b;
● Ternary operator: A ternary operator operates on three operands.
● Example: z=(a>b)?a:b;

Classification based on Role of Operator

21CSS101J-PPS
19

● Arithmetic Operator
● Relational Operator
● Logical Operator
● Assignment Operator
● Increment & decrement Operator
● Conditional Operator
● Bitwise Operator
● Special Operator

Arithmetic Operator: The basic arithmetic operators are:


● + - Addition
● - - Subtraction
● * - Multiplication
● / - Division
● % - Modulo Division
Example Program:
#include <stdio.h>
void main()
{
int a,b,c,d,e,f;
a=10;
b=7;
c=a+b;
d=a-b;
e=a*b;
f=a/b;
printf(“\n%d”,c);
printf(“\n%d”,d);
printf(“\n%d”,e);
printf(“\n%d”,f);
}
Output:
17
3
70
1

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

<= Less than or equal to


>= Greater than or equal to
== Equal to
!= Not equal to
The conditions are checked using relational operators. They return either 1 or 0 as true value or false
value respectively.

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

Logical Operator: It is used to combine the results of two or more conditions.


Operator Meaning Example Return Value
(Result)
&& Logical AND (4<6)&&(8==9 0
)
|| Logical OR (7<9)||(3<1) 1
! Logical NOT !(29>89) 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

Assignment Operator: It is used to assign a value or an expression to another variable.


Syntax:
Variable=expression (or) value
Compound Assignment:

Operato Exampl Meanin


r e g
+= x+=y x=x+y
-= x-=y x=x-y
*= X*=y x=x*y
/= x/=y x=x/y

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

The value of b is -10

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

Special Operator: C Language supports some special operators. They are:

21CSS101J-PPS
24

Operato
Meaning
r
, Comma operator
Size of the
Sizeof
Operands
& and * Pointer operator

● The comma operator is used to separate the variables.


● The size of operator is used to get the size of every operands.
● The pointer operator is used to get the address of the operand.

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

Operator Precedence & Associativity of Operators


The Arithmetic Operators are evaluated from the left to right using the precedence
of operators, when the expression is written without the paranthesis.
Precedence Operators
High *, / , %
Low +, -

Rules for evaluation of expression:


1. Any expression within the parenthesis is evaluated first.
2. Arithmetic expression is evaluated from left to right using the rule of precedence.
3. Within the parenthesis, highest precedence operator is evaluated first.
4. If the operators have the same precedence, associativity is to be applied.
5. If the parenthesis is nested, the innermost sub-expression is evaluated first.
Example:
Given expression 45 + 8 – 5 * 7
Step 1: 45 + 8 – 5 * 7
Step 2: 45 + 8 – 35

21CSS101J-PPS
25

Step 3: 53 – 35
Step 4: 18
The result is 18

21CSS101J-PPS

You might also like