Operators are used to performing operations on variables and values.
In C language, There are the following types of operators:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
Arithmetic Operators
Arithmetic Operators are used to doing mathematical calculations.
Operator | Name | Meaning | Example |
+ | Addition | Adds 2 values | x+y |
– | Subtraction | Substracts one value from another value | x-y |
* | Multiplication | Multiplies two values | x*y |
/ | Division | Divides one value by another value | x/y |
% | Modulus | Returns the remainder of the division | x%y |
++ | Increment | Increases the value of a variable by 1 | x++ |
— | Decrement | Decreases the value of a variable by 1 | x– |
Assignment Operators
Assignment Operators are used to assigning the value to a variable.
Operator | Example | Same as | Meaning |
= | x=2 | x=2 | Assigning 2 to x variable |
+= | x += 2 | x = x+2 | Adding 2 to x variable’s value and assigning value to x |
-= | x -= 2 | x = x-2 | Subtracting 2 to x variable’s value and assigning value to x |
*= | x *= 2 | x = x*2 | Multiplying 2 to x variable’s value and assigning value to x |
/= | x /= 2 | x = x/2 | Dividing 2 to x variable’s value and assigning value to x |
%= | x %= 2 | x = x%2 | Assigning division remainder to variable x |
&= | x &= 2 | x = x&2 | |
|= | x |= 2 | x = x | 2 | |
^= | x ^= 2 | x = x^2 | |
>>= | x >>= 2 | x = x >> 2 | |
<<= | x <<= 2 | x = x << 2 |
Comparison Operators
Comparison operators are used to comparing two values and return true or false. Its the comparison is right then it will be true otherwise false.
Operator | Name | Example |
== | is equal to | a==b |
!= | is not equal to | a!=b |
> | is greater than | a>b |
< | is less than | a<b |
>= | is greater than and equal to | a>=b |
<= | is less than and equal to | a<=b |
Logical Operators
Logical operators are used to checking if it is true or false.
Operator | Name | Meaning | Example |
&& | Logical and | It returns true if both statements are true otherwise returns false. | a > 2 && b < 3 |
|| | Logical or | It returns true if either 1st statement or 2nd statement is true, otherwise returns false. | a > 2 || b < 3 |
! | Logical not | It reverses the false to true and true to false. | !(a > 2 || b < 3) |
Size of Operator
The sizeof operator returns the size of a variable in bytes.
Example:
int myInt = 1;
printf("%d", sizeof(myInt));
Bitwise Operators
Operators | Name | Example |
& | Bitwise AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise XOR | a ^ b |
~ | Bitwise complement | ~ b |
<< | Shift left | a<<1 |
>> | Shift right | a>>1 |