Let us understand what are the different types of C operators and understand the different types of operators in C programming language with operator precedence chart.
Every mathematical expression includes operators and operands without which it is an incomplete expression.
It is, therefore, important to define the use of operators in C programming as there is a lot of mathematical calculations required in C programming. These operators are inevitable in numerical methods C programs.
An operator usually behaves like functions. An operator is an object that is capable of manipulating the values of the operands associated with it. Every operator requires one or more operands to make it a meaningful expression.
These operators could be unary operators or binary operators. The unary operators require only one operand with it whereas binary operators require a minimum of two operators to work with.
Here’s the list of C operators available in C programming language:
- Assignment operator
- Relational operator
- Arithmetic operator
- Logical operator
- Conditional operator
- Bitwise operator
- Increment/Decrement operator
- Special operator
- Miscellaneous operator
Types of C Operators – Examples and Explanation
Note: Assume, three integer variables x, y and z where x = 100 and y = 50.
Let us now see different types of C operators including unary and binary operators with their description and example.
An arithmetic operator is a mathematical function that takes in two operands and performs simple and common mathematical calculations.
|+||This is a simple addition operator used to add two or more operands.||x + y = 150|
|–||This is a simple subtraction operator used to subtract two or more operands.||x – y = 50|
|/||This is a simple division operator used to divide two or more operands.||x / y = 2|
|%||This is a simple modulus operator used to find the mod for two or more operands.||x % y = 0|
|*||This is a simple multiplication operator used to multiply two or more operands.||x * y = 5000|
An assignment operator is used to assign a new value to the variable on the left-hand side of the expression which is also known as Lvalue. It may include a combination of bitwise and logical operators.
These operators are popularly known as shorthand operators since they represent a concise form of the regular assignment and arithmetic operators. These are usually a combination of Assignment operator and Arithmetic or Bitwise operator.
|=||This is the assignment operator and assigns the value of right operand to the left operand.||z = x|
|%=||This operator assigns the modulus of two operands to the left operand.||x %= y is same as|
x = x % y
|+=||This operator assigns the addition of two or more operands to the left operand.||x += y is same as|
x = x % y
|-=||This operator assigns the subtraction of two or more operands to the left operand.||x -= y is same as|
x = x % y
|*=||This operator assigns the multiplication of two or more operands to the left operand.||x *= y is same as|
x = x % y
|>>=||This is a bitwise right shift assignment operator and assigns the result to the left operand.||x >>= y is same as|
x = x >> y
|<<=||This is a bitwise left shift assignment operator and assigns the result to the left operand.||x <<= y is same as|
x = x << y
||=||This is a bitwise OR assignment operator and assigns the result to the left operand.||x |= y is same as|
x = x | y
|/=||This operator assigns the division of two or more operands to the left operand.||x /= y is same as|
x = x / y
|&=||This is a bitwise AND assignment operator and assigns the result to the left operand.||x &= y is same as|
x = x & y
|^=||This is a bitwise XOR assignment operator and assigns the result to the left operand.||x ^= y is same as|
x = x ^ y
The logical operators are used with Boolean values. They always return a boolean value viz. True or False. It returns a boolean value True if both the operands are True otherwise, it returns a Boolean False.
In other words, if both the operands are non-zero, the result is True else False is returned.
|!||This is a unary logical NOT operator and is used to reverse the state of its operand. If the operand is in a false state, then it reverses its state to True.||!(x && y) is True|
|||||This is a binary logical OR operator. If any of the operands is non-zero, then it returns True.||(x || y) is True|
|&&||This is a binary logical AND operator. Only if both the operands are non-zero, then it returns True, else False.||(x && y) is False|
The bitwise operations are directly supported by the processor and are usually faster compared to other operators. These bitwise operators use less power as they use less computing resources.
The bitwise operators are used to change individual bits in an operand and it operates on one or more bit patterns. Every bit can be considered as a boolean variable and can hold one or two values viz. True or False.
|&||A bitwise AND operator is a binary operator and takes two binary representations and performs the logical AND operation on each pair of the corresponding bits by multiplying them.||x & y|
||||A bitwise OR operator is a binary operator and takes two-bit equal patterns and performs the logical inclusive OR operation on each pair of corresponding bits.||x | y|
|<<||This is binary left shift operator and it moves the value of the left operand towards the left by a particular number of bits as specified by the right operand.||x < y|
|>>||This is binary right shift operator and it moves the value of the left operand towards the right by a particular number of bits as specified by the right operand.||x >> y|
|^||This is a binary operator and is called as bitwise XOR operator. It takes two equal bit patterns and performs the logical XOR operation on each pair of corresponding bits.||x ^ y|
|~||This is a unary operator and is called as One’s complement operator. It performs logical negation and returns the result after flipping the original bits.||~x|
There are a total of six relational C operators. A relational operator in C programming tests the relationship between two different entities or the operands.
Based on numerical equality, these relational operators check if the two operands are related to each other based on the operator and return True if they’re related as per the operator, otherwise False.
|>||This is a binary greater than operator that returns True if the first operand is greater than the second operator.||(x > y) is True|
|<||This is a binary less than operator that returns True if the first operand is less than the second operator.||(x < y) is False|
|>=||This is a binary greater than or equal to operator that returns True if the first operand is greater than or equal to the second operator.||(x >= y) is True|
|<=||This is a binary less than or equal to operator that returns True if the first operand is less than or equal to the second operator.||(x <= y) is False|
|==||This is a binary operator that checks the equivalency of the operands. If the values of both the operands are equivalent, then it returns True, else False.||(x == y) is False|
|!=||This is a binary NOT operator that returns True if the values of the operands are unequal, else it returns False.||(x != y) is True|
There are some C operators that helps to manipulate or fetch data based on memory allocations or memory addresses of the variables.
|&||This unary operator is called as Address of operator or Reference operator. It returns the memory address of the variable.||&x|
|sizeof||The sizeof() operator returns the memory allocated to the variable in bytes.||sizeof(x)|
|*||This is a pointer to an integer operator and is a unary operator.||*x|
|?:||The conditional operator is also known as Ternary operator and it acts like If-Else loop. If x is greater than y, it executes m, else n.||(x > y) ? m : n|
Increment and Decrement Operators
The increment and decrement operators are nothing but a modified version of arithmetic addition and subtraction operator. They are usually used in looping conditions such as for loop, while loop and do – while loop.
|++ (Postfix)||This is the postfix increment operator and is an unary operator. It adds 1 to the value.||x++ is same as|
x = x + 1
|– – (Postfix)||This unary operator is the postfix decrement operator. It subtracts 1 from the value.||y- – is same as|
y = y – 1
|++ (Prefix)||This is the prefix increment operator and is an unary operator. It adds 1 to the value.||++x|
|– – (Prefix)||This unary operator is known as prefix decrement operator and it subtracts 1 from the value.||– -y|
|,||This is known as Comma operator which evaluates comma separated expressions from left to right. It is a binary operator.||x, y|
|->||This is a binary operator, known as an Arrow operator and is used to access the members of a structure variable using pointers.||x -> y|
|.||This is known as a dereference operator or the dot operator. It is used to refer to a structure member variable using the structure name and is a binary operator.||x. y = 10|
|||This is an Array operator used to define an Array with dimensions.||x|
|()||This is a function call operator used to define and call functions or methods with optional parameters.||x(arguments)|
In every mathematical expression, the operator precedence plays a very important role. It determines the overall result of the expression.
The operator precedence describes the order in which the operations of a mathematical expression are performed and it defines the way how an expression is evaluated.
Usually, C operators with high precedence is executed before an operator with lower precedence.
|Postfix and Prefix||, (), ->, ++, – -, .,||Left to right|
|Unary||+, -, *, &, !, ~, sizeof, ++, – –||Right to left|
|Multiplicative||*, /, %||Left to right|
|Addition||-, +||Left to right|
|Right Shift and Left Shift||>>, <<||Left to right|
|Relational||>=, <=, >, <||Left to right|
|Equality||==, !=||Left to right|
|Bitwise AND, Bitwise XOR, Bitwise OR||&, ^, |||Left to right|
|Conditional||?:||Right to left|
|Assignment||=, >=, <, <=, &=, ^=, |=, +=, -=, *=, /=, %=, >||Right to left|
|Comma||,||Left to right|
If you have any other information about C operators, do let us know about it in the comment section below. Find more about operators in C programming language on Wikipedia.