# 6 Types of Operators in C and C++ | Enhance Your Fundamental Skills

* Operators are the basic concept of any programming language, used to build a foundation in programming for freshers*. Operators can be defined as basic symbols that help us work on logical and mathematical operations. Operators in C and C++, are tools or symbols that are used to perform mathematical operations concerning arithmetic, logical, conditional and, bitwise operations.

There are many sub-operators presents in each type of Operators in C/C++. Let’s discuss one by one with their examples.

## Types of Operators in C and C++

There are 6 types of Operators in C/C++

Let us discuss in detail the function of each type of operator.

### 1. Arithmetic Operators

It includes basic arithmetic operations like addition, subtraction, multiplication, division, modulus operations, increment, and decrement.

The Arithmetic Operators in C and C++ include:

**+ (Addition)**– This operator is used to add two operands.**– (Subtraction)**– Subtract two operands.*** (Multiplication)**– Multiply two operands.**/ (Division)**– Divide two operands and gives the quotient as the answer.**% (Modulus operation)**– Find the remains of two integers and gives the remainder after the division.**++ (Increment)**– Used to increment an operand.**— (Decrement)**– Used to decrement an operand.

**Before we move ahead, you should Master in the Data Types in C/C++**

##### Example of Arithmetic Operators in C

#include<stdio.h> int main() { int a=10, b=7; printf("Welcome to DataFlair tutorials!\n\n"); printf("The Addition of %d and %d is: %d\n",a,b,a+b); printf("The Subtraction of %d and %d is: %d\n",a,b,a-b); printf("The Multiplication of %d and %d is: %d\n",a,b,a*b); printf("The Division of %d and %d is: %d\n",a,b,a/b); printf("The Modulus operation of %d and %d is: %d\n",a,b,a%b); printf("The Incremented value ++a is: %d\n",++a); printf("The Decremented value --b is: %d\n",--b); return 0; }

**Code on Screen-**

**The output will be- **

**Struggling with Structure of C Program?**

##### Example of Arithmetic Operators in C++

Here is a code in C++ which illustrates all the basic arithmetic operators:

#include <iostream> using namespace std; int main() { cout<<"Welcome to DataFlair tutorials!\n\n"<<endl<<endl; int a = 10, b = 7; cout<<"The Addition of "<< a << " and " << b << " are: " << a + b <<endl; cout<<"The Subtraction of "<< a << " and " << b << " are: " << a - b <<endl; cout<<"The Multiplication of "<< a << " and " << b << " are: " << a * b <<endl; cout<<"The Division of "<< a << " and " << b << " are: " << a / b <<endl; cout<<"The Modulus operation between "<< a << " and " << b << " is: " << a % b <<endl; cout<<"The Incremented value ++a is: "<< ++a <<endl; cout<<"The Decremented value --a is: "<< --a <<endl; return 0; }

**Code-**

**Output-**

##### Table for Arithmetic Operators in C and C++

Operator | Operand | Operation | Elucidation |

+ | a, b | a + b | Addition |

– | a, b | a – b | Subtraction |

* | a, b | a * b | Multiplication |

/ | a, b | a / b | Division |

% | a, b | a % b | Modulus operator – to find the remainder when two integral digits are divided |

++ | a | a ++ | Increment |

— | a | a – – | Decrement |

### 2. Relational Operators

It is used to compare two numbers by checking whether they are equal or not, less than, less than or equal to, greater than, greater than or equal to.

**==****(Equal to)**– This operator is used to check if both operands are equal.**!=****(Not equal to)**– Can check if both operands are not equal.**>****(Greater than)**– Can check if the first operand is greater than the second.**< (Less than)-**Can check if the first operand is lesser than the second.**>=****(Greater than equal to)**– Check if the first operand is greater than or equal to the second.**<= (Less than equal to)**– Check if the first operand is lesser than or equal to the second

If the relational statement is satisfied (it is true), then the program will return the value 1, otherwise, if the relational statement is not satisfied (it is false), the program will return the value 0.

**It’s time to gear up your skills with Variables in C and C++**

##### Example of Relational Operators in C-

