Every programming language consists of operators, which are nothing but their supporting building blocks. These operators provide a solid foundation for beginners in this field.
There are various types of operators, each a basic symbol facilitating performing logical and mathematical processes. Operators in C and C++ are more like tools leveraged to perform bitwise, conditional, arithmetic, and logical operations.
Also, the different types of operators in C/C++ have a different yet vast number of sub-operators. In this article, we’ll learn everything about operators, including their various types.
Introduction to Operators
Operators are the symbols or keywords that perform various operations on operands. They enable us to carry out specified arithmetic operators (+, -, *, /), relational operators (>, <, ==), logical operators (&&, ||, !), assignment operators (=), and more. They are used to manipulate data and control program flow in C programming. In other terms, an operator can be said to operate the operands.

POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
Types of Operators in C
C/C++ has 6 different types of operators. Let’s go over each sort of operator’s purpose in detail.
1. Arithmetic Operators
Mathematical operations like addition (+), multiplication (*), subtraction (-), division (/), and modulus (%) are all performed using arithmetic operators. All operations on numerical values (constants and variables) are carried out by this type of operator.
Example of Arithmetic Operators
Below is the example of Arithmetic type of operator:
#include
int main()
{
int a = 7,b = 5, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a is divided by b = %d \n",c);
return 0;
}
Output:
a+b = 12
a-b = 2
a*b = 35
a/b = 1
The remainder when a divided by b = 2
2. Logical Operators
Relational operators and logical operators are combined to produce advanced conclusions.
Logical operators in programming, such as && (logical AND), || (logical OR), and ! (logical NOT), are used to evaluate conditions and return true or false based on the truth values of the operands. They are commonly used in conditional statements and loops to make decisions and control program flow.
Example of Logical Operators
Below is the example of logical type of operator:
// Working of logical operators
#include
int main()
{
int a = 15, b = 15, c = 20, results;
results = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", results);
results = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", results);
results = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", results);
results = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", results); results = !(a != b); printf("!(a != b) is %d \n", results); results = !(a == b); printf("!(a == b) is %d \n", results); return 0; } Output: (a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
3. Bitwise Operators
Bit-by-bit operations are carried out by bitwise operators, which operate on bits. In these type of operators, mathematical operations are transformed into bit-level processing, which speeds up and simplifies implementation during computation and program compilation.
Example of Bitwise Operators
int a = 5, b = 9; // a = 5(00000101), b = 9(00001001)
cout << (a ^ b); // 00001100
cout <<(~a); // 11111010
4. Relational Operators
This type of operators are employed in the evaluation of two operands’ values. For instance, determining whether one operand is greater than the other operand, whether one operand equals the other, etc. (==, >=, =) are a few relational operators.
Example of Relational Operators
Below is the example of rational type of operator
int a = 3;
int b = 5;
cout<<(a < b);
// operator to check if a is smaller than b
5. Assignment Operators
The primary duty of an assignment operator in a program is to assign a value to a variable. A variable can be given the outcome of an expression by using assignment operators. The value assignment process for every variable depends heavily on this operator. = is the most popular assignment operator.
Example of Assignment Operators
Below is the example of assignment type of operator
#include
int main()
{
int a = 7, b;
b = a; // b is 7
printf("b = %d\n", b);
b += a; // b is 14
printf("b = %d\n", b);
b -= a; // b is 7
printf("b = %d\n", b);
b *= a; // b is 49
printf("b = %d\n", b);
b /= a; // b is 7
printf("c = %d\n", c);
b %= a; // b = 0
printf("b = %d\n", b);
return 0;
}
Output:
b = 7
b = 14
b = 7
b = 49
b = 7
b = 0
6. Conditional Operators
An if-else block 21 single statement requires less effort when using a conditional or ternary operator. These types of operators was designed to work with conditional expressions.
Example of Conditional operators
Syntax:
VariableName = (condition)? TrueValue : FalseValue;
Example:
a= (b>c) ? (b+c) : (b-c);
Comparison of Each Operator
Below is a quick glance of comparison of all the major types of operators for your better understanding.
| Operator | Explanation |
|---|---|
| Arithmetic Operators | Leveraged to carry out arithmetic operations on operands. |
| Logical Operators | Leveraged to integrate/combine logical expressions. |
| Bitwise Operators | Leveraged to carry out bitwise operations on operands. |
| Relational Operators | Leveraged to compare two operands. |
| Assignment Operators | Leveraged to assign any value to the operand. |
Operator Use Cases and Best Practices
Below are the use cases and examples of different types of operators:
- Arithmetic operators: These are used to carry out operations like division, multiplication, addition, and subtraction on operands.
- Logical operators: These operators integrate logical expressions like AND, OR, and NOT.
- Relational operators: These operators, including less than, greater than, equal to, and not equal to, are utilized for comparing two operands.
- Bitwise operators: These operators are utilized for carrying out bitwise operations on operands like AND, OR, XOR, and NOT.
It’s crucial to use operators in accordance with recommended procedures. The following are some of the most significant best practices:
- Utilizing the appropriate operator for the action you wish to take.
- Expressions are grouped together by using parenthesis.
- Using whitespace helps improve the readability of your code.
- Preventing side effects, which are modifications to the program’s state that aren’t primarily caused by your action.
By adhering to these best practices, you can develop easy to comprehend and clear code.

82.9%
of professionals don't believe their degree can help them get ahead at work.
Conclusion
In this guide, we have covered all about operators and major type sf operators used. Basically, operators are the fundamentals of any coding language and how they work. One needs to have extensive knowledge of operators to develop complicated code for operating various apps or software. For those who aspire to become master coders, having a solid grasp of their usage is essential.
Consider learning programming and data science to create programs like never seen before. If so, the Business Analytics and Data Science course offered by HeroVired can help you start your educational path.
What is the difference between prefix and postfix operators in C?
What are operators in C?
What are the two basic types of operators?
- arithmetic operators.
- relational operators.
What is the role of arithmetic operators in programming?
What is the purpose of assignment operators in programming?
Updated on September 16, 2024
