6 Types of Operators in C and C++ | Enhance Your Fundamental Skills [Quiz included]

Get Certified in C Programming and Take Your Skills to the Next Level

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.

This is just the beginning, at the end of this article we will test your knowledge with an interactive quiz

Types of Operators in C and C++

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

Types of Operators in C and 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:

  1.  + (Addition) – This operator is used to add two operands.
  2. – (Subtraction) – Subtract two operands.
  3. * (Multiplication) – Multiply two operands.
  4. / (Division) – Divide two operands and gives the quotient as the answer.
  5. % (Modulus operation) – Find the remains of two integers and gives the remainder after the division.
  6. ++ (Increment) – Used to increment an operand.
  7. — (Decrement) – Used to decrement an operand.
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-

Example of Arithmetic Operators in C

The output will be- 

Arithmetic Operators with Result

 

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-

Example of Arithmetic Operators in Cpp

Output-

Output of Arithmetic Operators in Cpp
Table for Arithmetic Operators in C and C++
OperatorOperandOperationElucidation
+a, ba + bAddition
a, ba – bSubtraction
*a, ba * bMultiplication
/a, ba / bDivision
%a, ba % b
Modulus operator – to find the remainder when two integral digits are divided
++aa ++Increment
aa – –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.

  1. == (Equal to)– This operator is used to check if both operands are equal.
  2. !=  (Not equal to)– Can check if both operands are not equal.
  3. > (Greater than)– Can check if the first operand is greater than the second.
  4. < (Less than)- Can check if the first operand is lesser than the second.
  5. >=  (Greater than equal to)– Check if the first operand is greater than or equal to the second.
  6. <= (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.

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-

Example of Relational Operators in C

The output will be-

Output of Relational Operators in C Language

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-

Example of Relational Operators in C++

Output-

Output of Relational Operators in Cpp
Table for Relational Operators in C and C++
 OperatorOperandOperationElucidation
==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 –

  1. && (AND) – It is used to check if both the operands are true.
  2. || (OR) – These operators are used to check if at least one of the operand is true.
  3. ! (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-

example of logical operators in C

The output will be-

Logical Operators in C with Output

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-

Example of Logical Operators in C++

Output-

Output of Logical Operators in C++

Table for Logical Operators in C and C++
OperatorOperandOperationElucidation
&&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.

  1. =  (Assignment)- Used to assign a value from right side operand to left side operand.
  2. += (Addition Assignment)- To store the sum of both the operands to the left side operand.
  3. -= (Subtraction Assignment) – To store the difference of both the operands to the left side operand.
  4. *= (Multiplication Assignment) – To store the product of both the operands to the left side operand.
  5. /= (Division Assignment) – To store the division of both the operands to the left side operand.
  6. %= (Remainder Assignment) – To store the remainder of both the operands to the left side operand.
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-

Example of Assignment Operators in C

The output will be-

Output of Assignment Operators in C

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-

Example of Assignment Operators in C++

Output-

Output of Assignment Operators in Cpp

Table for Assignment Operators in C and C++
OperatorOperandOperationElucidation
=a, ba=b
Used to assign a value from right side operand to left side operand
+=a, ba+=b
a=a+b: The value of a+b is stored in a
-=a, ba-=b
a=a-b: The value of a-b is stored in a
*=a, ba*=b
a=a*b: The value of a*b is stored in a
/=a, ba/=b
a=a/b: The value of a/b is stored in a
%=a, ba%=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 –

  1. & (Bitwise AND) – Converts the value of both the operands into binary form and performs AND operation bit by bit.
  2. | (Bitwise OR) – Converts the value of both the operands into binary form and performs OR operation bit by bit.
  3. ^ (Bitwise exclusive OR) – Converts the value of both the operands into binary form and performs EXCLUSIVE OR operation bit by bit.
  4. ~ (One’s complement operator): Converts the operand into its complementary form.
  5. << – Left shift
  6. >> – 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

aba & ba | ba ^ b~a
000001
010111
100110
111100

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:

  1. Find the binary equivalent of a and b:
  2. Perform boolean AND/OR/EXCLUSIVE OR operation bit by btw
  3. 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-

Example of Bitwise operators in C

The Output is –

Output of Bitwise Opertors

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-

Example of Bitwise Operators in C++

Output-

Output of Bitwise Operators in C++

Table for Bitwise Operators in C and C++
OperatorOperandOperationElucidation
&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
<<aa<<Left shift
>>aa>>Right shift

 

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:

  1. sizeof – It returns the memory occupied by the particular data type of the operand
  2. & (Pointer) – It refers to the address (memory location) in which the operand is stored.
  3. *  (Pointer) –  It is a pointer operator
  4. ? (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-

Misc Operators in C

The output is-

Misc Operators Results

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-

Example of Misc Operators in C++

Output-

Output of Misc Operators in C++

A table for Misc Operators in C and C++
OperatorOperandOperationElucidation
sizeofasizeof(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* aIt is a pointer
?:a,ba? b: statement
It is an alternative for if-else condition

Quiz on Operators in C and C++

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.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

3 Responses

  1. harsh shukla says:

    best information ,
    please keep it up like that

  2. naykokohein says:

    ဘာလို့မရတာလဲ

  3. Ayush Soni says:

    Quiz on Operators in C and C++
    Question 1- ka answer
    a=76, b=12 or c=88 hoga

Leave a Reply

Your email address will not be published. Required fields are marked *