C++ - Unary Operators
Unary operators are those operators which acts upon a single operand to produce a new value. Types of unary operators in C++ are as follows:
- Unary minus operator (-)
- Unary plus operator (+)
- Increment operator (++)
- Decrement operator (--)
- NOT operator (!)
- Bitwise NOT operator (~)
- Addressof operator (&)
- Dereference operator (*)
- sizeof() operator
- new operator (new and new[])
- delete operator (delete and delete[])
- cast operator ()
- Unary minus operator
It is used change the sign of the operand which means a negative number becomes positive and a positive number becomes positive.
int a = 10; int b = -a; //b = -10
- Unary plus operator
It returns the value same as the operand.
int a = 10; int b = +a; //b = 10
- Increment operator
It is used to increase the value of operand by 1. The operator can be used in two ways.
- Pre-increment: The operator precedes the operand and the value of the operand is increased by 1 before it is used.
int a = 10; int b = ++a; //b = 11
- Post-increment: The operator follows the operand and the value of the operand is increased by 1 after it is used.
int a = 10; int b = a++; //b = 10 int c = a; //c = 11
- Pre-increment: The operator precedes the operand and the value of the operand is increased by 1 before it is used.
- Decrement operator
It is used to decrease the value of operand by 1. The operator can be used in two ways.
- Pre-decrement: The operator precedes the operand and the value of the operand is decreased by 1 before it is used.
int a = 10; int b = --a; //b = 9
- Post-decrement: The operator follows the operand and the value of the operand is decreased by 1 after it is used.
int a = 10; int b = a--; //b = 10 int c = a; //c = 9
- Pre-decrement: The operator precedes the operand and the value of the operand is decreased by 1 before it is used.
- NOT operator
It is used to reverse the logical state of its operand. The operator converts a TRUE condition into FALSE and vice-versa.
int x = 10; bool y = (x > 5); // y = TRUE y = !(x > 5); // y = FALSE
- Bitwise NOT operator
It is used to change each bit to its opposite - 0 becomes 1 and 1 becomes 0.
int x = 18; // binary: 0000000000010010 int y = ~x; // binary: 1111111111101101
- Addressof operator
It is used to return the memory address of a variable. The returned value is also known as pointer as it points to the variable in memory address.
int x = 10; int *p; // pointer declaration p = &x; // address of x is copied to pointer p
- Dereference operator
It is used to return the value stored in the address pointed by the pointer.
int x = 10; int *p; // pointer declaration p = &x; // address of x is copied to pointer p int y = *p; // gives the value stored in the address.
- sizeof operator
It is used to return the size of the operand in bytes.
int x = 10; int y = sizeof(x); // size of int is 4 bytes
- new operator
It is used to initialize the memory and return the address of the newly allocated and initialized memory to the pointer variable.
//pointer declared with assignment int *p1 = new int; //pointer initialized with 10 int *p2 = new int(10); //allocate array of size 10 of memory of type int int *p3 = new int[10];
- delete operator
It is used to deallocate dynamically allocated memory.
delete p1; //release pointer p1 delete[] p2; //release block of memory p2
- cast operator
It is used to convert the value of one data type to another data type.
float x = 10.5; int y = int (x); // functional notation int z = (int) x; // c-like cast notation