- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Data Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Programming Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library Functions
- C++ Standard Library Functions
- C++ Header Files
- C++ Character String Functions
- C++ Mathematical Functions
- C++ Programming Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype Definition
- C++ Function Call
- C++ Function Return
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented Programming
- C++ Object Oriented Programming
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- Register Now
- Login Page
- All Test List

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.

C++ provides the following types of operators:

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Increment-Decrement Operators
- Conditional Operators
- Bitwise Operators

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

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 |

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 |

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

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

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

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

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

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

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

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.

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:

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

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 |

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

- Check Even or Odd
- Check Prime or Not
- Check Alphabet or Not
- Check Vowel or Not
- Check Leap Year or Not
- Check Palindrome or Not
- Check Armstrong or Not
- Check Reverse equal Original
- Find Largest of two Numbers
- Find Largest of three Numbers

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.

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.

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.

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:

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

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.

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.

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.

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:

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

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:

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

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 |

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

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

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

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:

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:

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

- Print Table of Number
- Print Prime Numbers
- Reverse Numbers
- Swap two Numbers
- Find Factorial of Number
- Find HCF LCM
- Fahrenheit to Centigrade Conversion
- Centigrade to Fahrenheit Conversion
- Print Fibonacci Series
- Check Palindrome or Not
- Check Armstrong or Not
- Generate Armstrong Numbers
- Find ncR nPr
- Decimal to Binary Conversion
- Decimal to Octal Conversion
- Decimal to Hexadecimal Conversion
- Binary to Decimal Conversion
- Binary to Octal Conversion
- Binary to Hexadecimal Conversion
- Octal to Decimal Conversion
- Octal to Binary Conversion
- Octal to Hexadecimal Conversion
- Hexadecimal to Decimal Conversion
- Hexadecimal to Binary Conversion
- Hexadecimal to Octal Conversion
- Pattern Programs
- Print Diamond Pattern
- Print Floyd Triangle
- Print Pascal Triangle