Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

C Operators

Er. Yubraj Chaudhary


C Operators
O An operator is a symbol that operates on a certain
data type and produces the output as the result of
the operation.
O In C, you can combine various operators of similar
or different categories and perform an operation.
O In this case the C compiler tries to solve the
expression as per the rules of precedence.
Types of operator
On the basis of number of operands used with a operator,
operator are classified into three types;
O Unary Operators
O A unary operator is an operator, which operates on one
operand, i.e. it operates on itself.
O e.g. i++
O Binary
O A binary operator is an operator, which operates on two
operands
O e.g. a+b
O Ternary
O A ternary operator is an operator, which operates on three
operands.
O e.g. ?: | x>y?a:b
Types of operator
On the basis of use operators are classified as;
O Arithmetic operator
O Assignment operator
O Increment and decrement operator
O Relational operator
O Logical operator
O Conditional operator
O Comma operator
Arithmetic operator
O Used for arithmetic operations
Operator Description
+ Addition

- Subtraction

/ Division

* Multiplication

% Modulo or remainder
Assignment Operator
O An assignment operator (=) is used to assign a constant or a
value of one variable to another.
a = 5;
b = a;
interest_rate = 10.5;
result = (a/b) * 100;

O We can use the assignment operator to assign a value to


multiple variables.
a=b=c=10;
O When a variable on the left hand side of an assignment
operator also occurs on the right hand side then we can avoid
writing the variable twice by using compound assignment
operator.
x=x+5;
can also be written as,
x+=5;
Increment and Decrement
Operators
O increment operator (++) increments the value of the variable by 1
O decrement operator (- -) decrements the value of the variable by 1.
O ++x or x++ is x=x+1 (Postfix and prefix are same if used independently)
O if used in expression e.g. y=++x and y=x++ is different
O y=++x; means first increment the value of x by 1 and then assign the value of x
to y. i.e
x=5;
y=++x;
Hence now the value of x is 6 and value of y is 6.
O y=x++; means first the value of x is assigned to y and then x is incremented.
i.e.
x=5;
y=x++;
O Hence now the value of x is 6 and value of y is 5.
Relational Operators
O Relational operators are used to compare values of two
expressions depending on their relations.
O An expression that contains relational operators is called
relational expressions.
O If the relation is true then the value of relational expression is 1
O and if the relation is false then the value of the expression is 0.
Operator Meaning
< Less than
<= Less than or equal to
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
Logical Operators
O is used to compare or evaluate logical and relational
expressions.
O There are three logical operators available in the C
language.
Operator Meaning

&& Logical AND


|| Logical OR
! Logical NOT

Truth table for logical AND Truth table for logical OR


Condition1 Condition2 Result Condition1 Condition2 Result
1 1 1 1 1 1
1 0 0 1 0 1
0 1 0 0 1 1
0 0 0 0 0 0
Example for logical operators
O Let us take three variables a, b, and c and assume a =
5,b=10 and c=7
a>b &&a+b>c
Here a>b will return false hence 0 and a+b>c will return true
hence 1. Therefore, looking at the truth table 0 AND 1 will
be 0. From this we can understand, that this
expression will evaluate to false.
O Let us look at the same example using Logical OR (||)
a>b || a+b>c
Here a>b will return false hence 0 and a+b>c will return true
hence 1. Therefore, looking at the truth table 0 OR 1 will be
1. From this we can understand, that this expression
will evaluate to true.
The Logical NOT operator (!)
O The ! (Logical NOT) operator is a unary
operator.
O This operator inverses a logical result.
E.g. if a = 5 and b = 7, then
!(a == b)
O will evaluate to be true.
O This expression works as follows, a==b
evaluates to be false, now because of the !
(NOT) operator, the false is inverted to be
true.
Conditional Operator
O Conditional operator is a ternary operator (? And :) which requires
three expressions as operands.
O This is written as –
Test expression ?expression1 : expression2
O Firstly test expression is evaluated. If test expression is true,
expression1 is evaluated and it becomes the value of the overall
conditional expression. If test expression is false, expression2 is
evaluated and it becomes the value of the overall conditional
expression.
Example:
Suppose we are finding the greater number between two.
x=2;
y=5;
max = x > y ? x : y;
O This gives the result as max=5, because x>y is evaluated first. It is
clearly seen that x is less than y, so b is assigned to max.
O Can be used in place of if..else
Comma operator
O The simplest use of comma operator is to separate the
variables while declaration.
O The comma operator (,) is also used to permit different
expressions to appear in situations where only one
expression would be used.
O The expressions are separated by the comma operator.
The separated expressions are evaluated from left to
right and the rightmost expression is the type and value
of the compound expression.
Example:
sum = ( a=4, b=5, c=7, a+b+c);
will result sum=16.
Evaluate an expression
O if, int i , j=3, k=6
O i=j*2/3+k/4+6-j*j*j/8
O 3*2/3+6/4+6-3*3*3/8
O 6/3+6/4+6-3*3*3/8 [3*2=6]
O 2+6/4+6-3*3*3/8 [6/3=2]
O 2+1+6-3*3*3/8 [6/4=1]
O 2+1+6-9*3/8 [3*3=9]
O 2+1+6-27/8 [9*3=27]
O 2+1+6-3 [27/8=3]
O 3+6-3 [2+1=3]
O 9-3 [3+6=9]
O 6 [9-3=6]
Operator Description Precedence level Associativity
() Function call
[] Array subscript
→ 1 Left to Right
Arrow operator
. Dot operator
++ Increment
-- Decrement
2 Right to Left
! Logical NOT

Precedence ~
*
/
One’s complement
Multiplication
Division 3 Left to Right

and %
+
-
Modulus
Addition
Subtraction
4 Left to Right
<< Left shift
Associativity >>
<
Right shift
Less than
5 Left to Right

<= Less than or equal to


of operators >
>=
Greater than
Greater than or equal to
6 Left to Right

== Equal to
7 Left to Right
!= Not equal to
& Bitwise AND 8 Left to Right
^ Bitwise XOR 9 Left to Right
| Bitwise OR 10 Left to Right
&& Logical AND 11 Left to Right
|| Logical OR 12 Left to Right
?: Conditional operator 13 Right to Left
=
*=
/=
Assignment operators 14 Right to Left
%=
+=
-=
, Comma operator 15 Left to Right
Any Questions??

Thank you

You might also like