SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
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 (20)

PPTX
Unit 2. Elements of C
Ashim Lamichhane
 
PPTX
Branching statements
ArunMK17
 
PDF
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
PPTX
Strings in c++
Neeru Mittal
 
PPTX
Data types in C language
kashyap399
 
PPTX
Break and continue in C
vishnupriyapm4
 
PPT
C++ Overview
kelleyc3
 
PPTX
Union in c language
tanmaymodi4
 
PPTX
C introduction by thooyavan
Thooyavan Venkatachalam
 
PPTX
Operators in java
Then Murugeshwari
 
PPT
Input and output in C++
Nilesh Dalvi
 
PPTX
Union in C programming
Kamal Acharya
 
PPTX
Loops in c language
Tanmay Modi
 
PPTX
Data Types and Variables In C Programming
Kamal Acharya
 
PDF
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Hossam Ghareeb
 
PPTX
Basic array in c programming
Sajid Hasan
 
PDF
Python
Helio Colombe
 
PPTX
Functions in c
sunila tharagaturi
 
PPTX
Looping statements in C
Jeya Lakshmi
 
PPT
Function overloading(c++)
Ritika Sharma
 
Unit 2. Elements of C
Ashim Lamichhane
 
Branching statements
ArunMK17
 
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
Strings in c++
Neeru Mittal
 
Data types in C language
kashyap399
 
Break and continue in C
vishnupriyapm4
 
C++ Overview
kelleyc3
 
Union in c language
tanmaymodi4
 
C introduction by thooyavan
Thooyavan Venkatachalam
 
Operators in java
Then Murugeshwari
 
Input and output in C++
Nilesh Dalvi
 
Union in C programming
Kamal Acharya
 
Loops in c language
Tanmay Modi
 
Data Types and Variables In C Programming
Kamal Acharya
 
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Hossam Ghareeb
 
Basic array in c programming
Sajid Hasan
 
Python
Helio Colombe
 
Functions in c
sunila tharagaturi
 
Looping statements in C
Jeya Lakshmi
 
Function overloading(c++)
Ritika Sharma
 

Similar to Types of Operators in C (20)

PPTX
C++ revision add on till now
AmAn Singh
 
PPTX
C++ revision add on till now
AmAn Singh
 
PPTX
Operators in C Programming
Jasleen Kaur (Chandigarh University)
 
PPTX
Operators in Python
Anusuya123
 
PPTX
Operators and it's type
Asheesh kushwaha
 
PDF
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
PDF
Coper in C
thirumalaikumar3
 
PPTX
Python_Module_3_AFkkkkV_Operators-1.pptx
tissot723
 
PPTX
c programming2.pptx
YuvarajuCherukuri
 
PPTX
Opeartor &amp; expression
V.V.Vanniapermal College for Women
 
DOC
Report on c
jasmeen kr
 
PPTX
3. C_OperatorsExpressions on c languyage.pptx
iramulittihad
 
PDF
Operators in c programming
savitamhaske
 
PPT
4_A1208223655_21789_2_2018_04. Operators.ppt
RithwikRanjan
 
PDF
SPL 6 | Operators in C
Mohammad Imam Hossain
 
ODP
Operators
jayesh30sikchi
 
PPTX
This slide contains information about Operators in C.pptx
ranaashutosh531pvt
 
PPTX
data type.pptxddddswwyertr hai na ki extend kr de la
LEOFAKE
 
PPTX
C PRESENTATION.pptx
VAIBHAV175947
 
PPTX
operatorsincprogramming-190221094522.pptx
ShirishaBuduputi
 
C++ revision add on till now
AmAn Singh
 
C++ revision add on till now
AmAn Singh
 
Operators in C Programming
Jasleen Kaur (Chandigarh University)
 
Operators in Python
Anusuya123
 
Operators and it's type
Asheesh kushwaha
 
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
Coper in C
thirumalaikumar3
 
Python_Module_3_AFkkkkV_Operators-1.pptx
tissot723
 
c programming2.pptx
YuvarajuCherukuri
 
Opeartor &amp; expression
V.V.Vanniapermal College for Women
 
Report on c
jasmeen kr
 
3. C_OperatorsExpressions on c languyage.pptx
iramulittihad
 
Operators in c programming
savitamhaske
 