#include <stdio.h> int main() { int a=10, b=10, c=20; printf("Welcome to DataFlair tutorials!\n\n"); printf("For %d == %d : The output is: %d \n", a, b, a == b); // condition is true printf("For %d == %d : The output is: %d \n", a, c, a == c); // condition is false printf("For %d != %d : The output is: %d \n", a, c, a != c); // condition is true printf("For %d != %d : The output is: %d \n", a, b, a != b); // condition is false printf("For %d > %d : The output is: %d \n", a, b, a > b); // condition is false printf("For %d > %d : The output is: %d \n", a, c, a > c); // condition is false printf("For %d < %d : The output is: %d \n", a, b, a < b); // condition is false printf("For %d < %d : The output is: %d \n", a, c, a < c); // condition is true printf("For %d >= %d : The output is: %d \n", a, b, a >= b); // condition is true printf("For %d >= %d : The output is: %d \n", a, c, a >= c); // condition is false printf("For %d <= %d : The output is: %d \n", a, b, a <= b); // condition is true printf("For %d <= %d : The output is: %d \n", a, c, a <= c); // condition is true return 0; }

**Code on Screen-**

**The output will be-**

##### Example of Relational Operators in C++

Here is a code in C++ which illustrates all the basic relational operators:

#include <iostream> using namespace std; int main() { cout<<"Welcome to DataFlair tutorials!"<<endl<<endl; int a = 10, b = 10, c = 20; cout<<"For " << a << " == " << b << " The output is: " << (a == b) << endl; // condition is true cout<<"For " << a << " == " << c << " The output is: " << (a == c) << endl; // condition is false cout<<"For " << a << " != " << c << " The output is: " << (a != c) << endl; // condition is true cout<<"For " << a << " != " << b << " The output is: " << (a != b) << endl; // condition is false cout<<"For " << a << " > " << b << " The output is: " << (a > b) << endl; // condition is false cout<<"For " << a << " > " << c << " The output is: " << (a > c) << endl; // condition is false cout<<"For " << a << " < " << b << " The output is: " << (a < b) << endl; // condition is false cout<<"For " << a << " < " << c << " The output is: " << (a < c) << endl; // condition is true cout<<"For " << a << " >= " << b << " The output is: " << (a >= b) << endl; // condition is true cout<<"For " << a << " >= " << c << " The output is: " << (a >= c) << endl; // condition is false cout<<"For " << a << " <= " << b << " The output is: " << (a <= b) << endl; // condition is true cout<<"For " << a << " <= " << c << " The output is: " << (a <= c) << endl; // condition is true return 0; }

**Code-**

**Output-**

##### Table for Relational Operators in C and C++

Operator | Operand | Operation | Elucidation |

== | a, b | (a==b) | Used to check if both operands are equal |

!= | a, b | (a!=b) | Used to check if both operands are not equal |

> | a, b | (a>b) | Used to check if the first operand is greater than the second |

< | a, b | (a<b) | Used to check if the first operand is lesser than the second |

>= | a, b | (a>=b) | Used to check if the first operand is greater than or equal to the second |

<= | a, b | (a<=b) | Used to check if the first operand is lesser than or equal to the second |

### 3. Logical Operators

It refers to the boolean values which can be expressed as:

- Binary logical operations, which involves two variables: AND and OR
- Unary logical operation: NOT

Logical Operators in C/C++ Includes –

**&& (AND)**– It is used to check if both the operands are true.**|| (OR)**– These operators are used to check if at least one of the operand is true.**! (NOT)**– Used to check if the operand is false

If the logical statement is satisfied (it is true), then the program will return the value 1, otherwise, if the relational statement is not satisfied (it is false), the program will return the value 0.

**7 Basic C Programs that will help you to rise from Noob to Pro**

##### Example of Logical Operators in C Programming-

