Operators
Operators
OPERATORS IN C LANGUAGE
C language supports a rich set of built-in operators.
An operator is a symbol that tells the compiler to perform certain
mathematical or logical and relational manipulations.
Operators are used in program to manipulate data and variables.
Types of operators:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Unary operators (Increment /Decrement operators)
6. Conditional operators
7. Bitwise operators
8. Special operators
i. Arithmetic operators
Arithmetic operators can be applied to any integer and floating point
number.
C supports all the basic arithmetic operators. The following table shows
all the basic arithmetic operators.
Operator Description
+ adds two operands
- subtract second operands from first
* multiply two operand
divide numerator by denumerator, gives quotient as
/ result
divide numerator by denumerator, gives remainder as
% result
ii. Relational operators
Also known as a comparison operator is an operator that compares two
values.
Expressions that contain relational operators are called relational
expressions.
The following table shows all relation operators supported by C.
Operator Description
Check if two operand are equal, returns 1 if both operands are
== equal otherwise it returns 0
Check if two operand are not equal. returns 1 if both operands
!= are not equal otherwise it returns 0
> Check if operand on the left is greater than operand on the right
< Check operand on the left is smaller than right operand
>= check left operand is greater than or equal to right operand
<= Check if operand on left is smaller than or equal to right operand
(iv)Assignment Operators
Assignment operator is used to assign the right side value/variable
to the left side variable.
Assignment operator is responsible for assigning values to the
variables. While the equal sign (=) is the fundamental assignment
operator.
Operator Description Example
= assigns values from right side operands to left side a=b
operand
+= adds right operand to the left operand and assign the a+=b is same as
result to left a=a+b
-= subtracts right operand from the left operand and assign a-=b is same as
the result to left operand a=a-b
*= multiply left operand with the right operand and assign a*=b is same as
the result to left operand a=a*b
/= divides left operand with the right operand and assign a/=b is same as
the result to left operand a=a/b
%= calculate modulus using two operands and assign the a%=b is same as
result to left operand a=a%b
V Unary operators (Increment /Decrement operators)
Unary operators act on single operands. C language supports three
unary operators: unary minus, increment, and decrement operators.
- unary minus(if the number is negative it becomes positive
after applying the unary minus, similarly if the number is
positive it becomes negative after applying the unary minus.
++ a=a+1 (increment and assignment)
-- a=a-1 (decrement and assignment)
a++,a-- (post increment and post decrement)
++a,--a(pre increment and pre decrement)
Operator Description
++a It will increment the value, and then assign the value to the variable.
--a It will decrement the value, and then assign the value to the variable.
a++ It will assign the value to the variable, and then increment the value.
a-- It will assign the value to the variable, and then decrement the value.
vi. Conditional operator
It is also known as ternary operator and used to evaluate
conditional expression. expr1 ? expr2 : expr3
If expr1 Condition is true ? Then value expr2 : Otherwise value expr3
num2>num3?num2:num3
Comma operator
The comma operator in c takes two operands. It works by evaluating the
first and discarding its value, and then evaluates the second and returns the
value as the result of the expression.
int a=2,b=3,x=0;
x = (++a, b+=a);
now the value of x =6.
Certain operators have higher precedence than others; for example, the
multiplication operator has a higher precedence than the addition
operator.
Associativity indicates in what order operators of equal precedence in
an expression are applied.
In general
Comma operator has lowest precedence
Unary operators are having highest precedence
While solving expression, equal priority operators are handled on the
basis of FIFO.
S.no Operators type Operator Associativity
1 Parentheses ( ) [ ] . -> Left to Right
2 Post increment, Post decrement a++, a-- Right to left
3 Pre increment, Pre decrement ++a, --a Right to left
4 Logical NOT ! Right to Left
5 Mul, division, modulo division * / % Left to Right
6 Addition, subtract + - Left to Right
<<
7 Bitwise shift left, Bitwise shift right >> Left to right
8 Relational comparison > < <= >= Left to Right
Expressions :-
Expression is a sequence of operators and operands that specifies
computation of a value.
a=9-12/3+3*2-1
a=?
a=9-4+3*2-1
a=9-4+6-1
a=5+6-1
a=11-1
a=10
A simple program using Expressions:-
#include<stdio.h>
int main()
{
int num1=10,num2=20;
int result;
result=num1*5 +num2*5+num1*5;
printf(“result is %d”,result);
return 0;
O/P:-
result is 200