4_A1208223655_21789_2_2018_04. Operators.ppt
RithwikRanjan
 
SPL 6 | Operators in C
Mohammad Imam Hossain
 
Operators
jayesh30sikchi
 
This slide contains information about Operators in C.pptx
ranaashutosh531pvt
 
data type.pptxddddswwyertr hai na ki extend kr de la
LEOFAKE
 
C PRESENTATION.pptx
VAIBHAV175947
 
operatorsincprogramming-190221094522.pptx
ShirishaBuduputi
 
Ad

More from Thesis Scientist Private Limited (20)

PDF
HTML guide for beginners
Thesis Scientist Private Limited
 
PDF
Ransomware attacks 2017
Thesis Scientist Private Limited
 
PDF
How to write a Great Research Paper?
Thesis Scientist Private Limited
 
PDF
Research Process design
Thesis Scientist Private Limited
 
PDF
How to write a good Dissertation/ Thesis
Thesis Scientist Private Limited
 
PDF
How to write a Research Paper
Thesis Scientist Private Limited
 
PDF
Internet security tips for Businesses
Thesis Scientist Private Limited
 
PDF
How to deal with a Compulsive liar
Thesis Scientist Private Limited
 
PDF
Driverless car Google
Thesis Scientist Private Limited
 
PDF
Podcast tips beginners
Thesis Scientist Private Limited
 
PDF
Vastu for Career Success
Thesis Scientist Private Limited
 
PDF
Reliance jio broadband
Thesis Scientist Private Limited
 
PDF
Job Satisfaction definition
Thesis Scientist Private Limited
 
PDF
Mistakes in Advertising
Thesis Scientist Private Limited
 
PDF
Contributor in a sentence
Thesis Scientist Private Limited
 
PDF
Different Routing protocols
Thesis Scientist Private Limited
 
PDF
Ad hoc network routing protocols
Thesis Scientist Private Limited
 
PDF
Latest Thesis Topics for Fog computing
Thesis Scientist Private Limited
 
PDF
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Thesis Scientist Private Limited
 
HTML guide for beginners
Thesis Scientist Private Limited
 
Ransomware attacks 2017
Thesis Scientist Private Limited
 
How to write a Great Research Paper?
Thesis Scientist Private Limited
 
Research Process design
Thesis Scientist Private Limited
 
How to write a good Dissertation/ Thesis
Thesis Scientist Private Limited
 
How to write a Research Paper
Thesis Scientist Private Limited
 
Internet security tips for Businesses
Thesis Scientist Private Limited
 
How to deal with a Compulsive liar
Thesis Scientist Private Limited
 
Driverless car Google
Thesis Scientist Private Limited
 
Podcast tips beginners
Thesis Scientist Private Limited
 
Vastu for Career Success
Thesis Scientist Private Limited
 
Reliance jio broadband
Thesis Scientist Private Limited
 
Job Satisfaction definition
Thesis Scientist Private Limited
 
Mistakes in Advertising
Thesis Scientist Private Limited
 
Contributor in a sentence
Thesis Scientist Private Limited
 
Different Routing protocols
Thesis Scientist Private Limited
 
Ad hoc network routing protocols
Thesis Scientist Private Limited
 
Latest Thesis Topics for Fog computing
Thesis Scientist Private Limited
 
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Thesis Scientist Private Limited
 
Ad

Recently uploaded (20)

PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PPTX
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
PDF
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
PDF
monopile foundation seminar topic for civil engineering students
Ahina5
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
PDF
Statistical Data Analysis Using SPSS Software
shrikrishna kesharwani
 
PPTX
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
PDF
IoT - Unit 2 (Internet of Things-Concepts) - PPT.pdf
dipakraut82
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PPTX
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
PPTX
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
site survey architecture student B.arch.
sri02032006
 
PPTX
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Thermal runway and thermal stability.pptx
godow93766
 
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
monopile foundation seminar topic for civil engineering students
Ahina5
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
Statistical Data Analysis Using SPSS Software
shrikrishna kesharwani
 
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
IoT - Unit 2 (Internet of Things-Concepts) - PPT.pdf
dipakraut82
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
MRRS Strength and Durability of Concrete
CivilMythili
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
site survey architecture student B.arch.
sri02032006
 
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 

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