Professional Documents
Culture Documents
PL1 Lecture 4 Operators and Expressions (2)
PL1 Lecture 4 Operators and Expressions (2)
PL1 Lecture 4 Operators and Expressions (2)
List of topics,
Operators
Types of operators
Operator precedence
OPERATORS AND EXPRESSIONS
Operators: The symbols which are used to perform logical
and mathematical operations.
Expression: Operators, functions, constants and variables
are combined together to form expressions.
Example: A + B * 5 is a Expression, where, +, * are
operators, A, B are variables, 5 is constant and A + B * 5 is
an expression.
TYPES OF C OPERATORS
C language offers many types of operators.They are,
a.Arithmetic operators
b.Assignment operators
c.Relational operators
d.Logical operators
e.Bit wise operators
f. Conditional operators (ternary operators)
g.Increment/decrement operators
h.Special operators
TYPES OF C OPERATORS
Types of Operators Description
Conditional (ternary) Conditional operators return one value if condition is true and
operators returns another value is condition is false.
Increment/decrement These operators are used to either increase or decrease the value
operators of the variable by one.
Arithmetic
Operators/Operation Example
+ (Addition) A+B
– (Subtraction) A-B
* (multiplication) A*B
/ (Division) A/B
% (Modulus) A%B
ARITHMETIC OPERATOR
#include <stdio.h>
int main()
{
int a=40,b=20, add, sub, mul, div;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b; Addition of a, b is : 60
printf("Addition of a, b is : %d\n", add); Subtraction of a, b is : 20
printf("Subtraction of a, b is : %d\n", sub); Multiplication of a, b is : 800
printf("Multiplication of a, b is : %d\n", mul);
Division of a, b is : 2
printf("Division of a, b is : %d\n", div);
return 0;
}
ASSIGNMENT OPERATOR
Operators Example/Description
= sum = 10;
10 is assigned to variable sum
• Used to assign values to variables. += sum += 10;
• Two categories of assignment operators: This is same as sum = sum + 10
1.Simple assignment operator -= sum -= 10;
This is same as sum = sum – 10
( Example: = )
*= sum *= 10;
2.Compound assignment This is same as sum = sum * 10
operators sum /= 10;
/=
( Example: +=, -=, *=, /=, This is same as sum = sum / 10
%=, &=, ^= ) sum %= 10;
%=
This is same as sum = sum % 10
&= sum&=10;
This is same as sum = sum & 10
Bitwise AND
^= sum ^= 10;
This is same as sum = sum ^ 10
Bitwise XOR
ASSIGNMENT OPERATOR
#include <stdio.h>
# include <stdio.h> int main(){
int main() int num = 1;
{ num =* 10;
int Total=0,i; num =/ 10;
for(i=0;i<10;i++)
{ return 0;
Total+=i; // This is same }
as Total = Total+i
}
#include <stdio.h>
printf("Total = %d", Total);
int main(){
}
int num = 1;
num *= 10;
num /= 10;
Total = 45
return 0;
}
RELATIONAL OPERATOR
Relational operators are used to find the relation between two variables. i.e.
to compare the values of two variables
Operators Example/Description
> 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)
== x == y (x is equal to y)
!= x != y (x is not equal to y)
RELATIONAL OPERATOR
#include <stdio.h>
int main()
{
int m=40,n=20; What will be the output!?
if (m == n)
{
printf("m and n are m is greater than n
equal");
}
else if (m < n)
{
printf("m is less than n");
}
else if (m > n)
{
printf("m is greater than
n");
}
LOGICAL OPERATOR
• These operators are used to perform logical operations on the given expressions.
• There are 3 logical operators in C language.
Operators Example/Description
(x>5)&&(y<5)
&& (logical AND) It returns true when both conditions are true
(x>=10)||(y>=10)
|| (logical OR) It returns true when at-least one of the condition is true
!((x>5)&&(y<5))
! (logical NOT) It reverses the state of the operand “((x>5) && (y<5))”
If “((x>5) && (y<5))” is true, logical NOT operator makes it
false
LOGICAL OPERATOR
#include <stdio.h>
int main()
{
int m=40,n=20, o=20,p=30;
if (m>n && m !=0) && Operator : Both conditions are
{
printf("&& Operator : Both conditions are true\n");
true
} || Operator : Only one condition is
if (o>p || p!=20)
{ true
printf("|| Operator : Only one condition is true\n");
}
! Operator : Both conditions are true.
if (!(m>n && m !=0)) But status is inverted as false
{
printf("! O perator : Both conditions are true\n");
}
else
{
printf("! O perator : Both conditions are true.
But status is inverted as false");
}
}
LOGICAL OPERATOR
#include <stdio.h>
if(!((x<5)&&(y>25)))
{ printf("condition satisfied!");}
return 0;
}
TERNARY OPERATOR
Syntax : (C ondition? true_value: false_value);
Example : (A > 100 ? 0 : 1);
In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if
else conditional statements.
#include <stdio.h>
int main() x value is 1
{
int x=1, y ; y value is 2
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
}
INCREMENT AND DECREMENT O PERATOR
Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ; i––;
INCREMENT AND DECREMENT O PERATOR
Output: 1 2 3 4 5 6 7 8 9 Output: 20 19 18 17 16 15 14 13 12 11
PRE/POST INCREMENT AND DECREMENT
OPERATOR
Operator Operator/Description
Pre increment operator (+ value of i is incremented before assigning it to the
+i) variable i
Post increment operator (i+
value of i is incremented after assigning it to the
+)
variable i
Pre decrement operator
value of i is decremented before assigning it to the
(- –i)
variable i
Post decrement operator value of i is decremented after assigning it to
(i– -) variable i
PRE/POST INCREMENT AND DECREMENT
OPERATOR
Output: 1 2 3 4 Output: 1 2 3 4 5
PRE/POST INCREMENT AND DECREMENT
OPERATOR
Output: 9 8 7 6 Output: 9 8 7 6 5
Operator Description Associativity
() Parentheses (function call) (see Note 1) left-to-right
[] Brackets (array subscript)
. Member selection via object name
-> Member selection via pointer
++ -- Postfix increment/decrement (see Note 2) C operators in order of precedence
++ -- Prefix increment/decrement right-to-left
(highest to lowest).
+- Unary plus/minus
!~ Logical negation/bitwise complement
(type) Cast (convert value to temporary value of type)
* Dereference
Their associativity indicates in what
& Address (of operand)
sizeof Determine size in bytes on this implementation order operators of equal
* / % Multiplication/division/modulus left-to-right precedence in an expression are
+ - Addition/subtraction left-to-right
<< >> Bitwise shift left, Bitwise shift right left-to-right
applied.
< <= Relational less than/less than or equal to left-to-right
> >= Relational greater than/greater than or equal to
== != Relational is equal to/is not equal to left-to-right
& Bitwise AND left-to-right
^ Bitwise exclusive OR left-to-right Note 1:Parentheses are also used to group sub-expressions
| Bitwise inclusive OR left-to-right to force a different precedence; such parenthetical
expressions can be nested and are evaluated from inner
&& Logical AND left-to-right to outer.
|| Logical OR left-to-right
?: Ternary conditional right-to-left Note 2:Postfix increment/decrement have high precedence,
but the actual increment or decrement of the operand is
= Assignment right-to-left delayed (to be accomplished sometime before the statement
+= -= Addition/subtraction assignment completes execution). So, in the statement y
*= /= Multiplication/division assignment = x * z++; the current value of z is used to evaluate the
expression
%= &= Modulus/bitwise AND assignment
(i.e., z++ evaluates to z) and z only incremented after all else
^= |= Bitwise exclusive/inclusive OR assignment is done.
<<= >>= Bitwise shift left/right assignment
, Comma (separate expressions) left-to-right
OPERATOR PRECEDENCE
Output: 6 Output: 7