C++ Unary Operators

The unary operators take a single argument in C++ language. Some of the operators you have already seen in previous articles are unary operators.

Here is the list of unary operators.

Advertisements
Unary OperatorsDescription
*Dereference operator
&Address Of operator
Minus sign
!Not  operator
~Complement
++Increment operator
Decrement operator
typeForced type conversion
sizeofSize of the data type
Table 1 – Unary Operators

Dereference Operator (*)

When a C++ pointer type is declared, and it is pointing to a variable. The dereference operator helps to access the content of a pointer type. We will learn more about while discussing pointers.

Address Of Operator (&)

It resembles the bitwise AND, but when used as a unary operator it returns the address of the variable.

int A = &B;

The variable A will store address of the variable B, not its value.

Minus Sign (-)

Minus is an arithmetic operator, but as a unary operator, it is used to denote a negative value. It is most commonly used the unary operator in C++ programs.

Negation Operator(!)

The negation is a logical operator which negates the value of a boolean variable. If the value is 1 (true) then, it becomes 0 (false). If the value is 0 (false) then, it becomes 1 (true).

Bitwise Complement (~)

The bitwise complement modifies the binary value of a variable to 2‘s complement binary value. In other words, you get a negative value for a positive number plus 1.

\begin{aligned}p = 45  \hspace{5px} becomes \hspace{5px} -46 \hspace{5px} after \hspace{5px} \sim p.\end{aligned}
Advertisements

Increment Operator (++)

The increment operator is used in loop structures. You will learn about loop structures later in this C++ tutorial. The increment operator increments an associated variable by 1.

i = 12;
i++; // same as saying i = i + 1;

The value of i after increment is 13.

Decrement Operator (–)

The decrement operator decreases the associated variable value by 1.

i = 34;
i--;

The value will be 33 after decrement.

Type Operator

The type conversion operator is used to modify the data type of a variable by force. The initial data type is changed to what specified in the type operator. It is also known as cast operator.

int A;
A = 10;
(float) A;

This will change the variable A into floating type from an integer.

Sizeof Operator( sizeof)

The sizeof operator is a special operator that returns the size of the data type in C++ program.

int A;
sizeof(A);

This will return the size of integer data type which is 4 bytes.

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.