SlideShare a Scribd company logo
For more Https://www.ThesisScientist.com
Unit 3
Operators
Operators
An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation on
data stored in variables. The variables that are operated are termed as operands.
C operators can be classified into a number of categories. They include:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operator
5. Increment and decrement operators
6. Conditional operator
7. Bitwise operators
8. Special operators
Now, let us discuss each category in detail.
Arithmetic Operators
C provides all the basic arithmetic operators. There are five arithmetic operators in C.
Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder after integer division
The division operator (/) requires the second operand as non zero, though the operands need not be integers.
The operator (%) is known as modulus operator. It produces the remainder after the division of two
operands. The second operand must be non-zero.
All other operators work in their normal way.
70
Relational Operators
Relational operator is used to compare two operands to see whether they are equal to each other, unequal,
or one is greater or lesser than the other.
The operands can be variables, constants or expressions, and the result is a numerical value. There are six
relational operators.
= = equal to
! = not equal to
< less than
< = less than or equal to
> greater than
> = greater than or equal to
A simple relation contains only one relational expression and takes the following form:
ae-1 relational operator ae-2
ae-1 and ae-2 are arithmetic expressions, which may be simple constants, variables or combination of these.
The value of the relational operator is either 1 or 0. If the relation is true, result is 1 otherwise it is 0.
e.g.: Expressions Result
4.5 < = 10 True
4.5 < -10 False
-35 > = 0 False
10 < 7+5 True
Logical Operators
Logical operators are used to combine two or more relational expressions. C provides three logical
operators.
Operator Meaning
&& Logical AND
¦¦ Logical OR
! Logical NOT
The result of Logical AND will be true only if both operands are true. While the result of a Logical OR
operation will be true if either operand is true. Logical NOT (!) is used for reversing the value of the
expression.
The expression which combines two or more relational expressions is termed as Logical Expression or
Compound Relational Expression which yields either 1 or 0.
e.g.: 1. if (age > 50 && weight < 80)
2. if ( a < 0 ¦ ¦ ch = = 'a')
3. if (! (a < 0))
71
Assignment Operators
Assignment operators are used to assign the result of an expression to a variable. The most commonly used
assignment operator is (=). Note that it is different from mathematical equality.
An expression with assignment operator is of the following form:
identifier = expression
#include <stdio.h>
main( )
{
int i;
i = 5;
printf ("%d", i);
i = i + 10;
printf ("n%d", i);
}
output will be: 5
15
In this program i = i+10; is an assignment expression which assigns the value of i+10 to i.
Expressions like i = i+10, i = i – 5, i = i*2, i = i/6, and i = i*4, can be rewritten using shorthand assignment
operators.
The advantages of using assignment operators are:
1. The statement is more efficient and easier to read.
2. What appears on the L.H.S need not to be repeated and therefore it becomes easier to write for long
variable names.
Increment and Decrement Operators
C has two very useful operators ++ and -- called increment and decrement operators respectively. These are
generally not found in other languages. These operators are called unary operators as they require only one
operand. This operand should necessarily be a variable not a constant.
The increment operator (++) adds one to the operand while the decrement operator (--) subtracts one from
the operand.
These operators may be used either before or after the operand. When they are used before the operand, it is
termed as prefix, while when used after the operand, they are termed as postfix operators.
e.g.: int i = 5;
i++;
++i;
––i;
i––;
e.g.: b = a ++; this is postfix increment expression. In the expression
firstly b = a; then a = a+1; will be executed, while in prefix increment
expression
b = - - a;
firstly a = a-1; then b = a; will be executed.
72
e.g.: # include <stdio.h>
main( )
{
int a = 10; b = 0;
a++;
printf ("n a = %d", a);
b = ++a;
printf ("n a = % d, b = % d", a, b);
b = a++;
printf ("n a = % d, b = % d", a, b);
}
output: a = 11
a = 12 b = 12
a = 13 b = 12
Conditional Operator
A ternary operator is one which contains three operands. The only ternary operator available in C language
is conditional operator pair "?:". It is of the form:
exp1 ? exp2 : exp3 ;
This operator works as follows. Exp1 is evaluated first. If the result is true then exp2 is executed, otherwise
exp3 is executed.
e.g.: a = 10;
b = 15;
x = (a > b ? a: b)
In this expression value of b will be assigned to x.
Bitwise Operators
Bitwise operators are used for manipulation of data at bit level. These operators are used for testing the bits,
or shifting them right or left. Bitwise operators may not be applied to float or double data type. It is
applicable to integer data types data only.
Some Bitwise Operators
Operator Meaning
& Bitwise Logical AND
¦ Bitwise Logical OR
^ Bitwise Logical XOR
<< Left shift
>> Right shift
~ One's complement
| (Bit-wise OR) :binary operator takes two operands of int type and
performs bit-wise OR operation. With assumption that int
size is 8-bits:
73
int a = 5; [binary : 0000 0101]
int b = 9; [binary : 0000 1001]
a | b yields [binary : 0000 1101]
& (Bit-wise AND) :binary operator takes two operands of int type and
performs bit-wise AND operation. With same assumption
on int size as above:
int a = 5; [binary : 0000 0101]
int b = 9; [binary : 0000 1001]
a & b yields [binary : 0000 0001]
^ (Bit-wise Logical XOR) :XOR gives 1 if only one of the operand is 1 else 0. With
same assumption on int size as above:
int a = 5; [binary : 0000 0101]
int b = 9; [binary : 0000 1001]
a ^ b yields [binary : 0000 1100]
<< (Shift left) :This operator shifts the bits towards left padding the
space with 0 by given integer times.
int a = 5; [binary : 0000 0101]
a << 3 yeilds [binary : 0010 1000]
>> (Shift right) :This operator shifts the bits towards right padding the
space with 0.
int a = 5; [binary : 0000 0101]
a >> 3 yeilds [binary : 0000 0000]
~ (one’s complement operator) :It is a uniary operator that causes the bits of its operand to
be inverted so that 1 becomes 0 and vice-versa. The
opearator must always precede the operand and must be
integer type of all sizes. Assuming that int type is of 1 byte
size:
inr a = 5; [binary : 0000 0101]
~a; [binary : 1111 1010]
Special Operators
C language supports some special operators such as comma operator, sizeof operator, pointer operators (&
and *), and member selection operators (. and ->). Pointer operators will be discussed while introducing
pointers and member selection operators will be discussed with structures and union. Right now, we will
discuss comma operator and sizeof operator.
(a) Comma Operator
This operator is used to link the related expressions together.
74
e.g.: int val, x, y;
value = (x = 0, y = 5, x+y);
It first assigns 10 to x, then 5 to y, finally sum x+y to val.
(b) sizeof Operator
The sizeof operator is a compile time operator and when used with an operand, it returns the
number of bytes the operand occupies. The operand may be a variable, a constant or a data type
qualifier.
e.g.: int n;
n = sizeof (int);
printf ("%d", n);
output: n = 2 /* Assuming that int size is 2 bytes */
Operator Precedence
Precedence defines the sequence in which operators are to be applied on the operands while evaluating the
expressions involving more than one operators. Operators of same precedence are evaluated from left to
right or right to left, depending upon the level. This is known as associativity property of an operator.
Summary of Precedence and Associativity
DESCRIPTION OPERATORS ASSOCIATIVITY
Function expression ( ) LR
Array expression [ ] LR
Structure operator  LR
Structure operator . LR
Unary Minus - RL
Increment/Decrement ++ -- RL
One's complement ~ RL
Negation ! RL
Address of & RL
Value at address * RL
Type cast (type) RL
Size in bytes sizeof RL
Multiplication * LR
Division / LR
Modulus % LR
Addition + LR
Subtraction - LR
Left shift << LR
Right shift >> LR
Less than < LR
Less than or equal to < = LR
Greater than > LR
Greater than or equal to > = LR
Equal to = = LR
75
Not equal to ! = LR
Bitwise AND & LR
Bitwise XOR ^ LR
Bitwise OR | LR
Logical AND && LR
Logical OR || LR
Conditional ?: RL
Assignment = RL
* = / = % = RL
+ = - = & = RL
^ = | = RL
<< = >> = RL
Comma , RL
Type Modifiers
The Basic Data Types may have modifiers preceding them to indicate special properties of the objects
being declared. These modifiers change the meaning of the Basic data types to suit the specific needs.
These modifiers are unsigned, signed, long and short. It is also possible to give these modifiers in
combination, e.g., unsigned log int.
Modifiers for char Data Type
char data type can be qualified as either signed or unsigned, both occupying one byte each, but having
different ranges. A signed char is same as an ordinary char and has a range from -128 to +127; whereas an
unsigned char has a range from 0 to 255. By default char is unsigned.
e.g.: main ( )
{
char ch = 291;
printf ("%dt%cn", ch, ch);
}
output: 35 #
Here ch has been defined as a char, and a char cannot take a value bigger than +128. That is why assigned
value of ch, 291 is considered to be 35 (291-128).
Modifiers for int Data Type
Integer quantities can be defined as short int, long int or unsigned int. short int occupies two bytes of space,
whereas long int occupies 4 bytes of space. A signed int has the same memory requirements as an unsigned
int (or a short int or a long int), the leftmost bit is reserved for the sign. With an unsigned int, all the bits are
used to represent the numerical value. The unsigned qualifier can also be applied to other qualified int. For
example, unsigned short int or unsigned long int. By default, modifier assumed with integers is signed.
Modifiers for double and float Data Type
Modifier long is used with double data type but not with float. Long double occupies 10 bytes of memory
space (usually, but actual size depends on implementation and hardware platform).
76
Data Type Range Bytes Format
signed char -128 to + 127 1 %c
unsigned char 0 to 255 1 %c
short signed int -32768 to 32767 2 %d
short unsigned int 0 to 65535 2 %u
long signed int -2147483648 to +2147483647 4 %ld
long unsigned int 0 to 4294967295 4 %lu
float -3.4e38 to 3.4e38 4 %f
double -1.7e308 to +1.7e308 8 %lf

