Operators in C
Overview
C Supports a rich set of built-in Operators. Operators are symbols that are used to perform some operation or a set of operations on a variable or a set of variables. C has a set of operators to perform specific mathematical and logical computations on operands.
Introduction to Operators in C Language
operators in C are that symbols which work on operands. Operator in C language is used to perform specific mathematical or logical computations on the operands and it reduces a single value.
Operators in C language, are classified into several categories.
- Arithmetic Operators
- Relational Operators
- Shift Operators
- Logical Operators
- Bitwise Operators
- Ternary or Conditional Operators
- Assignment Operators
- Misc Operators
- Special Operators
Arithmetic Operators
An arithmetic operator is used to perform arithmetic/mathematical operations on operands. Some of the arithmetic operators are (+, -, *, /, %, ++, --)
Operator | Name of Operator | What it does | How it is used |
---|---|---|---|
+ | Unary Plus | Add two Operands | a+b |
- | Unary Minus | Subtracts the second operand from the first. | a-b |
* | Multiplication | Multiplies both operands. | a*b |
/ | Division | Divides numerator by de-numerator. | a/b |
% | Modulus | return remainder, after an integer division. | a%b |
++ | Increment Operator | increases the integer value by one. | a++ |
- - | Decrement Operator | decreases the integer value by one. | a- - |
Relational Operators
Relational operators help in making a relationship or comparison between two operands with which they are used. Hence, relational operators help us make decisions in the program and their end result is either true or false. Some of the relation operators are (==, !=, <, >, <=, >=)
Example:
The expression given above, we used an equality operator that means it checks the value of a and b if both values are the same then it will return true otherwise it will return false.
Operator | Name of Operator | What it does | Return value |
---|---|---|---|
== | Equality Operator | checks if a == b | Boolean |
!= | Not equal to | checks if a != b | Boolean |
< | Less than | checks if a < b | Boolean |
> | Greater than | checks if a > b | Boolean |
<= | Less than or equal to | checks if a<=b | Boolean |
>= | Greater than or equal to | checks if a>=b | Boolean |
Shift Operators
The Shift Operators is used when we want to shift a binary bit either in the left direction or right direction.
Shift Operators are classified into two categories C Language:
- Left Shift Operator: Left Shift Operator performs operations on the binary bits. The left shift operator is a type of binary operator so we need two operands to shift the position of the bits to the left side and add zeroes to the empty space on the right side after shifting the bits.
Syntax:
The output of the left shift operator, will be equivalent to multiplying varName with 2 ^ no_of_position (2 raised to power no_of_position)
Example:
- Right Shift Operator: Right Shift Operator performs operations on the binary bits. The Right shift operator is a type of binary operator so we need two operands to shift the position of the bits to the right side and add zeroes to the empty space on the left side after shifting the bits.
Syntax:
The output of the right shift operator, will be equivalent to dividing varName with 2^no_of_position (2 raised to power no_of_position)
Example:
Operator | Name of the Operator | What it does | How it is used |
---|---|---|---|
<< | Left Shift Operator | shifts the number of bits to the left side | a << 1 |
>> | Right Shift Operator | shifts the number of bits to the right side | a >> 2 |
Logical Operators
The logical operators are used when we want to check or test more than one condition and make decisions. Some of the logical operators are(&&, ||, !).
Example:
The logical expression given above is true only if a > b is true and x == 100 is true. if either (or both) of them are false, the expression is false.
Operator | Name of the operator | What it does | How it is used/output |
---|---|---|---|
&& | logical AND | returns true if both side operands value is true otherwise returns false | Boolean |
|| | logical OR | returns true if one of the operand's value is true or both of the operand's values is true otherwise returns false | Boolean |
! | logical Not | returns true if the condition in consideration is not satisfied Otherwise returns false | Boolean |
Bitwise Operators
An Bitwise operator is used for the manipulation of data at the bit level. These operators are not applied for the float and double datatype.
Bitwise operator first converts the integer into its binary representation then performs its operation. Bitwise operators subsist of two digits, either 0 or 1. Some of the bitwise operators are (&, | , ^, ~)
Note: Shift Bitwise operators are used to shift the bits right to left. Some of the shift bitwise operators are(<<, >>)
We use the following truth table for the Bitwise Operators:
A | B | A & B (Bitwise AND) | A | B (Bitwise OR) | A ^ B (Bitwise XoR) |
---|---|---|---|---|
1 | 1 | 1 | 1 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
0 | 0 | 0 | 0 | 0 |
Example:
In the above example, We have two variables a and b.
a = 5 (In Decimal);
b = 6 (In Decimal);
So, a's binary representation is 0101(5) and b's binary representation is 0110(6)
Operator | Name of Operator | What it does | How it is used |
---|---|---|---|
& | bitwise AND | bitwise AND operator do AND of every corresponding bits of both operands and output 1 (true) if both operands have 1 at that position otherwise 0(false). | a & b |
| | bitwise OR | bitwise OR operator do OR operation of every corresponding bits of both operands and output 0 (false) if both operands have 0 at that position otherwise 1(true). | a | b |
~ | bitwise complement | performs complement operation on an operand and bitwise complement changes 1 to 0 and 0 to 1 | ~a |
^ | bitwise exclusive OR | returns 1 if the corresponding bits of two operands are opposite else 0 | a^b |
<< | shift left | shifts the number of bits to the left side | a << 1 |
>> | shift right | shifts the number of bits to the right side | a >> 1 |
Ternary or Conditional Operators
the ternary or conditional operators is used to construct the conditional expression. A conditional operator pair "?:"
Syntax:
Here exp1, exp2, exp3 are expressions.
The Operator ?: works as follows: exp1 is evaluated first. If it is true, then the expression exp2 is evaluated and becomes the value of the expression. If exp1 is false, then exp3 is evaluated and its value becomes the value of the expression.
Example:
In the above example, we have two variables a and b. x, will be assigned the value of b because a>b is false.
Misc Operators
Misc Operator is type of Operator in C language. It is also called Miscellaneous Operator. Some of the Misc operators are (sizeof() ?: , & * )
Example:
Operator | Name of Operator | What it does | How it is used |
---|---|---|---|
sizeof() | sizeof | It returns the size of variable | if variable a is a interger variable the sizeof(a) will return 4 |
?: | conditional or ternary operator | if the condition is true then it returns the value of x else value of y | condition?x |
cast | type cast | it converts one datatype to another datatype | int(5.260) would return 5 |
, | comma operator | Used to link the related expressions together | a = (1,2,3) would return 3 |
& | Address Operator | returns the address of the variable. | &a |
* | pointer operator | pointer to a variable | *a |
Assignment Operators
An assignment operator is used to assign values to the operands. Some of the assignment operators are (=, +=, -=, *=, /=, %=)
Eample:
In the above example, we are assigning b's value to variable a.
Operator | Name of Operator | What it does | How it is used |
---|---|---|---|
= | assignment | assign value of variable b to variable a | a = b |
+= | plus assign | a = a+b (adds values of a to b and assign this value to a) | a += b |
-= | minus assign | a = a-b (subtracts values of b from a and assign this value to a) | a -= b |
*= | times assign | a = a*b (Multiplies a with b and assign the value to a) | a *= b |
/= | div assign | a = a/b (divides a by b and assigns the value to a) | a /= b |
%= | Mod assign | a = a%b (divides a by b and assigns the value of the remainder to a) | a %= b |
Special Operators
C supports some special operators some of the special operators are (comma operator, address operator, size of operator, pointer operator)
Example:
Operator | Name of Operator | What it does | How it is used |
---|---|---|---|
, | Comma | Used to link the related expressions together | value = (x=10, y=5) |
& | Address Operator | returns the address of the variable. | &a |
sizeof() | sizeof | returns the size of a variable | m = sizeof(a) |
Let's Understand the sizeof() operator with the help of program
Output:
Note: sizeof() may give different output according to the compiler.
Precedence(or priority) and Associativity of Operators in C
Precedence determines which operator is performed first in an expression if there are more than one operator of different precedence(lower precedence means higher priority). Associativity determines in which direction we should start computing the operators having the same precedence. The table shows the precedence and their associativity among operators.
Token | Operator | Precedence | Associativity |
---|---|---|---|
() | function call | 1 | left-to-right |
[] | array element | 1 | left-to-right |
++ | postfix increament | 1 | left-to-right |
- - | postfix decreament | 1 | left-to-right |
++ | prefix increament | 2 | right-to-left |
- - | prefix decreament | 2 | right-to-left |
+ | unary plus | 2 | right-to-left |
- | unary minus | 2 | right-to-left |
! | Logical negation | 2 | right-to-left |
~ | one's complement | 2 | right-to-left |
* | indirection | 2 | right-to-left |
& | address | 2 | right-to-left |
sizeof | size(in bytes) | 2 | right-to-left |
(type) | type cast | 2 | right-to-left |
* | multiplication | 3 | left-to-right |
/ | division | 3 | left-to-right |
% | modulus | 3 | left-to-right |
+ | addition | 4 | left-to-right |
- | subtraction | 4 | left-to-right |
<< | left shift | 5 | left-to-right |
>> | right shift | 5 | left-to-right |
< | less than | 6 | left-to-right |
<= | less than or equal to | 6 | left-to-right |
> | greater than | 6 | left-to-right |
>= | greater than or equal to | 6 | left-to-right |
== | equality | 7 | left-to-right |
!= | inequality | 7 | left-to-right |
& | bitwise AND | 8 | left-to-right |
^ | bitwise XOR | 9 | left-to-right |
| | bitwise OR | 10 | left-to-right |
&& | Logical AND | 11 | left-to-right |
|| | Logical OR | 12 | left-to-right |
?: | conditional expression | 13 | right-to-left |
= *= /= %= += -= &= ^= |= <<= >>= | assignment operators | 14 | right-to-left |
, | comma operator | 15 | left-to-right |
Let's understand the precedence(or priority) operator with the help of programming example.
Example 1:
Output:
explanation: priority or precedence for the values assigned to any variable is given from left to right.
Example 2:
Output:
explanation: priority or precendence for the values indside brackets () assigned to any variable is given from right to left.
Conclusion
- We perform operations on variables or operands by using operators in C
- An expression is the combination of operands and operators that reduces a single value.
- Arithmetic operators in C are used for mathmatical operations and include (+, -, *, /, %, ++, --).
- Relational operators in C are used to compare 2 variables and include (==, !=, <, >, <=, >=).
- Shift operators in C are used to shift bits of a number and include (<<, >>).
- To check more than one condition we use logical operators and they include (&&, ||, !).
- Bitwise operator are used to manipulate bits of a number and include (&, | , ^, ~).
- Ternary operator (?) are used as a short-hand to write if-else condition.
- Miscellaneous operator include (sizeof() ?: , & * ).
- To assign values we use assignment operators in C and they include (=, +=, -=, *=, /=, %=).
- We also understood the priority of operators in C.