The C language has many built-in operators and provides various types of operators. A symbol that instructs the compiler to perform specific mathematical or logical functions is known as an operator. In the C programming language, operators are symbols that tell the compiler how to carry out particular operations, such as arithmetic, logical, relational, or bitwise operations. These operations are performed using mathematical operators in programming languages like C and C++. The different types of operators available in C include arithmetic operators, relational operators, logical operators, bitwise operators, and more. There are numerous operators available in the C programming language. We’ll examine the supported operators in C, the various types of operators, and operator precedence in this post.
Operators in C
Operators use one or more operands to perform an action. Operators are symbols that tell the computer how to perform operations with operands. Operations could be mathematical, logical, or relational in nature. The C programming language has a large number of operators.
Types of Operators in C
Operators are classified as follows based on their application.
- Arithmetic operators
- Relational operators
- Logical operators
- Unary operators
- Conditional operator
- Assignment operators
- Bitwise operators
- Other operators
Precedence of Operators in C
The order of the operators to be evaluated in an expression is determined by the Precedence of Operator. Operations can be evaluated from left to right or right to left.
Operator precedence determines how terms in an expression are grouped and how an expression is evaluated. Certain operators are more important than others; for example, the multiplication operator is more important than the addition operator. Operators with the most precedence appear at the top of the table, while those with the least precedence appear at the bottom. Higher precedence operators in an expression will be evaluated first.
The precedence of the operators and the direction of evaluation are shown in the table below.
|Group||( ) [ ]||Left to right|
|Postfix||-> .||Left to right|
|Postfix||++ —||Left to right|
|Unary||+ – ! ~||Right to Left|
|Prefix||++ —||Right to Left|
|Unary||(type)* sizeof||Right to Left|
|Multiplicative||* / %||Left to right|
|Additive||+ –||Left to right|
|Shift||<< >>||Left to right|
|Relational||< <= > >=||Left to right|
|Equality||== !=||Left to right|
|Bitwise AND||&||Left to right|
|Bitwise XOR||^||Left to right|
|Bitwise OR|||||Left to right|
|Logical AND||&&||Left to right|
|Logical OR||||||Left to right|
|Conditional||?:||Right to Left|
|Assignment||= += -= *= /= %=>>= <<= &= ^= |=||Right to Left|
|Comma||,||Left to right|
Mathematical arithmetic operations such as addition, subtraction, multiplication, division, and module are performed using arithmetic operators. The arithmetic or mathematical operations that these operators handle include addition (+), subtraction (-), multiplication (*), division (/), the remainder of the division (%), increment (++), and decrement (–).
There are two categories of arithmetic operators:
Unary Operators: These operators, like ++ and —, operate on a single value (operand).
Binary Operators: These operators use two operands, such as +,-,*,/.
The relational operators are used to compare two operands. The relational operators will compare and return true or false depending on the result. Equal, not equal, greater than, greater than or equals to, less than, less than or equals to are the relational operators.
When comparing the values of two operands, relational operators are used. If you want to see if one operand is equal to or greater than another, use the >= operator.
The logical operators will carry out the operations between the operands. AND, OR, and NOT are the logical operators. Logical Operators are used to return True or False values.
If you use the Logical AND operator and both operands are non-zero, the condition becomes true. Otherwise, the condition will be false. If you use the Logical OR Operator and one of the two operands is non-zero, the condition becomes true. Otherwise, the condition will be false.
It is used to reverse the logical state of its operand if you call it Logical NOT Operator. If a condition is true, the Logical NOT operator changes it to false.
To assign a value or expression to a variable, use the assignment operators. The assignment operators are used to assign values to variables. =, +=, -=, *=, /=,%=,>>=, =, &=, =, |= are the assignment operators. The assignment operators are used to assign or store the result of an expression in a variable. Along with assignment, the assignment operators can carry out mathematical or logical operations.
Before assigning a value, the conditional operator is used to check the condition. Conditional operators are referred to as ternary operators. ?: is the conditional operator.
Bitwise operations are carried out using bitwise operators. Bitwise AND, bitwise OR, bitwise NOT, and bitwise XOR are the bitwise operators.
These operators are used on the operands to perform bit-level operations. The operators are converted to bit-level first, followed by calculations. Bitwise operators are operators that operate on bits and perform bit-by-bit operations. Mathematical operations such as addition, subtraction, multiplication, division, and so on are converted to bit-level processing, which makes computation and programme compilation faster and easier to implement.
The unary operators only have one operand. Unary operators are used to specify positive or negative values. +, -, (type)* sizeof are the unary operators.