More Related Content

What's hot

Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case Statements
Dipesh Pandey
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
Kamal Acharya
 
C string
C stringC string
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
madan reddy
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
Archana Gopinath
 
1 puc programming using c++
1 puc programming using c++1 puc programming using c++
1 puc programming using c++
Prof. Dr. K. Adisesha
 
C operator and expression
C operator and expressionC operator and expression
C operator and expression
LavanyaManokaran
 
C++
C++C++
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
Manash Kumar Mondal
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++Fahim Adil
 
String C Programming
String C ProgrammingString C Programming
String C Programming
Prionto Abdullah
 
25422733 c-programming-and-data-structures-lab-manual
25422733 c-programming-and-data-structures-lab-manual25422733 c-programming-and-data-structures-lab-manual
25422733 c-programming-and-data-structures-lab-manual
kamesh dagia
 
10. switch case
10. switch case10. switch case
10. switch case
Way2itech
 
Data types in C
Data types in CData types in C
Data types in C
Ansh Kashyap
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
Gagan Deep
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
Rai University
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
Wingston
 

What's hot (20)

Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case Statements
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
C string
C stringC string
C string
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
1 puc programming using c++
1 puc programming using c++1 puc programming using c++
1 puc programming using c++
 
C operator and expression
C operator and expressionC operator and expression
C operator and expression
 
