By Neeru Redhu
CCS HAU, Hisar
Operator is a symbol that tells the computer to
perform certain mathematical or logical
manipulations
Operators used in programs to manipulate data and
variables.
Usually form a part of mathematical or logical
expression
2
C operator can be classified into:
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators
Bitwise operators
Special operators
3
ARITHMETIC OPERATORS
Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo division
4
Integer Arithmetic
Operands integer (a, b)
From a integer expression (a+b or a-b etc)
Integer arithmetic
Real Arithmetic
Involves only real operands (variables declared as float)
Operator % cannot be used.
5
RELATIONAL OPERATORS
To compare two quantities relative to each other
Relational operators
Operator Meaning
== Is equal to
6
!= Is not equal to
A>B or a<b (relational expression)
Examples
4.5 <= 10 TRUE
4.5 < -10 FALSE
-35 >= 0 FALSE
10 < 7+5 TRUE
a+b = c+d TRUE/FALSE
7
RELATIONAL OPERATORS COMPLEMENTS
Actual Complement
> <=
< >=
== !=
8
Actual Simplified
! (x < y) x >= y
! (x > y) x <= y
! (x!=y) x == y
! (x<= y) x>y
! (x>= y) x<y
! (x == y) x != y
9
LOGICAL OPERATORS
&& -- AND
|| -- OR
! -- NOT
10
PRECEDENCE OF RELATIONAL AND LOGICAL OPERATORS
Highest !
> , >=, <, <=
= =, ! =
&&
Lowest ||
11
ASSIGNMENT OPERATORS
Used to assign result/value to a variable
a=3
b = -6
c=a+b
d = d + c etc
12
SHORTHAND ASSIGNMENT OPERATORS
Statement with simple Statement with
assignment operator shorthand operator
a=a+1 a += 1
a=a1 a=1
a=a*b a *= b
a=a/b a/=b
a=a%b a%=b
13
INCREMENT AND DECREMENT OPERATORS
++ and -- (+1 and 1 respectively)
c = 5; c = 5;
d = ++c; d = c++;
14
CONDITIONAL OPERATORS
uses ternary operator ( ? : )
if (a > b)
e. g. a = 10; x = a;
b = 15; else
x = b;
x = (a > b) ? a : b;
15
BITWISE OPERATOR
Operates only on integer
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
16
SPECIAL OPERATORS
Comma operator ( , )
Used to link related expression together
val = (a = 1, b = 2, x + y);
sizeof operator
Compile time operator
Returns the number of bytes the operand occupies
m = sizeof (sum);
18
Operator Description Associativity
left-to-right
() Parentheses (function call) (see Note 1)
[] Brackets (array subscript)
. Member selection via object name
-> Member selection via pointer
++ -- Postfix increment/decrement (see Note 2)
right-to-left
Prefix increment/decrement
++ -- Unary plus/minus
+- Logical negation/bitwise complement
!~ Cast (convert value to temporary value
(type) of type)
* Dereference
& Address (of operand)
sizeof Determine size in bytes on this
implementation
* / % Multiplication/division/modulus left-to-right
+ - Addition/subtraction left-to-right
left-to-right
<< >> Bitwise shift left, Bitwise shift right
left-to-right
Relational less than/less than or equal to 19
< <=
Relational greater than/greater than or
> >=
equal to
Operator Description Associativity
left-to-right
== != Relational is equal to/is not equal to
|| Logical OR left-to-right
right-to-left
= Assignment
+= -= Addition/subtraction assignment
*= /= Multiplication/division assignment
%= &= Modulus/bitwise AND assignment
^= |= Bitwise exclusive/inclusive OR assignment
<<= >>= Bitwise shift left/right assignment
20
left-to-right
, Comma (separate expressions)
MANAGING INPUT AND OUTPUT OPERATIONS
FORMATTED INPUT
21
22
23
COMMONLY USED SCANF CODES
Format Output
printf (%d,9876); 9876
printf (%6d,9876); _ _9876
printf (%2d,9876); 9876
printf (%-6d,9876); 9876_ _
printf (%06d,9876); 009876
25
Output of Real number
Format Output
printf (%7.4f,y) 98.7654
printf (%7.2f,y) _ _ 98.77
printf (%-7.2f,y) 98.77_ _
printf (%f,y) 98.7654
printf (10.2e,y) _ __ 9.88e+01
printf (%11.4e,-y) -9.8765e+01
printf (%-10.2e,y) 9.88e+01_ _
printf (%e,y) 9.876540e+01
26
END
27