Operators in C
Operators in C
c = a + b;
● Here, ‘+’ is the operator known as the addition operator and ‘a’ and ‘b’ are
operands. The addition operator tells the compiler to add both of the operands
‘a’ and ‘b’.
TYPES:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
1. Arithmetic Operators:
● These operators are used to perform arithmetic/mathematical operations on
operands.
a) Unary Operators:
● Operators that operate or work with a single operand are unary operators.
● For example: Increment(++) and Decrement(- - ) Operators
int val = 5;
++val; // 6
b) Binary Operators:
● Operators that operate or work with two operands are binary operators.
● For example: Addition(+), Subtraction(-), multiplication(*), Division(/)
operators
int a = 7;
int b = 2;
Example:
A=5 B=5
A + B =
+ Addition between 2 operands.
10
A * B =
* Multiplication between 2 operands.
25
int c= a+b; // 9
Example:
int main()
{
int a = 10,b = 15, result;
result = a+b;
printf("Addition: a+b = %d \n",result);
result = a-b;
printf("Subtraction: a-b = %d \n",result);
result = a*b;
printf("Multiplication: a*b = %d \n",result);
result = a/b;
printf("Division: a/b = %d \n",result);
result = a%b;
printf("Modulo Division: %d \n",result);
result = ++a;
printf("Increment the value of a by 1: %d \n",result);
result = --b;
printf("Decremented the value of b by 1: %d \n",result);
return 0;
}
Output:
Addition: a+b = 25
Subtraction: a-b = -5
Division: a/b = 0
Modulo Division: 10
2. Relational Operators:
● These are used for the comparison of the values of two operands.
● For example, checking if one operand is equal to the other operand or not, an
operand is greater than the other operand or not, etc.
int a = 3;
int b = 5;
a < b;
3. Logical Operators:
● Logical Operators are used to combine two or more conditions/constraints or
to complement the evaluation of the original condition in consideration.
● The result of the operation of a logical operator is a Boolean value
either true or false.
● For example, the logical AND represented as ‘&&’ operator in C returns
true when both the conditions under consideration are satisfied. Otherwise, it
returns false.
● Therefore, a && b returns true when both a and b are true (i.e. non-zero)
(4 != 5) && (4 < 5); // true
Operator Description
4. Bitwise Operators:
● The Bitwise operators are used to perform BIT-LEVEL OPERATIONS on
the operands.
● The operators are first converted to bit-level and then the calculation is
performed on the operands.
Operator Also known as
<< Binary Left Shift Operator
>> Binary Right Shift Operator
~ Binary Ones Complement Operator
& Binary AND Operator
^ Binary XOR Operator
| Binary OR Operator
● The mathematical operations such as addition, subtraction, multiplication, etc.
can be performed at bit-level for faster processing.
● For example, the bitwise AND represented as & operator in C takes two
numbers as operands and does AND on every bit of two numbers.
● The result of AND is 1 only if both bits are 1.
int a = 5, b = 9; // a = 5(00000101),
b = 9(00001001)
(a ^ b); // 00001100
(~a); // 11111010
5. Assignment Operators:
● Assignment operators are used to assigning value to a variable.
● The left side operand of the assignment operator is a variable and the right
side operand of the assignment operator is a value.
● The value on the right side must be of the same data type as the variable on
the left side otherwise the compiler will raise an error.
Operator Example
= a=b or b=a
+= a += b or a = a+b
-= a -=b or a = a-b
*= a *= b or a = a*b
/= a /= b or a = a/b
b. “+=”: This operator is combination of ‘+’ and ‘=’ operators. This operator first
adds the current value of the variable on left to the value on the right and then
assigns the result to the variable on the left.
For example:
(a += b) can be written as (a = a + b)
(a -= b) can be written as (a = a - b)
c. “-=”: This operator is a combination of ‘-‘ and ‘=’ operators. This operator first
subtracts the value on the right from the current value of the variable on left and
then assigns the result to the variable on the left.
For example:
(a -= b) can be written as (a = a - b)
d. “*=”: This operator is a combination of ‘*’ and ‘=’ operators. This operator first
multiplies the current value of the variable on left to the value on the right and then
assigns the result to the variable on the left.
For example:
(a *= b) can be written as (a = a * b)
e. “/=”: This operator is a combination of ‘/’ and ‘=’ operators. This operator first
divides the current value of the variable on left by the value on the right and then
assigns the result to the variable on the left.
For example:
(a /= b) can be written as (a = a / b)
6. Other Operators:
Apart from the above operators, there are some other operators available in C used
to perform some specific tasks.
a. sizeof operator:
c. Conditional Operator:
OPERATOR PRECEDENCE
● Operator precedence determines the grouping of terms in an expression and
decides how an expression is evaluated. Certain operators have higher
precedence than others;
● For example, the multiplication operator has a higher precedence than the
addition operator.
● The below table describes the precedence order and associativity of operators
in C. The precedence of the operator decreases from top to bottom.
* Dereference
12 || Logical OR left-to-right
= Assignment right-to-left
+= , -= Addition/subtraction assignment
*= , /= Multiplication/division assignment
Bitwise exclusive/inclusive OR
^= , |= assignment
#include <stdio.h>
main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5)
printf("Value of (a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );
return 0;
}
Output:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50