C++
C++C++
C++
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
25422733 c-programming-and-data-structures-lab-manual
25422733 c-programming-and-data-structures-lab-manual25422733 c-programming-and-data-structures-lab-manual
25422733 c-programming-and-data-structures-lab-manual
 
Chap 6(decision making-looping)
Chap 6(decision making-looping)Chap 6(decision making-looping)
Chap 6(decision making-looping)
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
10. switch case
10. switch case10. switch case
10. switch case
 
Data types in C
Data types in CData types in C
Data types in C
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 

Similar to Types of Operators in C

C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
AmAn Singh
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
AmAn Singh
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
Jasleen Kaur (Chandigarh University)
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
Anusuya123
 
Operators and it's type
Operators and it's type Operators and it's type
Operators and it's type
Asheesh kushwaha
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
Coper in C
Coper in CCoper in C
Coper in C
thirumalaikumar3
 
c programming2.pptx
c programming2.pptxc programming2.pptx
c programming2.pptx
YuvarajuCherukuri
 
Opeartor &amp; expression
Opeartor &amp; expressionOpeartor &amp; expression
Opeartor &amp; expression
V.V.Vanniapermal College for Women
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
savitamhaske
 
4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt
RithwikRanjan
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
Mohammad Imam Hossain
 
This slide contains information about Operators in C.pptx
This slide contains information about Operators in C.pptxThis slide contains information about Operators in C.pptx
This slide contains information about Operators in C.pptx
ranaashutosh531pvt
 
