codescracker


c++

C++ Operators



« Previous Tutorial Next Tutorial »


In C++, operators are used in a program to perform specific task or operations on data. Like, in the following code fragment, the arithmetic operator (addition) perform the addition of the two number (a and b) and initialized to a variable called sum:

int sum = a + b;

Here, the variable sum will initialized a value, which will be the summation of the two values occupied in the variable a and b.

Types of Operators

C++ provides the following types of operators:

Let's discuss all the above C++ operators in detail one by one

C++ Arithmetic Operators

You can categorize, C++ arithmetic operators in the following two parts:

Arithmetic Operator Type Meaning Example
Unary Operators Operates on single operands +a
Binary Operators Operates on double operands a+b

C++ Unary Operators

Operators that act on one operand are referred to as Unary Operators. Unary operators are of two types described in the following table:

Unary Operator Type Meaning
Unary Plus The operator unary '+' precedes an operand. The operand (the value on which the operator operates) of the unary + operator must have arithmetic or pointer type and the result is the value of the argument
Unary Minus The operator unary '-' precedes an operand. The operand of the unary - operator must have arithmetic type and the result is the negative of its operand's value

C++ Unary Plus Operator

Here is an example, showing the concept of unary plus operator in C++

if a = 5 then +a means 5
if a = 0 then +a means 0
if a = -5 then +a means -5

C++ Unary Minus Operator

This operator reverses the sign of the operand's value. Here is an example, showing the concept of unary minus operator in C++

if a = 5 then -a means -5
if a = 0 then -a means 0 (there is no quantity known as -0)
if a = -5 then -a means 5

C++ Binary Operators

Operators that act upon two operands are referred to as Binary Operators. The operand of a binary are distinguished as the left or right operand. Together, the operator and its operand constitute an expression. Here, the following table describes the types of binary operators in C++

Binary Operator Type Meaning
Addition The arithmetic binary operator + adds values of its operands and the result is the sum of the values of its two operands.
Subtraction The - operator subtracts the second operand from the first.
Multiplication The × operator multiplies the values of its operands.
Division The / operator divides its first operand by the second.
Modulus The % operator finds the modulus of its first operand relative to the second. That is, it produces the remainder of dividing the first by the second operand.

C++ Addition Operator

Here is an example, showing the concept of addition operator in C++

4 + 10 results in 14
a + 5(a=2) results in 7
a + b(a=4, b=6) results in 10

C++ Subtraction Operator

Here is an example, showing the concept of subtraction operator in C++

14 - 3 evaluates to 11
a - b(a=7, b=5) evaluates to 2

C++ Multiplication Operator

Here is an example, showing the concept of multiplication operator in C++

3 × 4 evaluates to 12
b × 4(b=6) evaluates to 24
a × c(a=3, c=5) evaluates to 15

C++ Division Operator

Here is an example, showing the concept of division operator in C++

100/5 evaluates to 20
a/2(a=16) evaluates to 8
a/b(a=159, b=3) evaluates to 5.3

C++ Modulus Operator

Here is an example, showing the concept of modulus operator in C++

19%6 evaluates to 1

Since 6 goes into 19 three times with a remainder 1. So, the above statement evaluates to 1.

C++ Arithmetic Operators Example

Let's take example program, to understand about the arithmetic operators in C++ practically and completely

/* C++ Operators - C++ Arithmetic Operators */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();

	int num1, num2;
	cout<<"Enter a positive number: ";
	cin>>num1;
	cout<<"Enter a negative number: ";
	cin>>num2;

	cout<<"\nIf:-\n\nnum1 = "<<num1<<"\tnum2 = "<<num2<<endl<<endl;
	cout<<"\nThen:-\n\nnum1 = "<<num1<<"\t";
	cout<<"num2 = "<<num2<<endl;
	cout<<"+num1 = "<<+num1<<"\t";
	cout<<"+num2 = "<<+num2<<endl;
	cout<<"-num1 = "<<-num1<<"\t";
	cout<<"-num2 = "<<-num2;

	getch();
}