#include <stdio.h> int main() { int a = 10, b = 10, c = 20, answer; printf("Welcome to DataFlair tutorials!\n\n"); answer = (a == b) && (c > b); printf("For (%d == %d) && (%d != %d), the output is: %d \n",a,b,b,c,answer); //condition is true answer = (a == b) && (c < b) && (c>0); printf("For (%d == %d) && (%d <= %d), the output is: %d \n",a,b,b,c,answer); //condition is false answer = (a == b) || (b > c); printf("For (%d == %d) || (%d < %d), the output is: %d \n",a,b,c,b,answer); / /condition is true answer = (a != b) || (a <= b) || (a>c); printf("For (%d != %d) || (%d < %d), the output is: %d \n",a,b,c,b,answer); //condition is true answer = !(a == b); printf("For !(%d == %d), the output is: %d \n",a,b,answer); //condition is false answer = !(a != b); printf("For !(%d == %d), the output is: %d \n",a,b,answer); //condition is true return 0; }

**Code on Screen-**

**The output will be-**

##### Example of Logical Operators in C++

Here is a code in C++ which illustrates all the basic logical operators:

#include <iostream> using namespace std; int main() { int a = 10, b = 10, c = 20, answer; cout<<"Welcome to DataFlair tutorials!"<<endl<<endl; answer = (a == b) && (c > b); cout<<"For (" << a << " == " << b << ") && (" << b << " != "<< c << "), the output is: " << answer <<endl; //condition is true answer = (a == b) && (c < b) && (c>0); cout<<"For (" << a << " == " << b << ") && (" << b << " <= "<< c << "), the output is: " << answer <<endl; //condition is false answer = (a == b) || (b > c); cout<<"For (" << a << " == " << b << ") && (" << c << " < "<< b << "), the output is: " << answer <<endl; //condition is true answer = (a != b) || (a <= b) || (a>c); cout<<"For (" << a << " != " << b << ") && (" << c << " < "<< b << "), the output is: " << answer <<endl; //condition is true answer = !(a == b); cout<<"For !(" << a << " == " << b << "), the output is: "<< answer <<endl; //condition is false answer = !(a != b); cout<<"For !(" << a << " != " << b << "), the output is: "<< answer <<endl; //condition is true return 0; }

**Code-**

**Output-**

##### Table for Logical Operators in C and C++

Operator | Operand | Operation | Elucidation |

&& | a, b | (a && b) | AND: Used to check if both the operands are true |

|| | a, b | (a || b) | OR: Used to check if at least one of the operand is true |

! | a | !a | NOT: Used to check if the operand is false |

### 4. Assignment Operators

It is used to assign a particular value to a variable. We will discuss it in detail in the later section with its shorthand notations.

**= (Assignment)-**Used to assign a value from right side operand to left side operand.**+= (Addition Assignment)-**To store the sum of both the operands to the left side operand.

**-= (Subtraction Assignment) –**To store the difference of both the operands to the left side operand.***=****(Multiplication Assignment)**– To store the product of both the operands to the left side operand.**/= (Division Assignment)**– To store the division of both the operands to the left side operand.**%= (Remainder Assignment)**– To store the remainder of both the operands to the left side operand.

**Explore the Career Opportunities in C with current salary structures**

##### Example of Assignment Operators in C

#include<stdio.h> int main() { printf("Welcome to Dataflair tutorials!\n\n"); int number = 10, result; result = number; printf("result = %d \n", result); result += number; //Same as result = result + a printf("result = %d \n", result); result -= number; //Same as result = result - a printf("result = %d \n", result); result *= number; //Same as result = result * a printf("result = %d \n", result); result /= number; //Same as result = result / a printf("result = %d \n", result); result %= number; //Same as result = result % a printf("result = %d \n", result); return 0; }

**Code on Screen-**

**The output will be-**

##### Example of Assignment Operators in C++

Here is a code in C++ which illustrates all the basic assignment operators:

#include <iostream> using namespace std; int main() { cout<<"Welcome to Dataflair tutorials!"<<endl<<endl; int number = 10, result; result = number; cout<<"result = "<< result<<endl; result += number; //Same as result = result + a cout<<"result = "<< result<<endl; result -= number; //Same as result = result - a cout<<"result = "<< result<<endl; result *= number; //Same as result = result * a cout<<"result = "<< result<<endl; result /= number; //Same as result = result / a cout<<"result = "<< result<<endl; result %= number; //Same as result = result % a cout<<"result = "<< result<<endl; return 0; }

