The symbols which are used to perform logical and mathematical operations in a C program are called C operators. C has a wide range of operators to perform various operations.
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).
The following table shows all the arithmetic operators supported by the C language. Assume variable A holds 15 and variable B holds 20 then −
Operator | Description | Example |
---|---|---|
+ | Adds two operands. | A + B = 35 |
− | Subtracts second operand from the first. | A − B = -5 |
* | Multiplies both operands. | A * B = 300 |
/ | Divides numerator by de-numerator. | B / A = 1 |
% | Modulus Operator and remainder of after an integer division. | B % A =5 |
++ | Increment operator increases the integer value by one. | A++ = 16 |
-- | Decrement operator decreases the integer value by one. | A-- = 14 |
Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program.
The following table shows all the relational operators supported by C. Assume variable A holds 15 and variable B holds 20 then −
Operator | Description | Example |
---|---|---|
== | Checks if the values of two operands are equal or not. If yes, then the condition becomes true. | (A == B) is not true. |
!= | Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. | (A != B) is true. |
> | Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. | (A > B) is not true. |
< | Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. | (A < B) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. | (A >= B) is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. | (A <= B) is true. |
Logical operators are used to perform logical operations on the given expressions. There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).
The following table shows all the logical operators supported by C. Assume variable A holds 15 and variable B holds 20 then −
Operator | Description | Example |
---|---|---|
&& | Logical AND Operator | (A > 15) && (B < 20) It returns true when both conditions are true |
|| | Logical OR Operator | (A >= 15)||(B < 20) It returns true when at-least one of the condition is true |
! | Logical NOT Operator | !((A > 15)&&(B < 20)) It reverses the state of the operand "(A > 15) && (B < 20)" If "(A > 15) && (B < 20)" is true, logical NOT operator makes it false |
Bitwise operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits.
These operators also perform shifting of bits from right to left and left to right
Now lets see truth table for bitwise &, | and ^
p | q | p & q | p | q | p ^ q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100
B = 0000 1101
-------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60 and variable 'B' holds 13, then −
Operator | Description | Example |
---|---|---|
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) = 12, i.e., 0000 1100 |
| | Binary OR Operator copies a bit if it exists in either operand. | (A | B) = 61, i.e., 0011 1101 |
^ | Binary XOR Operator copies the bit if it is set in one operand but not both. | (A ^ B) = 49, i.e., 0011 0001 |
~ | Binary One's Complement Operator is unary and has the effect of 'flipping' bits. | (~A ) = ~(60), i.e,. -0111101 |
<< | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | A << 2 = 240 i.e., 1111 0000 |
>> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 = 15 i.e., 0000 1111 |