Here is the sample run of the above C++ program:

c++ operators

Here is another program

/* C++ Operators - C++ Arithmetic Operators */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();

	int num1, num2, res;
	cout<<"Enter any two number: ";
	cin>>num1>>num2;
	res = num1 + num2;
	cout<<"\n";
	cout<<num1<<" + "<<num2<<" = "<<res<<endl;
	res = num1 - num2;
	cout<<num1<<" - "<<num2<<" = "<<res<<endl;
	res = num1 * num2;
	cout<<num1<<" * "<<num2<<" = "<<res<<endl;
	res = num1 / num2;
	cout<<num1<<" / "<<num2<<" = "<<res<<endl;
	res = num1 % num2;
	cout<<num1<<" % "<<num2<<" = "<<res<<endl;

	getch();
}

Here is the sample run of this C++ program:

c++ arithmetic operators

C++ Relational Operators

C++ provides six relational operators listed in the following table:

Relational Operator Name
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equal to
!= not equal to

The following table summarizes the action of these relational operators :

1 represents true and 0 represents false.
p q p < q p <= q p = = q p > q p >= q p != q
0 1 1 1 0 0 0 1
1 0 0 0 0 1 1 1
3 3 0 1 1 0 1 0
2 6 1 1 0 0 0 1

C++ Relational Operators Example

Here is an example program, demonstrating relational operators in a C++ program

/* C++ Operators - C++ Relational Operators */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	
	int p, q;
	int res;
	cout<<"Enter any two number: ";
	cin>>p>>q;
	
	cout<<"\n";
	cout<<"p    q    p<q    p<=q    p==q    p>q    p>=q    p!=q\n\n";
	
	res = p<q;
	cout<<p<<"    "<<q<<"    "<<res<<"      ";
	res = p<=q;
	cout<<res<<"       ";
	res = p==q;
	cout<<res<<"       ";
	res = p>q;
	cout<<res<<"      ";
	res = p>=q;
	cout<<res<<"       ";
	res = p!=q;
	cout<<res<<endl;
	
	getch();
}

Here are the four sample runs of the above C++ program. These sample runs shows operations performed on different-different values as shown in the above table

c++ relational operators

operators in c++

c++ bitwise operators

c++ operators with suitable examples

C++ Relational Operators More Examples

C++ Increment/Decrement Operator

Operator Name Meaning
++ Increment Operator Increments value by 1
-- Decrement Operator Decrements value by 1

The operator ++ adds 1 to its operand, and -- subtracts one. Let's look at the following statement

a = a + 1;

is same as

++a;

or,

a++;

and

a = a - 1;

is same as

--a;

or,

a--;

However, both the increment and decrement operators come in two varieties : they may either precede or follow the operand. The prefix version comes before the operand (as in ++a or --a) and the postfix version comes after the operand (as in a++ or a--). The two versions have the same effect upon the operand, but they differ when they take place in an expression. Let's discuss about this.

Working with prefix version

When an increment or decrement operator precedes its operand (i.e., in its prefix form), C++ performs the increment or decrement operation before using the value of the operand. Take a look at the following code fragment :

int sum = 10;
int count = 5;
sum = sum + ++count;

here after running the above code fragment, sum will have the value of 16 not 15 and count will have the value of 6, since in prefix version, C++ performs the increment or decrement operation before using the value of the operand as already told.

Note - The prefix increment operators follow change-then-use rule i.e., they first change (increment or decrement) the value of their operand, then use the new value in evaluating the expression.

Working with postfix version

When an increment or decrement follows its operand (i.e., in its postfix form), C++ first uses the value of the operand in evaluating the expression before incrementing or decrementing the operand's value. Take a look at the following code fragment :

int sum = 10;
int count = 5;
sum = sum + count++;

here after running the above code fragment, sum will have the value of 15 and count will have the value of 6, since in postfix version, C++ first uses the value of the operand in evaluating the expression before incrementing or decrementing the operand's value as already told.

Note - The postfix increment or decrement operator follow use-then-change rule i.e., they first use the value of their operand in evaluating the expression and then change (increment or decrement) the operand's value.

