![]() |
VOOZH | about |
Operators are the basic components of C programming. They are symbols that represent some kind of operation, such as mathematical, relational, bitwise, conditional, or logical computations, which are to be performed on values or variables. The values and variables used with operators are called operands.
30
On the basis of the number of operands they work on, operators can be classified into three types :
C language provides a wide range of built in operators that can be classified into 6 types based on their functionality:
The arithmetic operators are used to perform arithmetic/mathematical operations on operands. In C, these include:
a + b = 30 a - b = 20 a * b = 125 a / b = 5 a % b = 0 +a = 25 -a = -25 a++ = 25 a-- = 26
The relational operators in C are used for the comparison of the two operands. All these operators are binary operators that return true or false values as the result of comparison.
a < b : 0 a > b : 1 a <= b: 0 a >= b: 1 a == b: 0 a != b : 1
Here, 0 means false and 1 means true.
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.
Symbol | Operator | Description | Syntax |
|---|---|---|---|
&& | Logical AND | Returns true if both the operands are true. | a && b |
|| | Logical OR | Returns true if both or any of the operand is true. | a || b |
! | Logical NOT | Returns true if the operand is false. | !a |
a && b : 1 a || b : 1 !a: 0
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.
Note: Mathematical operations such as addition, subtraction, multiplication, etc. can be performed at the bit level for faster processing.
Symbol | Operator | Description | Syntax |
|---|---|---|---|
& | Bitwise AND | Performs bit-by-bit AND operation and returns the result. | a & b |
| | Bitwise OR | Performs bit-by-bit OR operation and returns the result. | a | b |
^ | Bitwise XOR | Performs bit-by-bit XOR operation and returns the result. | a ^ b |
~ | Bitwise First Complement | Flips all the set and unset bits on the number. | ~a |
<< | Bitwise Leftshift | Shifts bits to the left by a given number of positions; multiplies the number by 2 for each shift. | a << b |
>> | Bitwise Rightshift | Shifts bits to the right by a given number of positions; divides the number by 2 for each shift. | a >> b |
a & b: 1 a | b: 29 a ^ b: 28 ~a: -26 a >> b: 0 a << b: 800
Assignment operators are used to assign 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.
The assignment operators can be combined with some other operators in C to provide multiple operations using single operator. These operators are called compound operators.
Symbol | Operator | Description | Syntax |
|---|---|---|---|
= | Simple Assignment | Assign the value of the right operand to the left operand. | a = b |
+= | Plus and assign | Add the right operand and left operand and assign this value to the left operand. | a += b |
-= | Minus and assign | Subtract the right operand and left operand and assign this value to the left operand. | a -= b |
*= | Multiply and assign | Multiply the right operand and left operand and assign this value to the left operand. | a *= b |
/= | Divide and assign | Divide the left operand with the right operand and assign this value to the left operand. | a /= b |
%= | Modulus and assign | Assign the remainder in the division of left operand with the right operand to the left operand. | a %= b |
&= | AND and assign | Performs bitwise AND and assigns this value to the left operand. | a &= b |
|= | OR and assign | Performs bitwise OR and assigns this value to the left operand. | a |= b |
^= | XOR and assign | Performs bitwise XOR and assigns this value to the left operand. | a ^= b |
>>= | Rightshift and assign | Performs bitwise Rightshift and assign this value to the left operand. | a >>= b |
<<= | Leftshift and assign | Performs bitwise Leftshift and assign this value to the left operand. | a <<= b |
a = b: 5 a += b: 10 a -= b: 5 a *= b: 25 a /= b: 5 a %= b: 0 a &= b: 0 a |= b: 5 a ^= b: 0 a >>= b: 0 a <<= b: 0
Apart from the above operators, there are some other operators available in C used to perform some specific tasks. Some of them are discussed here:
Syntax
The comma operator (represented by the token) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type).
The comma operator has the lowest precedence of any C operator. It can act as both operator and separator.
Syntax
The conditional operator is the only ternary operator in C. It is a conditional operator that we can use in place of if..else statements.
Syntax
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then we will execute and return the result of Expression2 otherwise if the condition(Expression1) is false then we will execute and return the result of Expression3.
Member operators are used to reference individual members of classes, structures, and unions.
Syntax
Casting operators convert one data type to another. For example, (int)2.2000 would return 2.
Syntax
Addressof operator & returns the address of a variable and the dereference operator * is used to access the value stored at that address.
sizeof(num) = 4 bytes &num = 0x7ffdb58c037c *add_of_num = 10 (10 < 5) ? 10 : 20 = 20 (float)num = 10.000000
It is strongly recommended to learn Operator Precedence and Associativity after reading about operators.