data type.pptxddddswwyertr hai na ki extend kr de la
data type.pptxddddswwyertr hai na ki extend kr de ladata type.pptxddddswwyertr hai na ki extend kr de la
data type.pptxddddswwyertr hai na ki extend kr de la
LEOFAKE
 
C PRESENTATION.pptx
C PRESENTATION.pptxC PRESENTATION.pptx
C PRESENTATION.pptx
VAIBHAV175947
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Deepak Singh
 
C++ Expressions Notes
C++ Expressions NotesC++ Expressions Notes
C++ Expressions Notes
Prof Ansari
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
Neeru Mittal
 

Similar to Types of Operators in C (20)

C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
Operators and it's type
Operators and it's type Operators and it's type
Operators and it's type
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
 
Coper in C
Coper in CCoper in C
Coper in C
 
c programming2.pptx
c programming2.pptxc programming2.pptx
c programming2.pptx
 
Opeartor &amp; expression
Opeartor &amp; expressionOpeartor &amp; expression
Opeartor &amp; expression
 
Report on c
Report on cReport on c
Report on c
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
 
Operators
OperatorsOperators
Operators
 
This slide contains information about Operators in C.pptx
This slide contains information about Operators in C.pptxThis slide contains information about Operators in C.pptx
This slide contains information about Operators in C.pptx
 
data type.pptxddddswwyertr hai na ki extend kr de la
data type.pptxddddswwyertr hai na ki extend kr de ladata type.pptxddddswwyertr hai na ki extend kr de la
data type.pptxddddswwyertr hai na ki extend kr de la
 
C PRESENTATION.pptx
C PRESENTATION.pptxC PRESENTATION.pptx
C PRESENTATION.pptx
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
 
C++ Expressions Notes
C++ Expressions NotesC++ Expressions Notes
C++ Expressions Notes
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 

More from Thesis Scientist Private Limited

HTML guide for beginners
HTML guide for beginnersHTML guide for beginners
HTML guide for beginners
Thesis Scientist Private Limited
 
Ransomware attacks 2017
Ransomware attacks 2017Ransomware attacks 2017
Ransomware attacks 2017
Thesis Scientist Private Limited
 
How to write a Great Research Paper?
How to write a Great Research Paper?How to write a Great Research Paper?
How to write a Great Research Paper?
Thesis Scientist Private Limited
 
Research Process design
Research Process designResearch Process design
Research Process design
Thesis Scientist Private Limited
 
How to write a good Dissertation/ Thesis
How to write a good Dissertation/ ThesisHow to write a good Dissertation/ Thesis
How to write a good Dissertation/ Thesis
Thesis Scientist Private Limited
 
How to write a Research Paper
How to write a Research PaperHow to write a Research Paper
How to write a Research Paper
Thesis Scientist Private Limited
 
Internet security tips for Businesses
Internet security tips for BusinessesInternet security tips for Businesses
Internet security tips for Businesses
Thesis Scientist Private Limited
 
How to deal with a Compulsive liar
How to deal with a Compulsive liarHow to deal with a Compulsive liar
How to deal with a Compulsive liar
Thesis Scientist Private Limited
 
Driverless car Google
Driverless car GoogleDriverless car Google
Driverless car Google
Thesis Scientist Private Limited
 
Podcast tips beginners
Podcast tips beginnersPodcast tips beginners
Podcast tips beginners
Thesis Scientist Private Limited
 
Vastu for Career Success
Vastu for Career SuccessVastu for Career Success
Vastu for Career Success
Thesis Scientist Private Limited
 
Reliance jio broadband
Reliance jio broadbandReliance jio broadband
Reliance jio broadband
Thesis Scientist Private Limited
 
Job Satisfaction definition
Job Satisfaction definitionJob Satisfaction definition
Job Satisfaction definition
Thesis Scientist Private Limited
 
Mistakes in Advertising
Mistakes in AdvertisingMistakes in Advertising
Mistakes in Advertising
Thesis Scientist Private Limited
 
Contributor in a sentence
Contributor in a sentenceContributor in a sentence
Contributor in a sentence
Thesis Scientist Private Limited
 