Important - The increment operator ++ and decrement operator -- are unary operators i.e., they operate upon single operand. And the postfix increment/decrement operators have higher precedence over the prefix increment/decrement operators.

Attention - According to Bjarne Stroustroup's book on "The C++ Programming Language", the postfix operator has higher precedence than prefix. But some implementations like TurboC++ implement it other way round.

C++ Increment/Decrement Operator Example

Here is an example program, demonstrating C++ increment/decrement operator

/* C++ Operators - C++ Increment/Decrement Operators */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();

	int num1=10, num2, num3;
	num2 = num1++;
	cout<<"Statements:-\nint num1=10, num2, num3;\n";
	cout<<"num2 = num1++;\n";
	cout<<"\nValues:-\n";
	cout<<"num2 = "<<num2<<"\tnum1 = "<<num1<<endl;

	num3 = ++num1;
	cout<<"\nStatement:-\nnum3 = ++num1;\n";
	cout<<"\nValues:-\n";
	cout<<"num3 = "<<num3<<"\tnum1 = "<<num1<<endl;

	num2 = num1--;
	cout<<"\nStatement:-\nnum2 = num1--;";
	cout<<"\n\nValues:-\n";
	cout<<"num2 = "<<num2<<"\tnum1 = "<<num1<<endl;

	num3 = --num1;
	cout<<"\nStatement:-\n";
	cout<<"num3 = --num1;\n";
	cout<<"\nValues:-\n";
	cout<<"num3 = "<<num3<<"\tnum1 = "<<num1<<endl;
	getch();
}

Here is the sample run of the above C++ program:

types of operators c++

C++ Logical Operators

C++ provides three logical operators listed in the following table:

Operator Name Meaning
|| Logical OR The logical OR operator (||) combines two expressions which make its operands. The logical OR ("||") operator evaluates to true i.e., 1 if either of its operands evaluate to true
&& Logical AND The logical AND operators, written as &&, also combines two expressions into one. The resulting expression has the value 1 (true) only if both of the original expressions (its operands) are true
! Logical NOT The logical NOT operator, written as !, works on single expression or operand i.e., it is a unary operator. The logical NOT operator (!) negates or reverses the truth value of the expression following it i.e., if the expression is true, then !expression is false, and vice-versa.

C++ Logical OR Operator

The expression

(6 <= 6) || (5 < 3)

returns 1 or true. Because, if one of the expression evaluates to true, then the resultant of the logical OR operator returns true or 1. From the above statements, the expression (6 <= 6) evaluates to true, then the whole expression becomes true.

C++ Logical AND Operator

The expression

(6 <= 6) && (5 < 3)

returns 0 or false. Because, expression returns true, only if both the expression returns true, but here the expression (5 < 3) returns false. Therefore, the whole expression becomes false and return 0 which is responsible for false.

C++ Logical NOT Operator

The expression

!(6 <= 6)
!(5 > 9)

returns 0 and 1. The expression (6 <=6) is true, but on adding the logical NOT operator, the expression becomes false, (return 0). And the expression, (5 > 9) is false, but logical NOT operator reverses it and makes it true. So this will return 1 or true.

C++ Logical Operators Example

Let's take an example program, for the complete understanding on the logical NOT operator in C++

/* C++ Operators - C++ Logical Operator */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();

	int res;

	res = (6 <= 6) || (5 <3);
	cout<<res<<endl;

	res = (6 <= 6) && (5 < 3);
	cout<<res<<endl;

	res = !(6 <= 6);
	cout<<res<<endl;

	res = !(5 > 9);
	cout<<res<<endl;

	getch();
}

Here is the sample output of the above C++ program:

c++ operator

C++ Conditional Operator

The conditional operators is used to store a value depending upon a condition. This operator is ternary operator i.e., it requires three operands. Here is the general form of the C++ conditional operator (?:)

expression1 ? expression2 : expression3

If expression1 evaluates to true i.e., 1 then the value of the whole expression is the value of expression2. Otherwise, the value of the whole expression is the value of the expression3. For example,

