You are on page 1of 12

C Operators

Arithmetic, Relational, Logical, Increment/Decrement and Precedence


of Operators
Operators and Expression
• Operators are special symbols used for
• Arithmetic calculations
• Assignment statements
• Logical comparisons
• Expression can be combination of variables, literals and operators that
result in value
• Operands are arguments
that operators perform Operand 14 + 5 – 4 * ( 5 – 3 ) Expression
operations on
Operator
Arithmetic Operators
• The arithmetic operators are all binary operators
• i.e. operators that take two operands
• Each operand can be either a literal , a variable identifier , or an
expression.
Operator Meaning Examples Operation Result Examples
+ Addition 1+2 = 3
Int/int int 2/2 = 1
- Subtraction 3 -2 = 1
Real/int real 7.0/2 = 3.5
* Multiplication 2*2 = 4
Int/real real 7/2.0 = 3.5
/ Division 2/2 = 1

Real/real real 7.0/2.0 = 3.5


% Modulo division 10/3= 1
Writing Mathematical formulas
• Always specify multiplication explicitly by using the operator * where
needed.
• Example: b² - 4 a c = b * b - 4 * a * c
• Use parentheses when required to control the order of operator
evaluation.
• Example: = (a+b)/(c+d)
• Two arithmetic operators can be written in succession if the second is
a unary operator
• Example: a x - ( b + c ) = a * - ( b + c )
Arithmetic Precedence
Assignment Operator
• The assignment operator assigns (=) the value on the right side of
operator to a variable on the left side of the operator
• Right side value may be from:
1. Literal: e.g. i = 1;
Assignment Operator
2. Variable identifier: e.g. start = i;
3. Expression: e.g. sum = first + second;
leftSide = rightSide ;

It is always a variable It is either a literal , a variable


identifier. identifier , or an expression.
Compound Assignment Operator
• When we want to modify the value of a variable by performing an
operation on the value currently stored in that variable we can use
compound assignment operators

Syntax:
It is either a literal , a
leftSide Op= rightSide ; variable identifier , or
an expression.

Always it is a variable
identifier.
It is an operator.

Above is equivalent to:


leftSide = leftSide Op rightSide ;
Compound Assignment Examples
Increment / Decrement Operators
Sample Explanation
• ++ and -- are a unary operators that can be Expression
applied to variables to increment/decrement
the value they hold. ++ ++x Increment x by 1, then use the
new value of x in the expression
• x++/++x; is equivalent to x = x+1; in which x resides
• x--/--x; is equivalent to x = x-1; ++ x++ Use the current value of x in the
expression in which x resides and
• Pre-Increment/Decrement(++x/--x) then increment x value by 1
• Placing the increment/ decrement operator
before the variable name -- --x Decrement x by 1, then use the
new value of x in the expression
• Post-Increment/Decrement(x++/x--) in which x resides
• Placing the increment/ decrement operator -- x-- Use the current value of x in the
after the variable name expression in which x resides and
then decrement x value by 1
Relational and Equality Operators
• In order to evaluate a comparison between two expressions we can use the relational
and equality operators
• Each operand can be either a literal , a variable identifier , or an expression.
• The result of a equality and relational operation is a Boolean value that can only be true
or false.
Algebraic equality or C++ Equality or relational Example of C++ Meaning of the Condition
Relational Operator Operator Condition
== x == y x is equal to y
!= x != y x is not equal to y
> x>y x is greater than y
< x<y x is less than y
>= x >= y x is greater than or equal to y
<= x <= y x is less than or equal to y
Relational Operators Examples
2 == 4 Evaluates to false
5>2 Evaluate to true
3 != 4 Evaluate to true
8 >= 15 Evaluates to false
6 < 12 Evaluate to true
34 <= 150 Evaluates to true

Instead of using only numeric constants, we can use any valid


expression, including variables. Suppose that a=2,b=3 and c=6
Operator Precedence and Associativity
Precedence and Associativity of C Operators
Symbol Type of Operation Associativity
[ ] ( ) . –> postfix ++ and postfix –– Expression Left to right

prefix ++ and prefix –– sizeof & * + – ~ ! Unary Right to left

typecasts Unary Right to left


*/% Multiplicative Left to right
+– Additive Left to right
< > <= >= Relational Left to right
== != Equality Left to right
= *= /= %=
+= –= <<= >>= &= Simple and compound assignment Right to left
^= |=
, Sequential evaluation Left to right

You might also like