SlideShare a Scribd company logo
1 of 37
Structured Programming Language
Operators in C
Mohammad Imam Hossain,
Lecturer, CSE, UIU
Operator
An operator is a program element that is applied to one or
more operands in an expression or statement.
• Operands can be a value or a variable.
• Operators that take
• one operand  Unary operators.
• two operands  Binary operators.
• three operands  Ternary operators.
 Example:
5 + 7
Or,
a + b where a , b are two variables.
Types of Operators
C Arithmetic Operators
Operator Purpose
+ Adds two operands
- Subtracts second operand from the first
* Multiplies two operands
/ Divides numerator by denominator
% Remainder after integer division
Examples
a= 10, b=3 result v1= 12.5,
v2 = 2.0
result c1=‘P’
c2 = ‘T’
result
a+b 13 v1+v2 14.5 c1 80
a-b 7 v1-v2 10.5 c1+c2 164
a*b 30 v1*v2 25.0 c1 + c2 +5 169
a/b 3 v1/v2 6.25 c1+c2+’5’ 217
a%b 1
 If one or both operands represent negative values,
then the addition, subtraction, multiplication and
division operations will result in values whose signs
are determined by the usual rules of algebra.
 For remainder, most versions of C assign the sign of
the first operand to the remainder to fulfill the next
condition
dividend = (integer quotient)*divisor + remainder
i.e.
a = ((a/b)*b)+(a%b)
will always be satisfied.
Values Results
11 % 3 2
11 % -3 2
-11 % 3 -2
-11 % -3 -2
Examples
Type Conversion
• The data type is promoted from lower to higher.
char
short
unsigned
short
int
unsigned
int
long
unsigned
long
long long
unsigned
long long
float
double
long
double
Example:
int i=7;
double d=5.5;
char c=‘w’;
Then the data type of
(i+c)-(2*d/5)
is double and the result is
123.8
Type Casting
• Explicit type conversion can be forced in any expression, with
a unary operator called a cast.
• Syntax: (type) expression
• Example: int n;
float x;
x=(float) n;
• The above statement will convert the value of n to a float
value before assigning to x, but n is not altered.
• Type casting doesn’t change the actual value of the variable
but the resultant value may be put in temporary storage.
Precedence(order of evaluation)
• The operators within C are grouped hierarchically according to
their precedence.
• Operations with a higher precedence are carried out before
operations having a lower precedence.
• Example:
2 precedence groups for arithmetic operators.
Group 1(higher precedence): *, /, %
Group 2(lower precedence): +, -
Associativity
• The order in which consecutive operations within the same
precedence group are carried out.
• Example:
each of the 2 precedence groups of arithmetic operators has
associativity from
Left  Right
a – b / c * d
Associativity
• The order in which consecutive operations within the same
precedence group are carried out.
• Example:
each of the 2 precedence groups of arithmetic operators has
associativity from
Left  Right
≡ a – [(b/c) * d]a – b / c * d
Code Sample
Unary Operators
• Operators that act upon a single operand to produce a new
value
• Same precedence group
• Associativity Right  Left
Operator Purpose Example
- To negate a numerical constant,
variable or expression
-743, -0X7FFF,-0.2, -5E-8,
-var1, -(x+y), -3*(x+y)
++ Pre/Post increment operator ++a, a++
-- Pre/Post decrement operator --a, a--
sizeof() Returns size of its operand in bytes sizeof(int)
(type) Type casting (float) 5
Code Sample
c=a++; /// c=a; a=a+1;
c=++a; /// a=a+1; c=1;
Relational Operators
Operator Purpose
< Check if operand on the left is smaller than operand on the right
<= Check if operand on the let is smaller than or equal to right operand
> Check if operand on the left is greater than operand o the right
>= Check left operand is greater than or equal to right operand
• Same precedence group
• Associativity Left  Right
Equality Operators
Operator Purpose
== Check if two operands are equal
!= Check if two operands are not equal
• Same precedence group
• Associativity Left  Right
Equality Operators
Operator Purpose
== Check if two operands are equal
!= Check if two operands are not equal
• Same precedence group
• Associativity Left  Right
The resulting value of both relational and equality operators is
either TRUE(integer 1) or FALSE(integer 0)
Examples
Expression(i=1,j=2,k=3) Interpretation Value
i < j true 1
(i + j) >= k true 1
(j + k) > (i + 5) false 0
k != 3 false 0
j == 2 true 1
Code Sample
Logical Operators
Operator Purpose
&& Logical AND
|| Logical OR
!
(unary)
Logical NOT
• && has higher precedence than ||
• Associativity Left  Right
Examples
Expression(i=7,f=5.5,c=‘w’) Interpretation Value
(i>=6) && (c==‘w’) true 1
(i>=6) || (c==119) true 1
(f<11) && (i>100) false 0
(c!=‘p’) || ( (i+f) <= 10) true 1
! (i<=7) false 0
Code Sample
Assignment Operators
Operator Purpose
= Assigns values from the right side operands to left side operand
+= a += 5 is same as a=a+5
-= a += 5 is same as a=a-5
*= a += 5 is same as a=a*5
/= a += 5 is same as a=a/5
%= a += 5 is same as a=a%5
Assignment Operators
• Same precedence group
• Associativity Right  Left
• Example:
i = j = 5;
first assigns 5 to variable j, then assigns the value of j to i
Code Sample
Operator Purpose
~
(unary)
One’s compliment operator
(reverse each bits of operands i.e. 0  1 and 1  0)
& Bitwise AND
(perform and operations on each bits)
| Bitwise OR
(perform or operation on each bits)
^ Bitwise exclusive OR (XOR)
<< shift left
>> shift right
Bitwise Operators
• Associativity Left  Right
• Precedence : shift > and > xor > or
Truth table for bitwise & , | , ^
a b a & b a | b a ^ b
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Examples
a = 0x6db7 = 0110 1101 1011 0111
~a = 0x9248 = 1001 0010 0100 1000
b = 0xa726 = 1010 0111 0010 0110
~b = 0x58d9 = 0101 1000 1101 1001
a = 0x6db7 = 0110 1101 1011 0111
b = 0xa726 = 1010 0111 0010 0110
a & b = 0xca91 = 0010 0101 0010 0110
Examples
int a = 0x6db7 ; // 0110 1101 1011 0111
b = a << 6 ; // 0110 1101 1100 0000
int a = 0x6db7 ; // 0110 1101 1011 0111
b = a >> 6 ; // ???? ???? ???? ????
Code Sample
Bitwise Assignment Operators
Operator Purpose
&= a &= 0x7f equivalents to a= a & 0x7f
^= a ^= 0x7f equivalents to a= a ^ 0x7f
|= a |= 0x7f equivalents to a= a | 0x7f
<<= a <<= 5 equivalents to a= a << 5
>>= a >>= 5 equivalents to a= a >> 5
• Associativity Right  Left
• Same precedence group
Conditional Operators
Operator
expression 1 ? expression 2 : expression 3
When evaluating expression, expression 1 is evaluated first.
If expression 1 is true, then expression 2 is evaluated and this
becomes the value of the conditional expression.
if expression 1 is false, then expression 3 is evaluated and this
becomes the value of the conditional expression.
Code Sample
• Write a program to find out whether a number is
even or odd.
Special operators
Operator Description
& Returns the address of an variable
Ex. Printf(“%#X n”, &x);
* Points to a memory location of specific data type
Ex. int *x;
, int a, b=10,c;
C operators precedence table
Category Operator Associativity
Postfix () [] -> . ++ -- L to R
Unary + - ! ~ ++ -- (type) * & sizeof() R to L
Multiplicative * / % L to R
Additive + - L to R
Shift << >> L to R
Relational < <= > >= L to R
Equality == != L to R
Bitwise AND & L to R
Bitwise XOR ^ L to R
Bitwise OR | L to R
Logical AND && L to R
Logical OR || L to R
Conditional ? : R to L
Assignment = += -= *= /= %= >>= <<= &= ^= |= R to L
Comma , L to R
References
• Schaum’s outlines “Programming with C” by Byron
Gottfried (3rd edition), Chapter 3 (full)
• https://www.programiz.com/c-programming/c-
operators
• https://www.tutorialspoint.com/cprogramming/c_oper
ators.htm

More Related Content

What's hot

Python Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.inPython Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.in
Learnbayin
 
CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++
Pranav Ghildiyal
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
bajiajugal
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
vishaljot_kaur
 

What's hot (19)

itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in java
 
Expression and Operartor In C Programming
Expression and Operartor In C Programming Expression and Operartor In C Programming
Expression and Operartor In C Programming
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
Java 2
Java 2Java 2
Java 2
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
 
Python Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.inPython Training in Bangalore | Python Operators | Learnbay.in
Python Training in Bangalore | Python Operators | Learnbay.in
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
Operator 04 (js)
Operator 04 (js)Operator 04 (js)
Operator 04 (js)
 
CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++CBSE Class XI :- Operators in C++
CBSE Class XI :- Operators in C++
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
Python operators
Python operatorsPython operators
Python operators
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
 

Similar to SPL 6 | Operators in C

Similar to SPL 6 | Operators in C (20)

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
 
OPERATORS OF C++
OPERATORS OF C++OPERATORS OF C++
OPERATORS OF C++
 
Types of Operators in C
Types of Operators in CTypes of Operators in C
Types of Operators in C
 
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
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
 
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/C++
Operators in C/C++Operators in C/C++
Operators in C/C++
 
02-01-0-Operators and Operands.pdf
02-01-0-Operators and Operands.pdf02-01-0-Operators and Operands.pdf
02-01-0-Operators and Operands.pdf
 
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
 
Java - Operators
Java - OperatorsJava - Operators
Java - Operators
 
C# operators
C# operatorsC# operators
C# operators
 
Opeartor &amp; expression
Opeartor &amp; expressionOpeartor &amp; expression
Opeartor &amp; expression
 
Coper in C
Coper in CCoper in C
Coper in C
 
c programming2.pptx
c programming2.pptxc programming2.pptx
c programming2.pptx
 
Chap 3(operator expression)
Chap 3(operator expression)Chap 3(operator expression)
Chap 3(operator expression)
 
Operators in java script
Operators   in  java scriptOperators   in  java script
Operators in java script
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 

More from Mohammad Imam Hossain

More from Mohammad Imam Hossain (20)

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 

SPL 6 | Operators in C

  • 1. Structured Programming Language Operators in C Mohammad Imam Hossain, Lecturer, CSE, UIU
  • 2. Operator An operator is a program element that is applied to one or more operands in an expression or statement. • Operands can be a value or a variable. • Operators that take • one operand  Unary operators. • two operands  Binary operators. • three operands  Ternary operators.  Example: 5 + 7 Or, a + b where a , b are two variables.
  • 4. C Arithmetic Operators Operator Purpose + Adds two operands - Subtracts second operand from the first * Multiplies two operands / Divides numerator by denominator % Remainder after integer division
  • 5. Examples a= 10, b=3 result v1= 12.5, v2 = 2.0 result c1=‘P’ c2 = ‘T’ result a+b 13 v1+v2 14.5 c1 80 a-b 7 v1-v2 10.5 c1+c2 164 a*b 30 v1*v2 25.0 c1 + c2 +5 169 a/b 3 v1/v2 6.25 c1+c2+’5’ 217 a%b 1
  • 6.  If one or both operands represent negative values, then the addition, subtraction, multiplication and division operations will result in values whose signs are determined by the usual rules of algebra.  For remainder, most versions of C assign the sign of the first operand to the remainder to fulfill the next condition dividend = (integer quotient)*divisor + remainder i.e. a = ((a/b)*b)+(a%b) will always be satisfied.
  • 7. Values Results 11 % 3 2 11 % -3 2 -11 % 3 -2 -11 % -3 -2 Examples
  • 8. Type Conversion • The data type is promoted from lower to higher. char short unsigned short int unsigned int long unsigned long long long unsigned long long float double long double Example: int i=7; double d=5.5; char c=‘w’; Then the data type of (i+c)-(2*d/5) is double and the result is 123.8
  • 9. Type Casting • Explicit type conversion can be forced in any expression, with a unary operator called a cast. • Syntax: (type) expression • Example: int n; float x; x=(float) n; • The above statement will convert the value of n to a float value before assigning to x, but n is not altered. • Type casting doesn’t change the actual value of the variable but the resultant value may be put in temporary storage.
  • 10. Precedence(order of evaluation) • The operators within C are grouped hierarchically according to their precedence. • Operations with a higher precedence are carried out before operations having a lower precedence. • Example: 2 precedence groups for arithmetic operators. Group 1(higher precedence): *, /, % Group 2(lower precedence): +, -
  • 11. Associativity • The order in which consecutive operations within the same precedence group are carried out. • Example: each of the 2 precedence groups of arithmetic operators has associativity from Left  Right a – b / c * d
  • 12. Associativity • The order in which consecutive operations within the same precedence group are carried out. • Example: each of the 2 precedence groups of arithmetic operators has associativity from Left  Right ≡ a – [(b/c) * d]a – b / c * d
  • 14. Unary Operators • Operators that act upon a single operand to produce a new value • Same precedence group • Associativity Right  Left Operator Purpose Example - To negate a numerical constant, variable or expression -743, -0X7FFF,-0.2, -5E-8, -var1, -(x+y), -3*(x+y) ++ Pre/Post increment operator ++a, a++ -- Pre/Post decrement operator --a, a-- sizeof() Returns size of its operand in bytes sizeof(int) (type) Type casting (float) 5
  • 15. Code Sample c=a++; /// c=a; a=a+1; c=++a; /// a=a+1; c=1;
  • 16. Relational Operators Operator Purpose < Check if operand on the left is smaller than operand on the right <= Check if operand on the let is smaller than or equal to right operand > Check if operand on the left is greater than operand o the right >= Check left operand is greater than or equal to right operand • Same precedence group • Associativity Left  Right
  • 17. Equality Operators Operator Purpose == Check if two operands are equal != Check if two operands are not equal • Same precedence group • Associativity Left  Right
  • 18. Equality Operators Operator Purpose == Check if two operands are equal != Check if two operands are not equal • Same precedence group • Associativity Left  Right The resulting value of both relational and equality operators is either TRUE(integer 1) or FALSE(integer 0)
  • 19. Examples Expression(i=1,j=2,k=3) Interpretation Value i < j true 1 (i + j) >= k true 1 (j + k) > (i + 5) false 0 k != 3 false 0 j == 2 true 1
  • 21. Logical Operators Operator Purpose && Logical AND || Logical OR ! (unary) Logical NOT • && has higher precedence than || • Associativity Left  Right
  • 22. Examples Expression(i=7,f=5.5,c=‘w’) Interpretation Value (i>=6) && (c==‘w’) true 1 (i>=6) || (c==119) true 1 (f<11) && (i>100) false 0 (c!=‘p’) || ( (i+f) <= 10) true 1 ! (i<=7) false 0
  • 24. Assignment Operators Operator Purpose = Assigns values from the right side operands to left side operand += a += 5 is same as a=a+5 -= a += 5 is same as a=a-5 *= a += 5 is same as a=a*5 /= a += 5 is same as a=a/5 %= a += 5 is same as a=a%5
  • 25. Assignment Operators • Same precedence group • Associativity Right  Left • Example: i = j = 5; first assigns 5 to variable j, then assigns the value of j to i
  • 27. Operator Purpose ~ (unary) One’s compliment operator (reverse each bits of operands i.e. 0  1 and 1  0) & Bitwise AND (perform and operations on each bits) | Bitwise OR (perform or operation on each bits) ^ Bitwise exclusive OR (XOR) << shift left >> shift right Bitwise Operators • Associativity Left  Right • Precedence : shift > and > xor > or
  • 28. Truth table for bitwise & , | , ^ a b a & b a | b a ^ b 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0
  • 29. Examples a = 0x6db7 = 0110 1101 1011 0111 ~a = 0x9248 = 1001 0010 0100 1000 b = 0xa726 = 1010 0111 0010 0110 ~b = 0x58d9 = 0101 1000 1101 1001 a = 0x6db7 = 0110 1101 1011 0111 b = 0xa726 = 1010 0111 0010 0110 a & b = 0xca91 = 0010 0101 0010 0110
  • 30. Examples int a = 0x6db7 ; // 0110 1101 1011 0111 b = a << 6 ; // 0110 1101 1100 0000 int a = 0x6db7 ; // 0110 1101 1011 0111 b = a >> 6 ; // ???? ???? ???? ????
  • 32. Bitwise Assignment Operators Operator Purpose &= a &= 0x7f equivalents to a= a & 0x7f ^= a ^= 0x7f equivalents to a= a ^ 0x7f |= a |= 0x7f equivalents to a= a | 0x7f <<= a <<= 5 equivalents to a= a << 5 >>= a >>= 5 equivalents to a= a >> 5 • Associativity Right  Left • Same precedence group
  • 33. Conditional Operators Operator expression 1 ? expression 2 : expression 3 When evaluating expression, expression 1 is evaluated first. If expression 1 is true, then expression 2 is evaluated and this becomes the value of the conditional expression. if expression 1 is false, then expression 3 is evaluated and this becomes the value of the conditional expression.
  • 34. Code Sample • Write a program to find out whether a number is even or odd.
  • 35. Special operators Operator Description & Returns the address of an variable Ex. Printf(“%#X n”, &x); * Points to a memory location of specific data type Ex. int *x; , int a, b=10,c;
  • 36. C operators precedence table Category Operator Associativity Postfix () [] -> . ++ -- L to R Unary + - ! ~ ++ -- (type) * & sizeof() R to L Multiplicative * / % L to R Additive + - L to R Shift << >> L to R Relational < <= > >= L to R Equality == != L to R Bitwise AND & L to R Bitwise XOR ^ L to R Bitwise OR | L to R Logical AND && L to R Logical OR || L to R Conditional ? : R to L Assignment = += -= *= /= %= >>= <<= &= ^= |= R to L Comma , L to R
  • 37. References • Schaum’s outlines “Programming with C” by Byron Gottfried (3rd edition), Chapter 3 (full) • https://www.programiz.com/c-programming/c- operators • https://www.tutorialspoint.com/cprogramming/c_oper ators.htm