# C++ operators and how to use these operators in C/C++ programming ## Description:

C/C++ operators- In this tutorial, we learn what is operators and how to use these operators in c/c++ programming with a full explanation.

## Arithmetic operators  in C / C++:

The arithmetic operators are the symbols that represent arithmetic operations. These are used in arithmetic expressions. Each arithmetic operators operates upon two numeric values( constants and variables ) and returns a value.

The following arithmetic operators are used in c/c++:

 Operators Meaning + Addition _ Subtraction * Multiplication / Division % For reminder

The ‘ + ’ and ‘ – ‘ symbols are quite familiar to you. The ‘ + ’ symbol indicates addition. Whereas ‘ – ’ symbol indicates subtraction.

The ‘ * ’ (asterisk ) is used in c/ c++ to indicate multiplication.

The ‘ / ’ (slash) is used to indicate both real and integer division. If both operands in division operators are integers the result will be an integer. For example:

5/3 —–>1

3/5 —–>0

Whereas on the other hand real division is performed producing the real result if both or either of the operand is real for example:

5.0 / 2.0 —–> 2.5

5 / 3.0 ——–> 1.666667

3.0 / 4 ——> 0.75

In the division operator, the second operand must be non zero.

The percent sign ( % ) produces a modulus or a reminder of an integer division. % computes the arithmetic remainder after a first integer value is divided by a second integer value. for modulus operator, both operands must be an integer and the second operand must be non zero otherwise it will not work.

2 % 2 ——-> 0

3 % 2 ——-> 1

11 % 3—–> 2

2 % 10 —-> 2

5 % 10 —-> 5

All arithmetic operators except the remainder operator are used for all types of numeric data.

## Increment ( ++ ) and Decrement ( — ) c/c++ operators:

C / C++ supports the ++ and – operators to increment and decrement variables by on respectively. The increment operator ( ++ ) is used to add 1 to variable and decrement operator ( — ) is used to subtract 1 from variable. These operators can use either side of the variable. So let’s discuss these operators in detail.

### Increment ( ++ ) operator:

The increment operator is represented by a double plus (++) sign. It is used to add 1 to the value of an integer variable. This operator can be used before or after the variable name.

For example:

If we want to add 1 to a value of variable ab it is normally written as

ab = ab+1;

by using the increment operator “ ++ ” it is written as

ab++;

the increment operator can be written either before or after the variable. If it is written before the variable it is known as prefixing. If it is written after the variable it is known as postfixing. Prefix and postfix operators have a different effects when they are used in expression.

#### Prefix increment operator:

When an increment operator is used in prefix mode in an expression it adds 1 to the value of the variable before the value of the variable is used in the expression.

##### Example how to use prefix increment operators in c++ programming:

In the above program, 1 will be added to the value of c before it is used in the expression thus after execution the sum will be equal to 9 and the value of c will be 4.

#### Postfix increment operator:

When an increment operator is used in postfix mode in an expression it adds 1 to the value of the variable after the value of the variable has been used in the expression. For example if in the above example increment operator is used in postfix mode the result will be different the statement will be as shown below:

Sum= a + b + c + c++;

In this case, 1 will be added to the value f c after its existing value has been used in the expression. Thus after execution, the sum will be equal to 5 and the value of c will be 4

### Decrement ( — ) operators:

The decrement operator is represented by double minus ( — ) sign it is used to subtract 1 from the value of an integer variable.

For example, to subtract 1 from the value of a variable ab the decrement statement is written as

Ab–;

Or

-ab;

#### Prefix decrement operator:

When a decrement operator is used in prefix mode in an expression it subtracts 1 from the value of the variable before the value of the variable is used in the expression.

##### Example how to use prefix decrement operators in c++ programming:

In the above program, 1 will be subtracted from the value of c before it is used in the expression thus after execution the sum will be equal to 7 and the value of c will be 2.

#### Postfix decrement operator:

When a decrement operator is used in postfix mode in an expression it subract 1 from the value of the variable after the value of the variable has been used in the expression. For example if in the above example decrement operator is used in postfix mode the result will be different the statement will be as shown below:

Sum= a + b + c + c–;