6 > 4 ? 9 : 7  evaluates to 9 because test expression 6 > 4 is true
4 == 9 ? 10 : 25  evaluates to 25 because test expression 4 == 9 is false

C++ Conditional Operator Example

Here is an example program, demonstrating the concept of C++ conditional operator

/* C++ Operators - C++ Conditional Operator */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();

	int res;

	res = (6 <= 6) || (5 <3);
	cout<<res<<endl;

	res = (6 <= 6) && (5 < 3);
	cout<<res<<endl;

	res = !(6 <= 6);
	cout<<res<<endl;

	res = !(5 > 9);
	cout<<res<<endl;

	getch();
}

Here is the sample output of the above C++ program:

c++ conditional operator

C++ Bitwise Operators

Bitwise operator works on bits and perform bit-by-bit operation. The following table lists the bitwise operators supported by C++

Operator Name Meaning
| Binary OR Operator Copies a bit if it exists in either operand
& Binary AND Operator Copies a bit to the result if it exists in both operands
^ Binary XOR Operator Copies the bit if it is set in one operand but not both
~ Binary Ones Complement Operator This is unary and has the effect of 'flipping' bits.

C++ Bitwise Operators Truth Table

The truth tables for &, |, and ^ are as follows:

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

C++ Bitwise AND Operator

  A = 0011 1100 (60)
& B = 0000 1101 (13)
-----------------
      0000 1100 (12)

C++ Bitwise OR Operator

  A = 0011 1100 (60)
| B = 0000 1101 (13)
-----------------
      0011 1101 (61)

C++ Binary XOR Operator

  A = 0011 1100 (60)
^ B = 0000 1101 (13)
-----------------
      0011 0001 (49)

C++ Bitwise Operators Example

Here is an example program, demonstrating the concept of bitwise operators in C++

/* C++ Operators - C++ Bitwise Operators */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();

	unsigned int a = 60;	  // 60 = 0011 1100
	unsigned int b = 13;	  // 13 = 0000 1101
	int c = 0;

	c = a & b;             // 12 = 0000 1100
	cout<<"a = 0011 1100 (60)\tand\tb = 0000 1101 (13)\n\n";
	cout<<"a & b = "<<c<<endl;
	c = a | b;             // 61 = 0011 1101
	cout<<"a | b = "<<c<<endl;

	c = a ^ b;             // 49 = 0011 0001
	cout<<"a ^ b = "<<c<<endl;

	c = ~a;                // -61 = 1100 0011
	cout<<"~a = "<<c<<endl;

	getch();
}

Here is the sample run of the above C++ program:

c++ bitwise or not xor and operators

Operators Precedence in C++

Highest Associativity
() [] -> . ++(post) --(post) Left to right
+ - ! ~ ++(pre) --(pre) (type)* & sizeof Right to left
* / % Left to right
+ - Left to right
<< >> Left to right
< <= > >= Left to right
== != Left to right
& Left to right
^ Left to right
| Left to right
&& Left to right
|| Left to right
?: Right to left
= += -= *= /= %= >>= <<= &= ^= |= Right to left
, Left to right
Lowest

Let's take an example, demonstrating operator precedence uses in a C++ program:

/* C++ Operators - Operator Precedence in C++ */

#include<iostream.h>
#include<conio.h>

void main()
{
	clrscr();

	int a = 20, b = 10, c = 15, d = 5, res;

	cout<<"a=20\tb=10\tc=15\td=5\n\n";

	res = (a + b) * c / d;
	cout<<"(a + b) * c / d = "<<res<<endl;

	res = ((a + b) * c) / d;
	cout<<"((a + b) * c) / d = "<<res<<endl;

	res = (a + b) * (c / d);
	cout<<"(a + b) * (c / d) = "<<res<<endl;

	res = a + (b * c) / d;
	cout<<"a + (b * c) / d = "<<res<<endl;

	getch();
}

Here is the sample run of the above C++ program:

operator precedence c++

More Examples

Here are some more C++ programs listed, that you may like:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test