**Code-**

**Output-**

##### Table for Assignment Operators in C and C++

Operator | Operand | Operation | Elucidation |

= | a, b | a=b | Used to assign a value from right side operand to left side operand |

+= | a, b | a+=b | a=a+b: The value of a+b is stored in a |

-= | a, b | a-=b | a=a-b: The value of a-b is stored in a |

*= | a, b | a*=b | a=a*b: The value of a*b is stored in a |

/= | a, b | a/=b | a=a/b: The value of a/b is stored in a |

%= | a, b | a%=b | a=a %b: The value of a%b is stored in a |

### 5. Bitwise Operators

It is based on the principle of performing operations bit by bit which is based on boolean algebra. It increases the processing speed and hence the efficiency of the program.

The Bitwise Operators in C/C++ Includes –

**& (Bitwise AND)**– Converts the value of both the operands into binary form and performs AND operation bit by bit.**| (Bitwise OR) –**Converts the value of both the operands into binary form and performs OR operation bit by bit.**^ (Bitwise exclusive OR)**– Converts the value of both the operands into binary form and performs EXCLUSIVE OR operation bit by bit.**~ (One’s complement operator)**: Converts the operand into its complementary form.**<< – Left shift****>> – Right shift**

**Key takeaway:** *Bitwise operators are not applicable in the case of float and double data type in C.*

In order to clearly understand bitwise operators, let us see the truth table for various bitwise operations and understand how it is associated with boolean algebra.

Since there are 2 variables, namely, a and b, there are 22 combinations for values a and b can take simultaneously.

**AND** – Both the operands should have boolean value 1 for the result to be 1.

**OR** – At least one operand should have boolean value 1 for the result to be 1.

**XOR (EXCLUSIVE OR)** – Either the first operand should have boolean value 1 or the second operand should have boolean value 1. Both cannot have the boolean value 1 simultaneously.

**One Complement: iF**

a | b | a & b | a | b | a ^ b | ~a |

0 | 0 | 0 | 0 | 0 | 1 |

0 | 1 | 0 | 1 | 1 | 1 |

1 | 0 | 0 | 1 | 1 | 0 |

1 | 1 | 1 | 1 | 0 | 0 |

The left and right shift operators are responsible for shifting the binary values by some specific number of places.

**Left shift**: It specifies the value of the left operand to be shifted to the left by the number of bits specified by its right operand

**Right shift**: It species the value of the left operand to be shifted to the right by the number of bits specified by its right operand.

Let us take an example each of performing bitwise AND, OR, EXCLUSIVE OR and ONE’S COMPLEMENT operation.

Consider two operands, a and b with values:

a = 26 and b=14

Therefore, a & b is computed as follows:

- Find the binary equivalent of a and b:
- Perform boolean AND/OR/EXCLUSIVE OR operation bit by btw
- Convert the answer into its corresponding decimal form.

**Bitwise AND**

a = 26 = 1 1 0 1 0

b = 14 = 0 1 1 1 0

________

a & b = 0 1 0 1 0 which is equal to 10

**Bitwise OR**

a = 26 = 1 1 0 1 0

b = 14 = 0 1 1 1 0

________

a | b = 1 1 1 1 0 which is equal to 30

**Bitwise XOR**

a = 26 = 1 1 0 1 0

b = 14 = 0 1 1 1 0

________

a | b = 1 0 1 0 0 which is equal to 20

**Bitwise****One’s Complement**

a = 26 = 1 1 0 1 0

Reversing its bits, we get 0 0 1 0 1 which is equal to 5 but this is not the correct answer! The correct answer is: -(a+1) which is -27 which is in accordance with two’s complement.

##### Example of Bitwise Operators in C

#include <stdio.h> int main() { printf("Welcome to DataFlair tutorials!\n\n"); int a = 26, b = 14; printf(" Bitwise AND operation %d & %d : %d\n",a,b,a&b); printf(" Bitwise OR operation %d | %d : %d\n",a,b,a|b); printf(" Bitwise XOR operation %d ^ %d : %d\n",a,b,a^b); printf(" Bitwise ONE'S COMPLEMENT ~ %d operation : %d\n",a,~a); return 0; }