In this case, 1 will be subtracted from the value f c after its existing value has been used in the expression. Thus after execution, the sum will be equal to 5 and the value of c will be 2.

## Assignment operators ( = ):

The assignment operator ( = ) is used to assign value to variables. It is used to assign a numeric or character value to a variable. However, string data can ot be assigned to a variable with this operator.

The assignment operator evaluates an expression and then assigns its value to a variable. The assignment operator ( = ) is used to assign the calculated value to a variable. The expression is written on the right side of the assignment operator. The variable to which the value of the expression is to be assigned is written on the left side of the assignment operator.

The syntax of the assignment operator is

Var= expression;

Where:

Var:

The variable name to which the value of the expression is assigned. The variable type must be the same as the type of value returned by the expression.

Expression:

An expression may be a combination of operands (variable and constants ) and arithmetic operators.

A character can also be assigned but a string cannot be assigned to a string variable.

## Relational c++ operators:

Relational operators are used to form conditions. These are used to write a conditional expression that describes the relationship between two values .these operations allow us to compare two values to see whether they are equal to each other unequal or whether one is greater than the other or vice versa. Relational operators return the 1 (true) or 0 (False). Don’t confuse the assignment operator ( = ) with the equals relational operator (==) the six relational operators in c++ are:

### Greater than ( > ) :

It is used to test if one expression is greater than the other expression. For example, if exp1 and exp2 are two expression then

exp1 > exp2

if exp1 greater than exp2 then this expression returns a true value. if exp1 is less than exp2 then this expression returns a false value.

### Greater than or equal to ( > =) :

It is used to test if one expression is greater than  or equal to the other expression. For example, if exp1 and exp2 are two expressions then

exp1 >= exp2

if exp1 greater than or equal to  exp2 then this expression returns a true value. if exp1 is less than exp2 then this expression returns a false value.

### less than ( < ) :

It is used to test if one expression is less than the other expression. For example, if exp1 and exp2 are two expressions then

exp1 < exp2

if exp1 less than exp2 then this expression returns a true value. if exp1 is greater than exp2 then this expression returns a false value.

### less than or equal to ( < =) :

It is used to test if one expression is less than  or equal to the other expression. For example, if exp1 and exp2 are two expression then

exp1 <= exp2

if exp1 less than or equal to  exp2 then this expression returns true value. if exp1 is greater than exp2 then this expression returns a false value.

### Equal to ( ==) :

It is used to test if one expression is equal to the other expression. For example if exp1 and exp2 are two expression then

exp1 == exp2

if exp1 equal to  exp2 then this expression returns true value. if exp1 is not equal to (less than or greater than ) exp2 then this expression returns false value.

### not Equal to ( !=) :

It is used to test if one expression is not equal to the other expression. For example if exp1 and exp2 are two expression then

exp1 != exp2

if exp1 is not equal to (less than or greater than)  exp2 then this expression return true value. if exp1 is equal to exp2 then this expression returns false value.

## Logical C++ Operators:

More than one condition can be combined and tested in an if statement by connecting them with logical operator &&(AND),  ||(OR),  and ! (NOT). Such condition expression are referred to as Boolean expression complex or compound expression.

A compound expression with && logical expression is true of both of the relational expression are true otherwise it is false for examples:

If((marks > 70) &&  (marks < 80))

Cout<<”garde is A”;

Suppose if marks is 75 the expression ((marks >= 70) && (marks < 80)) is true if marks has the value  60 the expression ((marks >= 70) && (marks < 80)) is false.

The compound expression with (||) logical operation is true if any of the conditions is/are true and false only in the one case when all condition are false. For example:

If((choice == ‘Y’) || (choice == ‘y’))

Goto top;

Thus the logical expression ((choice == ‘Y’) || (choice == ‘y’)) is true if either of the relational expressions is true otherwise it is false.

These definitions are summarized in the following truth table which displays app possible values for two decision making condition X and Y and the corresponding values of the logical expression.

 X Y (X&&Y) (X||Y) true true true True true False False True False True False True False False False False

now we discuss each of them in detail. There are three type of logical operators in c++ which are:

1. &&—–>AND Operator
2. ||  —-> OR    Operator
3. !——>  NOT  Operator

### The && (AND) Operator in c++:

