Loading....
Coupon Accepted Successfully!

 

Operators in C and C++

Operator is an instruction to the compiler or interpreter specified by a single or double symbol to perform certain operation with constants.

 

Example:

5+10


Here, 5 and 10 are constants. The symbol ‘+’ is an operator that indicates the operation to be performed. The ‘+’ performs addition of numbers. The ‘+’ is a single operator.

 

int x = 5;

++x;


In the above example, the operator ++ is an increment operator. This operator adds one to the value of operand.


The types of operators (Figure) with their symbols are described in Table 4.7.

 

Description: 132186.png

Types of operators
 

Table 4.7 Types of Operators

Type of Operators

Symbolic Representation

Arithmetic operators

+, −, *, /, and %

Relational operators

>, <, = =, >=.<=, and !=

Logical operators

&&, ||, and !

Increment and decrement operators

++ and −−

Assignment operators

=,+=,−=,*=,/=,<<=,>>=,&=,|=,^=

Bit wise operators

&, |, ^, >>, <<, and ~

Special operator

,

Conditional operators

? and :


C++ supports all operators of ‘C’ and C++ introduces new additional operators. The new operators are listed in Table 4.8.

 

Table 4.8 Operators in C++

Operators

Description

<< 

Insertion operator

>> 

Extraction operator

::

Scope access (or resolution) operator

::*

Pointer to member decelerator

−>*

Deference pointers to pointers to class members

.*

Deference pointers to class members

delete

Memory release operator

new

Memory allocation operator


The scope access (or resolution) operator :: (two colons) allows you to access a global (or file duration) name even if a local hides it redecoration of that name.

 

The .* and −>* operators represents deference pointers to class members and deference pointers to pointers to class members, respectively.

Precedence of Operators in C++

In the following operator precedence Table 4.9, the C++ operators are classified into 13 groups. The #1 group has the top (highest) precedence, group #2 (unary operators) takes second precedence, and so on to the comma operator, which has lowest precedence. The precedence of operator in C++ is shown in Table 4.9.

The operators within each category have equal precedence.

The unary (group #2), conditional (group #11), and assignment (group #12) operators associate right-to-left; all other operators associate left-to-right.
 

Table 4.9 Precedence of Operators in C++

# Group

Operator

Operation

1. Top

()

[]

−>

::

.

Function call

Array subscript

C++ indirect component selector

C++ scope access/resolution

C++ direct component selector

2. Unary

!

~

+

++

−−
&
*
sizeof
new
delete

Logical negation (NOT)

Bitwise (1’s) complement

Unary plus

Unary minus

Pre-increment or post-increment

Pre-decrement or post-decrement
Address
Indirection
Returns size of operand, in bytes
Dynamically allocates C++ storage
Dynamically de-allocates C++ storage

3. Multiplicative

*

/

%

Multiply

Divide

Remainder (modulus)

4. Member Access

.*

−>*

C++ dereference

C++ deference

5. Additive

+

Binary plus

Binary minus

6. Shift

<< 

>> 

Shift left

Shift right

7. Relational

<=

>=

Less than

Less than or equal to

Greater than

Greater than or equal to

8. Equality

==

!=

Equal to

Not equal to

9. Bitwise

&

Bitwise AND

 

^

Bitwise XOR

 

|

Bitwise OR

10. Logical

&&

Logical AND

 

||

Logical OR

11. Conditional

?:

(a ? x : y means “if a then x, else y”)

12. Assignment

=

*=

/=

%=

+=

-=

&=

^=

|=

<<=

>>=

Simple assignment

Assign product

Assign quotient

Assign remainder (modulus)

Assign sum

Assign difference

Assign bitwise AND

Assign bitwise XOR

Assign bitwise OR

Assign left shift

Assign right shift

13. Comma

,

Evaluate


All of the operators in this table can be overloaded except the following:

 

. C++ direct component selector
.* C++ dereference
:: C++ scope access/resolution
?: Conditional

Precedence of * and [ ] Operators

In C++, the statements * x[4] and (* x) [4] are not same because the * operator having lower precedence than the [ ] operator. Consider the following examples.

 

int *arr[5];


The above statement declares an array of five pointers and the following operation is invalid because the array name itself is an address and it is a constant. Hence, cannot be changed.

 

arr++; or ++arr;

 

int (* arr)[5]


The above declaration declares a pointer to an array of five elements. Hence, the operations such as arr++ and ++arr are not supported. The following program explains both these conditions.


4.21 Write a program declare a pointer to array and display the elements.

 

Explanation: In the above program, an integer array a[3][3] is declared and initialized. The base address of array and number of rows are passed to the function display(). In function display(), d is a pointer. The base address received by the variable x is assigned to pointer d. Using nested for loops, the elements of array are displayed.





Test Your Skills Now!
Take a Quiz now
Reviewer Name