Operators are essential components in programming for logical and mathematical functions. Operators in C programming are categorised in following ways.
Table of Contents
Arithmetic Operators in C
Binary Arithmetic operators
These are of 5 types.
We are assuming a=8 and b=2.
- Addition: ‘ + ‘
a + b //Answer: 10
- Subtraction: ‘ – ‘
a - b //Answer: 6
- Multiplication: ‘ * ‘
a * b //Answer: 16
- Division: ‘ / ‘
a / b //Answer: 4
- Modulus : ‘ % ‘
a % b //Answer: 0 (Because reminder of 8/2 is 0)
Note: The division operator gives quotient, while modulus operator gives reminder.
Unary Arithmetic operators
These are of 2 types. These operators can be used before or after the operand.
- Increment Operator ++ (Plus-Plus)
- Decrement Operator – – (Minus-Minus)
It increments the value of given operand by 1.
a = 10; a++; //incremented value by 1, a is now 11 b=10; ++b; //incremented value by 1, b is now 11
It decrements the value of given operand by 1
a = 10; a--; //decremented value by 1, a is now 9 b = 10; --b; //decremented value by 1, b is now 9
The above examples have used the post and pre increment/decrement operators in same way for the basic understanding. However, there is significant difference as well in particular scenarios which shall be discussed later.
Assignment Operators in C
They assign the value to the identifier on the left of operator from literal, another identifier or expression on right of operator.
Assign: ‘ = ‘
a = 10; //assign value 10 to a b = a; //Assign value of a to b
Perform Operation and Assign: += -= *= /=
a = 5; //a has value 5 a += 10; //is same as (a = a + 10) i.e. a = 5 + 10 which assigns value 15 to a b = 6; //b has value 6 b -= 2; //is same as (b = b - 2) i.e. b = 6 - 2 which assigns value 4 to b
Relational Operators in C
These are also called comparison operators, used to compare the values of two operands i.e. they tell whether the condition is true (1) or false (0).
- Less than <
- Greater than >
- Less than equal to <=
- Greater than equal to >=
- Equal equal to ==
- Not equal to !=
a = 10; //assign value 10 to a a < 14; //is value of a less than 14 ? True. hence, result is 1 a >= 14; //is value of a greater than or equal to 14 ? False. hence, result is 0 a != 14; //is value of a not equal to 14 ? True. hence, result is 1 a == 14; //is value of a equal 14 ? False. hence, result is 0
Please Note that = is assignment operator, and == is comparison operator which checks whether values are equal or not.
Logical Operators in C
These operators are used to make a decision by result of two or more conditions (relations).
- AND & : Both conditions should be satisfied
- OR | : at least one condition should be satisfied
- Short Circuit AND &&
- Short Circuit OR ||
- NOT ! : Reverse the Output
a = 10; a < 14 & a > 8; //a is less than 14 and a is greater than 8, hence result is 1 (true) a < 14 & a > 12; //a is less than 14 and a is not greater than 12 , hence result is 0 (false)
b = 10; b < 14 | b > 12; //b is less than 14 or greater than 12, hence result is 1 (true) b > 14 | b < 8; //neither b is greater than 14, nor a is not less than 8 , hence result is 0 (false)
These operators gives same output as their normal versions but they execute quicker.
If AND operator finds false condition, it would not check the other condition since result would be false anyway. Similarly, if OR operator finds true condition, it won’t check other conditions.
a = 5; (! a > 10); //True. a > 10 is false, but not operator is making it true, hence result is 1;
This is a basic introduction about operators and their usage. These are essential elements to get started with coding. In practice, there are several more operators in C programming for specific purposes which are not discussed at this point to keep things simple.