Operators in C
Operators are used to perform operations on variables or constant data values. On the basis of number of operands, there are two types of operators:
- Unary Operator - works on one operand, e.g. a++.
- Binary Operator - works on two operands, e.g. a+b.
Note: You can add constant values to a variable.
Types of Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
We are going to discuss about these operators in detail.
Arithmetic Operators
They are used to do Arithmetic Operations on data.
| Operator | Name | Description | Code |
|---|---|---|---|
| + | Addition | Adds two data values. | int a = 10 + 20; |
| - | Subtraction | Subtracts two data values. | int a = 100 - 20; |
| * | Multiplication | Multiply two data values. | int a = 3 * 2; |
| / | Division | Divides two data values. | int a = 10 / 5; |
| % | Modulus | Return remainder when two data values are divided. | int a = 23 % 5; |
| ++ | Increment | Increment value of a variable by 1. | a++; |
| -- | Decrement | Decrement value of a variable by 1. | a--; |
- Post-increment - it adds value at the end of instruction.
- Pre-increment - it adds value at the start of instruction.
Assignment Operators
They are used to assign values to variables. int a =10;
| Operator | Description | Code |
|---|---|---|
| = | Assigning 10 to int type variable a. | int a = 10; |
| += | It is short form of "a=a+10". | a+=10; |
| -= | It is short form of "a=a-10". | a-=10; |
| *= | It is short form of "a=a*10". | a*=10; |
| /= | It is short form of "a=a/10". | a/=10; |
| %= | It is short form of "a=a%10". | a%=10; |
Note: You can use this notation for any binary operator. For example, x = x ^ y can be written x ^= y.
Comparison Operators
They are used to compare values and make decisions. They are most widely used in conditional statements. They return either true or false (Boolean), on the basis of which we proceed further.
For example, if 10 is greater than 5 then add a and b otherwise subtract a and b.
| Operator | Name | Description | Code |
|---|---|---|---|
| == | Double equal | Checks if a is equal to b. | a == b; |
| != | Not equal | Checks if a is not equal to b. | a != b; |
| < | Less than | Checks if a is less than b. | a < b |
| <= | Less than or equal to | Checks if a is less than or equal b. | a <= b |
| > | Greater than | Checks if a is greater than b. | a > b |
| >= | Greater than or equal to | Checks if a is greater than or equal b. | a >= b |
Logical Operators
Like comparison operators, they also return true (1) or false (0). There are three types of logical operators:
| Operator | Name | Description | Code |
|---|---|---|---|
| && | AND | (a > b) && (b > c) | It says that both conditions should be true. a should be greater than b and b should be greater than c. |
| || | OR | (a > b) || (b > c) | It says that only one conditions should be true. a should be greater than b or b should be greater than c. |
| ! | NOT | !(a > b) | Inverts the result. If a is greater than b, it will return false. |
Bitwise Operators
Bitwise operators perform operation on bit level. We deal with decimal values with base 10, but for using bitwise operators, computer convert base 10 to base 2 (binary format) and perform operations on bit and again convert then to base 10 and show us the output in decimal form.
| Operator | Name | Code | Description |
|---|---|---|---|
| & | bitwise AND | a & b | Binary of 2 is 10 and of 3 is 11 and their and will be 10 in binary and computer gives the answer 2. |
| | | bitwise OR | a | b | By taking bitwise OR of 10 and 11 we will get 11 in binary and output will be 3. |
| ~ | bitwise NOT | ~a (uniary operator) | Binary of a (a=2) is 10 and bitwise not will be 01 and output will be 1. |
| >> | Shift Right | a >> 2 | a=8; binary is 1000. a << 2 will be 10 and output will be 2. |
| << | Shift Left | a << 2 | a=8; binary is 1000. a >> 2 will be 100000 and output will be 32. |
Operator Precedence
If we have multiple operators in an expression, then compiler will have to decide which operator will be evaluated first and which will be evaluated in the last.
The order in which the arithmetic expression is evaluated is called the order of precedence. It is also known as hierarchy of operation.
When an arithmetic expression is evaluated, the computer performs only one operation at one time. In an expression in C the operations are performed in the following order
- All multiplications and divisions are performed first from left to right.
- All additions and subtractions are then performed from left to right.
- If the parenthesis are used in an expression, the expression within parenthesis are first computed from left to right.
- When parenthesis are used within parenthesis, the expression within innermost parenthesis is evaluated first.
Example of Operator Precedence
(4-(3*5))+2
- (3*5) is computed and returns value of 15.
- 4-15 is computed and then return a value of -11.
- -11+2 is computed and returns value of -9.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP
What will be the output of this code?
int a = 10, b = 3;
int result = a / b;
printf("%d", result);