Enhance Your Fundamental Skills [Quiz included] – DataFlair

How to determine if a sequence is arithmetic or not
How to determine if a sequence is arithmetic or not

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

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++

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

Stay updated with latest technology trends
Join DataFlair on Telegram!!

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-

The output will be-

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.

  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-

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 –

  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-

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.

  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-

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 –

  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

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:

  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-

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

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-

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

Quiz on Operators in C and C++

Quiz Summary

0 of 15 Questions completed

Questions:

Information

You have already completed the quiz before. Hence you can not start it again.

Quiz is loading…

You must sign in or sign up to start the quiz.

You must first complete the following:

Results

Results

0 of 15 Questions answered correctly

Your time:

Time has elapsed

You have reached 0 of 0 point(s), (0)

Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. 1. Question

    Predict the output of the following C/C++ Program:

    #include
    using namespace std;
    int main()
    {
    int x, y, z;
    x=6;y=5;z=4;
    y=++x+z++;
    z=y++*++x;
    x=–y+z–;
    cout<<x<<endl<<y<<endl<<z;
    }CorrectIncorrect

  2. 2. Question

    Predict the output of the following C/C++ Program:

    #include
    using namespace std;
    int main()
    {
    static int x;
    for(x=0; x<6; x++)
    {
    cout<<“First loop”<<endl;
    for(x=0;x<6; ++x)
    {
    cout<<“second loop”<<endl;
    }
    }
    }CorrectIncorrect

  3. 3. Question

    Predict the output of the following C/C++ Program:

    #include
    using namespace std;
    int main()
    {
    static int m,n;
    m=6;n=5;
    n=++m;
    m= m++ + ++n;
    cout<<m<<endl<<n;
    }CorrectIncorrect

  4. 4. Question

    Predict the output of the following C/C++ Program:

    #include
    using namespace std;
    int main()
    {
    int m,n;
    m= 9*8+9-12*11/2;
    n= ~m;
    cout<<m<<endl<<n;
    }CorrectIncorrect

  5. 5. Question

    Predict the output of the following C/C++ Program:

    #include
    using namespace std;
    int main()
    {
    int m,n;
    m= 3;
    n= m<<3;
    cout<<m<<endl<<n;
    }CorrectIncorrect

  6. 6. Question

    Predict the output of the following C/C++ Program:

    #include
    using namespace std;
    int main()
    {
    int x,y,result;
    if(x>=y)
    {
    result =x-y>y*2?y:x+2;
    }
    cout<<result;
    }CorrectIncorrect

  7. 7. Question

    Predict the output of the following C/C++ Program:

    #include
    using namespace std;
    int main()
    {
    int x=5,y=9,z=3;
    int result= x|z&y;
    cout<<result;
    }CorrectIncorrect

  8. 8. Question

    Predict the output of the following C/C++ Program:

    #include
    using namespace std;
    int main()
    {
    int x=5,y=9,z=3;
    int res= x|z&y;
    int res1= x|y&y;
    int result=res>res1?res:res1;
    cout<<result;
    }CorrectIncorrect

  9. 9. Question

    Predict the output of the following C/C++ Program:

    #include
    using namespace std;
    int main()
    {
    int a=10,b=90;
    int c= a^b;
    a = ~b;
    b = !c;
    cout<<a<<endl<<b<<endl<<c;
    }CorrectIncorrect

  10. 10. Question

    Predict the output of the following C/C++ Program:

    #include
    using namespace std;
    int main()
    {
    int a=5,b=12,c=15,d=20;
    if(a>b||c>d)
    cout<<“Delhi”;
    else if(a<c&&b<d)
    cout<<“Mumbai”;
    else if(ac)
    cout<<“Jaipur”;
    else
    cout<<“Goa”;
    }CorrectIncorrect

  11. 11. Question

    which of the following is not an assignment operator?

    CorrectIncorrect

  12. 12. Question

    Which of the following is not a relational operator?

    CorrectIncorrect

  13. 13. Question

    Which of the following is the compile time unary operator?

    CorrectIncorrect

  14. 14. Question

    Which of the following option has operators arranged in precedence order

    CorrectIncorrect

  15. 15. Question

    b =(~a)<<1 What is the value of b if a=5?

    CorrectIncorrect

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.

Experience the joy of learning, get certified free courses – follow us on Google

You are watching: Enhance Your Fundamental Skills [Quiz included] – DataFlair. Info created by THVinhTuy selection and synthesis along with other related topics.

Rate this post

Related Posts