Operators in C
An expression consists of variables and constants separated by Operators in C. There are 7 types of Operators in C as listed below:
1. Arithmetic Operators
Arithmetic Operators are used to perform arithmetic operations.
| Operator | Meaning | Example | Result |
| + | addition | 4+2 | 6 |
| - | subtraction | 4-2 | 2 |
| * | multiplication | 4*2 | 8 |
| / | division | 4/2 | 2 |
| % | modulus | 4%2 | 0 |
2. Relational Operators
Relational Operators are used to compare the values of operands or expressions to produce a logical value. A logical is either true or false.
| Operator | Meaning | Example | Result |
| < | less than | 4<3 | false |
| > | greater than | 4>3 | true |
| <= | less than or equal to | 4<=3 | false |
| >= | greater than or equal to | 4>=3 | true |
| (==) | equal to | 4==3 | false |
| != | not equal to | 4!=3 | true |
3. Logical Operators
Logical Operators are used to connect more relational operations to form a complex expression that is called Logical Operators or Logical expressions. A value obtained by evaluating a logical expression is always logical either true or false.
| Operator | Meaning | Example | Result |
| && | logical and | (4<3)&&(4>3) | false |
| || | logical or | (4<2) || (4>3) | true |
| ! | logical not | ! (4<2) | true |
4. Increment and Decrement operators (++ and – –)
The increment operator (++) is used to increase the value of an integer or char variable by 1. The decrement operator (- -) is used to reduce the value of an integer or char variable by 1.
Example:
a = 25;
a++ or ++a will produce the result a = 26 a = 25;
a- - or - -a will produce the result a = 24
5. Assignment Operators
Assignment Operators are used to perform arithmetic operations while assigning a value to a variable.
6. Conditional Operators or Ternary Operators (?:)
Conditional Operators are used to check a condition and select a value depending on the value of the condition. Normally the selected value will be assigned to a variable that has the following form :
variable=(condition)?value1:value2;
Example :
big=(x>y)?x:y;
7. Bitwise Operators
Bitwise Operators are used to perform operations at a binary digit level. These operators are not commonly used and are used only in special applications where optimized use of storage is required.
| Operator | Meaning |
| << | Shifts the bits to left |
| >> | Shifts the bit to right |
| ~ | Bitwise inversion |
| & | Bitwise logical and |
| | | Bitwise logical or |
| ^ | Bitwise exclusive or |