OPERATORS
Operand ->
Operands are the values or variables on which operations are to be performed.
Operator ->
An Operator is a symbol that tells the computer to perform certain mathematical
or logical manipulation.
C supports a rich set of operators. They usually form a part of mathematical or
logical expressions. C operators can be classified into a number of categories:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment or Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
Arithmetic Operator –
Arithmetic operators +, -, *, / and %, all work the same way as they do in other
languages. They can operate on any built-in data type allowed in C.
Operator Meaning
+ Addition or Unary operator
- Subtraction or Unary operator
* Multiplication
/ Division
% Modulo Division
Integer division truncates any fractional part. The modulo division produces the
remainder of an integer division.
Ex:
a+b
a–b
a* b
a/b
a%b
-a * b
Relational Operator –
We can compare any two quantities using these relational operators depending on
their relations & take certain decisions. A simple relation expression contains only one
relational operator.
For ex: We may compare the marks of two students or the weight of two persons and so
on; these comparisons can be done with the help of relational operators. C supports six
relational operators in all:
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
Logical Operator –
Logical operators are used to combine two operands.
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
The logical operators && and || are used when we want to test more than one condition
and make decisions.
Ex:
a = = b && b = = c && c= = a
a = = b || b = = c || c = = a
a = = b && b! = c || c = = a
Assignment Operator –
Assignment operator is used to assign the results of an expression to an variable.
Operator Meaning
= Assign or Initialize a value
Ex:
b = 10;
c = a + b;
Increment and Decrement Operator –
These are the unary operators written as ++a or a++ and –b or b--. The first usage
is known as pre-increment and post-increment respectively where as second usage is
known as pre-decrement and post-decrement operators respectively. When used as pre-
increment and pre-decrement, the operators first increments or decrements the value of its
operand by 1. When used as post-increment or post-decrement, the operators uses the old
value of its operand in the expression & then increments or decrements its value by 1.
Note:
a ++ -> a = a + 1, similarly; --b -> b = b – 1
Operator Meaning
++ Increment
-- Decrement
Ex:
x = 20;
y = ++x;
In this case, the value of y and m would be 21.
And if,
y = x++;
Then the value of y would be 20 where as x would be 21.
Conditional Operator –
A ternary operator pair “? : ” is available in C to construct conditional
expressions of the form :
exp1? exp2:exp3;
where,
exp1, exp2 & exp3 are the expressions.
When exp1 is evaluated if it is true then exp2 is evaluated otherwise if it is false then
exp3 is evaluated.
Ex:
int num1 = 50, num2 = 30, big;
big = (num1 > num2) ? num1: num2;
printf(“Greater Number is : %d”, big);
Bitwise Operators –
These operators are used for manipulating data at bit level. These operators are
not applied to float or double.
Operator Meaning
~ One’s Complement
>> Right Shift
<< Left Shift
& Bitwise AND
| Bitwise OR
^ Bitwise EX-OR
Ex:
int a=10; int b=6; int c; 1010
c = a & b; 0110
printf(“%d”,c); 0010
Output: - 2ffffff
Special Operators –
The special operators are used for some special tasks, these operators are:
Operator Meaning
* Pointer Operator
& Address Operator
, Comma Operator
. or -> Member Selection Operator
sizeof () Size of Operator
typecast Type Cast Operator
[] Subscript Operator
• ‘*’ operator is also called “value at address operator”. It gives the value stored
data particular address. This operator is also called “indirection operator”.
• ‘&’ operator can be remembered as address of. It is used to determine the
address of any variable.
• ‘,’ operator is used to link the related expressions together.
• ‘.’ or ‘->’ operator is used to access the values of members of a structure or
union.
• sizeof() operator is a unary operator that gives the no. of bytes occupied by the
variable or the data type.
Ex: sizeof(x) => 2 -> int
1 –> char
4 -> float
8 -> double
Or sizeof(int).
• Typecasting means forcing the compiler to explicitly convert the value of an
expression to a particular datatype.
Ex:
float a;
int x=6, y=4;
a = (float) x/y;
printf(“a=%d”,a);
o/p : 1.5000