Different Routing protocols
Different Routing protocolsDifferent Routing protocols
Different Routing protocols
Thesis Scientist Private Limited
 
Ad hoc network routing protocols
Ad hoc network routing protocolsAd hoc network routing protocols
Ad hoc network routing protocols
Thesis Scientist Private Limited
 
IPTV Thesis
IPTV ThesisIPTV Thesis
Latest Thesis Topics for Fog computing
Latest Thesis Topics for Fog computingLatest Thesis Topics for Fog computing
Latest Thesis Topics for Fog computing
Thesis Scientist Private Limited
 
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Thesis Scientist Private Limited
 

More from Thesis Scientist Private Limited (20)

HTML guide for beginners
HTML guide for beginnersHTML guide for beginners
HTML guide for beginners
 
Ransomware attacks 2017
Ransomware attacks 2017Ransomware attacks 2017
Ransomware attacks 2017
 
How to write a Great Research Paper?
How to write a Great Research Paper?How to write a Great Research Paper?
How to write a Great Research Paper?
 
Research Process design
Research Process designResearch Process design
Research Process design
 
How to write a good Dissertation/ Thesis
How to write a good Dissertation/ ThesisHow to write a good Dissertation/ Thesis
How to write a good Dissertation/ Thesis
 
How to write a Research Paper
How to write a Research PaperHow to write a Research Paper
How to write a Research Paper
 
Internet security tips for Businesses
Internet security tips for BusinessesInternet security tips for Businesses
Internet security tips for Businesses
 
How to deal with a Compulsive liar
How to deal with a Compulsive liarHow to deal with a Compulsive liar
How to deal with a Compulsive liar
 
Driverless car Google
Driverless car GoogleDriverless car Google
Driverless car Google
 
Podcast tips beginners
Podcast tips beginnersPodcast tips beginners
Podcast tips beginners
 
Vastu for Career Success
Vastu for Career SuccessVastu for Career Success
Vastu for Career Success
 
Reliance jio broadband
Reliance jio broadbandReliance jio broadband
Reliance jio broadband
 
Job Satisfaction definition
Job Satisfaction definitionJob Satisfaction definition
Job Satisfaction definition
 
Mistakes in Advertising
Mistakes in AdvertisingMistakes in Advertising
Mistakes in Advertising
 
Contributor in a sentence
Contributor in a sentenceContributor in a sentence
Contributor in a sentence
 
Different Routing protocols
Different Routing protocolsDifferent Routing protocols
Different Routing protocols
 
Ad hoc network routing protocols
Ad hoc network routing protocolsAd hoc network routing protocols
Ad hoc network routing protocols
 
IPTV Thesis
IPTV ThesisIPTV Thesis
IPTV Thesis
 
Latest Thesis Topics for Fog computing
Latest Thesis Topics for Fog computingLatest Thesis Topics for Fog computing
Latest Thesis Topics for Fog computing
 
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
 

Recently uploaded

road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 

Recently uploaded (20)

road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 