**Code on Screen-**

**The Output is –**

##### Example of Bitwise Operators in C++

Here is a code in C++ which illustrates all the basic bitwise operators:

#include <iostream> using namespace std; int main() { cout<<"Welcome to DataFlair tutorials!"<<endl<<endl; int a = 26, b = 14; cout<<" Bitwise AND operation" << a << " & " << b << " : " << (a&b) <<endl; cout<<" Bitwise OR operation" << a << " | " << b << " : " << (a|b) <<endl; cout<<" Bitwise XOR operation" << a << " ^ " << b << " : " << (a^b) <<endl; cout<<" Bitwise ONE'S COMPLEMENT ~"<< a << " operation :"<< (~a) <<endl; return 0; }

**Code-**

**Output-**

##### Table for Bitwise Operators in C and C++

Operator | Operand | Operation | Elucidation |

& | a, b | ( a & b ) | Bitwise AND: Converts the value of both the operands into binary form and performs AND- operation bit by bit |

| | a, b | ( a | b ) | Bitwise OR: Converts the value of both the operands into binary form and performs OR- operation bit by bit |

^ | a, b | ( a ^ b ) | Bitwise exclusive OR: Converts the value of both the operands into binary form and performs EXCLUSIVE OR operation bit by bit |

~ | a | ( ~ a ) | One’s complement operator: Converts the operand into its complementary form |

<< | a | a<< | Left shift |

>> | a | a>> | Right shift |

**Before we start discussing Miscellaneous Operators, you should know What is Pointers in C/C++**

### 6. Miscellaneous Operators

Apart from the above-discussed operators, there are certain operators which fall under this category which include sizeof and ternary (conditional) operators.

Here is a table which illustrates the use of these operators:

**sizeof**– It returns the memory occupied by the particular data type of the operand**& (Pointer)**– It refers to the address (memory location) in which the operand is stored.*******(Pointer)**– It is a pointer operator**? (Condition)**– It is an alternative for if-else condition

##### Example of **Miscellaneous **Operators in C

#include<stdio.h> int main() { printf("Welcome to Data Flair tutorials!\n\n"); // Use of * and & operator int number = 10, *pointer; pointer=&number; //Here the pointer stores the memory address of variable number printf("The value of the number is: %d\n",*pointer); // Use of ?: operator int expression1 = 10, expression2 = 20, expression3; expression3 = ( expression1 > expression2 ) ? expression1 : expression2; printf("The Output of the ternary statement is: %d", expression3); return 0; }

**Code on Screen-**

**The output is-**

##### Example of **Miscellaneous** Operators in C++

Here is a code in C++ which illustrates some of the basic misc operators:

#include <iostream> using namespace std; int main() { cout<<"Welcome to Data Flair tutorials!"<<endl<<endl; // Use of * and & operator int number = 10, *pointer; pointer=&number; //Here the pointer stores the memory address of variable number cout<<"The value of the number is: "<<*pointer<<endl; // Use of ?: operator int expression1 = 10, expression2 = 20, expression3; expression3 = ( expression1 > expression2 ) ? expression1 : expression2; cout<<"The Output of the ternary statement is: "<< expression3 <<endl; return 0; }

**Code-**

**Output-**

##### A table for Misc Operators in C and C++

Operator | Operand | Operation | Elucidation |

sizeof | a | sizeof(a) | It returns the memory occupied by the particular data type of the operand |

& | a | & a | It refers to the address (memory location) in which the operand is stored. |

* | a | * a | It is a pointer |

?: | a,b | a? b: statement | It is an alternative for if-else condition |

## Summary

Operators are the basic foundation of the C/C++ Programming language. Now, you can perform any operation of mathematical, logical, relational, with other condition. We learned each operator in C and C++ with their examples. As a beginner, you should know each operator, and how, why, when to use it.

*It’s not the end, check out the next article – Recursion in C/C++*

Drop your queries and suggestion in the comment section.

best information ,

please keep it up like that