The && (AND ) operator is used to combine two or more relational expressions. If all relational expression are true then the output returned by the compound expression is true. If anyone relational expression in the compound expression is false the output is false.

### The || (OR) Operator:

The || (OR) operator is used to combine more than one relational expression. if anyone of the given relational expressions is true the output will be true otherwise the output will be false.

### The ! (NOT) Operator:

The ! (NOT ) operator is also known as the unary operator. It inverts the value returned by the relational expression or the compound expression.

## Bitwise C/C++ Operators

The bitwise c++ operators are used to access the computer hardware. Use of bitwise operators requires knowledge of different number system and conversion numbers from one system into another number system. The data inside the computer is represented in binary form, i.e. in a sequence of 0s and 1s. 8-bits are used to represent one character inside the computer. The bitwise c++ operators are used to manipulate the data inside the computer. These are also used in networking software to move data from one computer to another in the network. The commonly used bitwise operators are given below.

### The Bitwise & (AND)  c++ Operators:

The bitwise AND operator is used to compare the contents of rue operands (of int data type) on bit-by-bit basis. It returns 1 if he corresponding bit in both the operands is l otherwise it returns 0.

### The Bitwise | (OR) c++ Operators:

The bitwise OR operator is used to compare the contents of two operands (of int data type) on bit by bit basis. It returns 1 of any one of the two bits is 1, otherwise it returns 0. It is represented by the vertical bar ( | ).

### The Bitwise ^ (XOR) C Operators:

The bitwise XOR operator is used to compare the contents to two operands (of int data type) one bit-by-bit basis. It returns 1 if the bits in the corresponding operands are different otherwise it returns 1 (if corresponding bits are same). It is represented by the caret ( ^ ) sign.

### The Bitwise ~ (complement) C Operators:

The bitwise complement operator is used to operate on an expression. It invert the bits of the expression. It takes each bit of the operand and converts it to 1 if it is a 0 an to 0 it if is 1. It is  represented by the tilde ( ~ ) sign.

### The Left-Shift (<<) c++ Operators

The left shift operator is used to shift a specified number of bits of an operand to the left. It has two operands. The first operand on the left hand-side of the operator is the constant or variable the operator is the constant or variable whose bits are to be shifted. The second operand on the right-hand-side of the operator specified the number of bits that are to be shifted to the left. The right shift operator is represented by the (<<) sign

### The Right-Shift (>>) Operators :

The right shift operator is used to shift a specified number of bits of an operand to the right. It has two operands. The first operand at the left hand side of the operator is the constant or variable whose bits are to be shifted. The second operand at the right hand side specified the number of bits that are to be shifted to the right.when the specified number of the bits of the operand are shifted to the right then the same number of bits on the left are filled with 0s. The right shift operator is represented by the (>>) sign.

to understand what is Bitwise c++ operators in detail click on below link

## The ‘sizeof’ c Operators:

In c / c++ data are mainly divided into three different types. These are:

Character data type:

Characters (letters digits and punctuation character) are represented by the char data type the short data type is the same size as char usually on byte.

Integer data type:

Integer data (whole numbers) are represented by int and long.

Real data type:

Numbers including decimal fractions are represented by float double or long double.

to find the size of these data type in c++ we have an operator which called sizeof operator.

The c operator sizeof is used to display the size in bytes of the data or data type. It has the following general format:

sizeof data

Or

sizeof (data type)

## Comma c++ operators:

In the below program example comma operator is used comma operator is also known as sequence point operator. It is used to put more than one expression. on a single line separated by commas as used in the below program.

## Priority of c++ operators:

If an expression has two or more operators then there is a certain order in which these operations are performed by the computer. This order is referred to as the order of precedence. The precedence order is given below

1. All parentheses are evaluated first.
2. Multiplication division and modulus operators have performed first these operators have the same level of precedence they are performed in left to right as they are encountered in an arithmetic expression. further, if an expression contains several like operations evaluation also proceeds from left to right.
3. Addition and subtraction will be performed next. They also have same level of precedence so performance is given to the operator which occurs first.

Parentheses are used to change the priority order because c/c++ evaluates the expression in parentheses first if parentheses are nested then evaluation begins with the innermost pairs and works outward.