Types of Operators in C

  • 1. For more Https://www.ThesisScientist.com Unit 3 Operators Operators An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation on data stored in variables. The variables that are operated are termed as operands. C operators can be classified into a number of categories. They include: 1. Arithmetic operators 2. Relational operators 3. Logical operators 4. Assignment operator 5. Increment and decrement operators 6. Conditional operator 7. Bitwise operators 8. Special operators Now, let us discuss each category in detail. Arithmetic Operators C provides all the basic arithmetic operators. There are five arithmetic operators in C. Operator Purpose + Addition - Subtraction * Multiplication / Division % Remainder after integer division The division operator (/) requires the second operand as non zero, though the operands need not be integers. The operator (%) is known as modulus operator. It produces the remainder after the division of two operands. The second operand must be non-zero. All other operators work in their normal way.
  • 2. 70 Relational Operators Relational operator is used to compare two operands to see whether they are equal to each other, unequal, or one is greater or lesser than the other. The operands can be variables, constants or expressions, and the result is a numerical value. There are six relational operators. = = equal to ! = not equal to < less than < = less than or equal to > greater than > = greater than or equal to A simple relation contains only one relational expression and takes the following form: ae-1 relational operator ae-2 ae-1 and ae-2 are arithmetic expressions, which may be simple constants, variables or combination of these. The value of the relational operator is either 1 or 0. If the relation is true, result is 1 otherwise it is 0. e.g.: Expressions Result 4.5 < = 10 True 4.5 < -10 False -35 > = 0 False 10 < 7+5 True Logical Operators Logical operators are used to combine two or more relational expressions. C provides three logical operators. Operator Meaning && Logical AND ¦¦ Logical OR ! Logical NOT The result of Logical AND will be true only if both operands are true. While the result of a Logical OR operation will be true if either operand is true. Logical NOT (!) is used for reversing the value of the expression. The expression which combines two or more relational expressions is termed as Logical Expression or Compound Relational Expression which yields either 1 or 0. e.g.: 1. if (age > 50 && weight < 80) 2. if ( a < 0 ¦ ¦ ch = = 'a') 3. if (! (a < 0))
  • 3. 71 Assignment Operators Assignment operators are used to assign the result of an expression to a variable. The most commonly used assignment operator is (=). Note that it is different from mathematical equality. An expression with assignment operator is of the following form: identifier = expression #include <stdio.h> main( ) { int i; i = 5; printf ("%d", i); i = i + 10; printf ("n%d", i); } output will be: 5 15 In this program i = i+10; is an assignment expression which assigns the value of i+10 to i. Expressions like i = i+10, i = i – 5, i = i*2, i = i/6, and i = i*4, can be rewritten using shorthand assignment operators. The advantages of using assignment operators are: 1. The statement is more efficient and easier to read. 2. What appears on the L.H.S need not to be repeated and therefore it becomes easier to write for long variable names. Increment and Decrement Operators C has two very useful operators ++ and -- called increment and decrement operators respectively. These are generally not found in other languages. These operators are called unary operators as they require only one operand. This operand should necessarily be a variable not a constant. The increment operator (++) adds one to the operand while the decrement operator (--) subtracts one from the operand. These operators may be used either before or after the operand. When they are used before the operand, it is termed as prefix, while when used after the operand, they are termed as postfix operators. e.g.: int i = 5; i++; ++i; ––i; i––; e.g.: b = a ++; this is postfix increment expression. In the expression firstly b = a; then a = a+1; will be executed, while in prefix increment expression b = - - a; firstly a = a-1; then b = a; will be executed.
  • 4. 72 e.g.: # include <stdio.h> main( ) { int a = 10; b = 0; a++; printf ("n a = %d", a); b = ++a; printf ("n a = % d, b = % d", a, b); b = a++; printf ("n a = % d, b = % d", a, b); } output: a = 11 a = 12 b = 12 a = 13 b = 12 Conditional Operator A ternary operator is one which contains three operands. The only ternary operator available in C language is conditional operator pair "?:". It is of the form: exp1 ? exp2 : exp3 ; This operator works as follows. Exp1 is evaluated first. If the result is true then exp2 is executed, otherwise exp3 is executed. e.g.: a = 10; b = 15; x = (a > b ? a: b) In this expression value of b will be assigned to x. Bitwise Operators Bitwise operators are used for manipulation of data at bit level. These operators are used for testing the bits, or shifting them right or left. Bitwise operators may not be applied to float or double data type. It is applicable to integer data types data only. Some Bitwise Operators Operator Meaning & Bitwise Logical AND ¦ Bitwise Logical OR ^ Bitwise Logical XOR << Left shift >> Right shift ~ One's complement | (Bit-wise OR) :binary operator takes two operands of int type and performs bit-wise OR operation. With assumption that int size is 8-bits:
  • 5. 73 int a = 5; [binary : 0000 0101] int b = 9; [binary : 0000 1001] a | b yields [binary : 0000 1101] & (Bit-wise AND) :binary operator takes two operands of int type and performs bit-wise AND operation. With same assumption on int size as above: int a = 5; [binary : 0000 0101] int b = 9; [binary : 0000 1001] a & b yields [binary : 0000 0001] ^ (Bit-wise Logical XOR) :XOR gives 1 if only one of the operand is 1 else 0. With same assumption on int size as above: int a = 5; [binary : 0000 0101] int b = 9; [binary : 0000 1001] a ^ b yields [binary : 0000 1100] << (Shift left) :This operator shifts the bits towards left padding the space with 0 by given integer times. int a = 5; [binary : 0000 0101] a << 3 yeilds [binary : 0010 1000] >> (Shift right) :This operator shifts the bits towards right padding the space with 0. int a = 5; [binary : 0000 0101] a >> 3 yeilds [binary : 0000 0000] ~ (one’s complement operator) :It is a uniary operator that causes the bits of its operand to be inverted so that 1 becomes 0 and vice-versa. The opearator must always precede the operand and must be integer type of all sizes. Assuming that int type is of 1 byte size: inr a = 5; [binary : 0000 0101] ~a; [binary : 1111 1010] Special Operators C language supports some special operators such as comma operator, sizeof operator, pointer operators (& and *), and member selection operators (. and ->). Pointer operators will be discussed while introducing pointers and member selection operators will be discussed with structures and union. Right now, we will discuss comma operator and sizeof operator. (a) Comma Operator This operator is used to link the related expressions together.
  • 6. 74 e.g.: int val, x, y; value = (x = 0, y = 5, x+y); It first assigns 10 to x, then 5 to y, finally sum x+y to val. (b) sizeof Operator The sizeof operator is a compile time operator and when used with an operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant or a data type qualifier. e.g.: int n; n = sizeof (int); printf ("%d", n); output: n = 2 /* Assuming that int size is 2 bytes */ Operator Precedence Precedence defines the sequence in which operators are to be applied on the operands while evaluating the expressions involving more than one operators. Operators of same precedence are evaluated from left to right or right to left, depending upon the level. This is known as associativity property of an operator. Summary of Precedence and Associativity DESCRIPTION OPERATORS ASSOCIATIVITY Function expression ( ) LR Array expression [ ] LR Structure operator  LR Structure operator . LR Unary Minus - RL Increment/Decrement ++ -- RL One's complement ~ RL Negation ! RL Address of & RL Value at address * RL Type cast (type) RL Size in bytes sizeof RL Multiplication * LR Division / LR Modulus % LR Addition + LR Subtraction - LR Left shift << LR Right shift >> LR Less than < LR Less than or equal to < = LR Greater than > LR Greater than or equal to > = LR Equal to = = LR
  • 7. 75 Not equal to ! = LR Bitwise AND & LR Bitwise XOR ^ LR Bitwise OR | LR Logical AND && LR Logical OR || LR Conditional ?: RL Assignment = RL * = / = % = RL + = - = & = RL ^ = | = RL << = >> = RL Comma , RL Type Modifiers The Basic Data Types may have modifiers preceding them to indicate special properties of the objects being declared. These modifiers change the meaning of the Basic data types to suit the specific needs. These modifiers are unsigned, signed, long and short. It is also possible to give these modifiers in combination, e.g., unsigned log int. Modifiers for char Data Type char data type can be qualified as either signed or unsigned, both occupying one byte each, but having different ranges. A signed char is same as an ordinary char and has a range from -128 to +127; whereas an unsigned char has a range from 0 to 255. By default char is unsigned. e.g.: main ( ) { char ch = 291; printf ("%dt%cn", ch, ch); } output: 35 # Here ch has been defined as a char, and a char cannot take a value bigger than +128. That is why assigned value of ch, 291 is considered to be 35 (291-128). Modifiers for int Data Type Integer quantities can be defined as short int, long int or unsigned int. short int occupies two bytes of space, whereas long int occupies 4 bytes of space. A signed int has the same memory requirements as an unsigned int (or a short int or a long int), the leftmost bit is reserved for the sign. With an unsigned int, all the bits are used to represent the numerical value. The unsigned qualifier can also be applied to other qualified int. For example, unsigned short int or unsigned long int. By default, modifier assumed with integers is signed. Modifiers for double and float Data Type Modifier long is used with double data type but not with float. Long double occupies 10 bytes of memory space (usually, but actual size depends on implementation and hardware platform).
  • 8. 76 Data Type Range Bytes Format signed char -128 to + 127 1 %c unsigned char 0 to 255 1 %c short signed int -32768 to 32767 2 %d short unsigned int 0 to 65535 2 %u long signed int -2147483648 to +2147483647 4 %ld long unsigned int 0 to 4294967295 4 %lu float -3.4e38 to 3.4e38 4 %f double -1.7e308 to +1.7e308 8 %lf