Unary Operators in C++



A unary operator is operators that act upon a single operand to produce a new value, It manipulates or modifies only the single operand or value. It is used to perform operations like incrementing, negating, or changing the sign of a value.

The term "unary" is derived as these operators require only one operand to perform an operation. Which makes it different from binary operators, that requires two operands to perform.

List of Unary Operators

Operator Description Example Result
Unary plus operator (+) This indicates a positive value, which results in no actual change in the operand. x = +5 x = 5
Unary negation operator (-) This negates the value of the operand, which changes the sign of an operand. x = -(+5) x = -5
Increment operator (++) This operator adds one to its operand, which results in an increment of operand by one. x = 5;
x++
x = 6
Decrement operator (--) This operator subtracts one from its operand, which results in a decrement of the operand by one. x = 5;
x--
x = 4
Logical NOT/ Logical negation operator (!) This operator reverses the meaning of its operand. The operand must be of arithmetic or pointer type (or an expression that evaluates to arithmetic or pointer type). The operand is implicitly converted to type bool. x = True;
!x
x = False
Bitwise NOT Operator (~) The Bitwise NOT is sometimes also called the "bitwise complement" or one's complement operator. x = 5 (binary 0101);
~x
x = -6 (0101 + 1 = 0110)
Indirection operator (*)/ Dereference It operates on a pointer variable and returns an l-value equivalent to the value at the pointer address. Which is also called dereferencing the pointer. *p = 5 Dereference and assign value
Address-of operator (&) This operator takes the address of its operand. int* ptr = &a; ptr = address of a
Cast operator () This operator provides a method for explicit conversion of the type of an object in a specific situation. int i = (int) 9.57; i = 9
sizeof operator It is a compile-time unary operator which is used to compute the size of its operand. sizeof(int) 4 bytes
new operator It is a memory allocation operator that is used to allocate memory dynamically. int* ptr = new int; Allocates memory
delete operator It is a memory allocation operator that is used to deallocate memory that was dynamically allocated. delete ptr; Frees the memory allocated

Unary Arithmetic Operators in C++

In C++, Unary arithmetic operators, are the operators, that operate on a single operand to perform arithmetic operations like changing signs, incrementing, or decrementing.

Here is the following list of unary arithmetic operators in C++:

  • Unary Plus (+)
  • Unary Minus (-)
  • Increment (++)
  • Decrement (--)

Unary Plus (+) Operator

This operator doesn't change the value of the operand, it just simply returns the operand as it is, whether the operand is negative or positive.

Syntax

+operand;

Example Code

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    int y = -4;
    cout << "Returning values using Unary plus" << endl;
    cout << +x << " " << +y << endl;
    return 0;
}

When the above code is compiled and executed, it produces the following result −

Returning values using Unary plus
5 -4

Unary Minus (-) Operator

The Unary minus or Unary negation operator is used to change the sign of the operand. If the operand is negative, it will become positive, and vice versa, where the operand can have any arithmetic type.

Syntax

-operand;

Example Code

#include 
using namespace std;

int main() {
    int x = 6;
    int y = -12;
    cout << -x << " " << -y << endl;
    return 0;
}
    

When the above code is compiled and executed, it produces the following result −

-6 12

Increment (++) Operator

The increment operator in C++ is used to increase the value of the operand by one. It has two forms:

  • Prefix increment (++x): First increases the value of the operand, then assigns the new value.
  • Postfix increment (x++): First assigns the value, then increments the operand.

Syntax for Prefix increment

++operand;

Syntax for Postfix increment

operand++;

Example Code

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    // Prefix increment
    int y = ++x;  // x is incremented first, then assigned to y
    cout << "Prefix increment:" << endl;
    cout << "x = " << x << ", y = " << y << endl;  

    int a = 5;
    // Postfix increment
    int b = a++;  // b is assigned the value of a first, then a is incremented
    cout << "Postfix increment:" << endl;
    cout << "a = " << a << ", b = " << b << endl;  
    return 0;
}

When the above code is compiled and executed, it produces the following result −

Prefix increment:
x = 6, y = 6
Postfix increment:
a = 6, b = 5

Decrement (--) Operator

The decrement operator in C++ is used to decrement the value of an operand by one and also has two forms:

  • Prefix decrement (--x): First decreases the value of the operand, then assigns the new value.
  • Postfix decrement (x--): First assigns the value, then decrements the operand.

Syntax for Prefix decrement

--operand;

Syntax for Postfix decrement

operand--;

Example Code

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    int y = --x;  // First, x is decremented to 4, then assigned to y
    cout << "x = " << x << " y = " << y << endl;

    int a = 5;
    int b = a--;  // First, a is assigned to b, then decremented to 4
    cout << "a = " << a << " b = " << b << endl;
    return 0;
}

When the above code is compiled and executed, it produces the following result −

x = 4 y = 4
a = 4 b = 5

Unary Logical Operators

Unary logical operators in C++, are operators which deal with Boolean values, here it reverses the meaning of their operand. The operand must be of arithmetic or pointer type (or an expression that evaluates to arithmetic or pointer type). Here the operand is implicitly converted to type bool.

Syntax

!operand;

Example Code

#include <iostream>
using namespace std;

int main() {
    int x = 10;   // non-zero value, considered True
    int y = 0;    // zero, considered False

    cout << "!x = " << !x << endl;  // x is True, so !x will become false (0)
    cout << "!y = " << !y << endl;  // y is False, so !y will become true (1)

    return 0;
}

When the above code is compiled and executed, it produces the following result −

!x = 0
!y = 1

Bitwise NOT Operator (~)

Unary bitwise NOT also known as a bitwise complement or one's complement operator in C++ is used to perform bit-level operations. This manipulates and operates on individual bits of a variable, and produces the bitwise one's complement of its operand. The operand must be of integral type.

Syntax

~operand;

Example Code

#include <iostream>
using namespace std;

int main() {
    int x = 5;  
    // In binary: 0000000000000101 (16-bit representation)

    // Applying the Unary Bitwise NOT (~) operator
    int result = ~x;

    // Output the result
    cout << "x = " << x << endl;       
    cout << "~x = " << result << endl; 

    return 0;
}

When the above code is compiled and executed, it produces the following result −

x = 5
~x = -6

Explanation

Firstly, x is assigned the value 5, in binary 5 is represented as 0000000000000101 (16-bit signed integer representation).
Applying the Unary Bitwise NOT (~) operator flips each bit of the operand, where 0 becomes 1 and 1 becomes 0.

  • x = 5 => 0000000000000101 (binary of 5)
  • ~x = -6 => 1111111111111010 (bitwise complement of 5)

When working with signed integers in two's complement, the most significant bit (leftmost bit) determines the sign of the number.

  • When it's 0, the number is positive.
  • When it's 1, the number is negative.

So, The binary number 1111111111111010 will give a negative decimal number.
Now, to check its decimal number, add 1, which equals 0000000000000110.
So, the result is -6.

Pointer Operators

Pointer operators are used to work with pointers in C++, these two pointer operators come under unary because it operates only in single operands.

Dereference (*) Operator

Dereference also known as an Indirection operator, operates on a pointer variable, which accesses the value stored at the address by a pointer and returns the value of the object that the pointer is pointing to.

Syntax

*pointer;

Address-of (&) Operator

Whereas, the Address-of operator returns the memory address of a variable. It returns the address memory where that variable is located.

Syntax

&operand;

Example Code

#include <iostream>
using namespace std;

int main() {
    int num = 10;
    // Using the Address-of operator to get the address of num
    int* ptr = #  // ptr now holds the address of num

    // Using the Dereference operator to access the value stored at 
    cout << "The value of num: " << num << endl;        
    cout << "The address of num: " << &num << endl;          
    cout << "The address stored in ptr: " << ptr << endl;      
    cout << "The value pointed to by ptr: " << *ptr << endl;
    return 0;
}

When the above code is compiled and executed, it produces the following result −

The value of num: 10
The address of num: 0x7fff08fa2814
The address stored in ptr: 0x7fff08fa2814
The value pointed to by ptr: 10

Typecast Operator (C++ Style)

The Typecast operator in C++ is a method used for explicit conversion of one data type into another.

Here, C++ provides several methods for type casting, including C and C++-style casts:

  • static_cast: It is used for conversion between related types (e.g. int to float, float to double, char to int, and derived class to base class).
  • dynamic_cast: It is used for downcasting in polymorphic class hierarchies, with runtime checking.
  • const_cast: It is used to add/remove the const qualifier.
  • reinterpret_cast: It is used for low-level pointer conversions.

Syntax

(type)operand;  // C-style cast

Example Code

#include <iostream>
using namespace std;

int main() {
    double a = 10.78; 
    // C-style cast: Convert double to int (fractional part is discarded)
    int b = (int)a;
    cout << "Original value of a = " << a << endl;
    cout << "After C-style cast = " << b << endl;
 
    return 0;
}

When the above code is compiled and executed, it produces the following result −

Original value of a = 10.78
After C-style cast = 10

Sizeof Operator

The sizeof operator in C++ is used to determine the size(in bytes) of any data type, variable, or data structure. It is a compile-time unary operator. This is often used for finding the memory consumption of data types, calculating the size of arrays or structures, and allocating memory dynamically using malloc or new.

Syntax

sizeof(type);

Example Code

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    double b = 5.75;
    char c = 'X';

    // Display the size of each data type and variable
    cout << "Size of int: " << sizeof(a) << " bytes" << endl;
    cout << "Size of double: " << sizeof(b) << " bytes" << endl;
    cout << "Size of char: " << sizeof(c) << " byte" << endl;
    cout << "Size of bool: " << sizeof(true) << " byte" << endl;

    return 0;
}

When the above code is compiled and executed, it produces the following result −

Size of int: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Size of bool: 1 byte

Type-Specific Operators

Type-Specific Operators in C++, are the operators, which is used for dynamic memory allocation and deallocation.

  • new: This is used to allocate memory for any single object or an array of objects.
  • delete: This is used to deallocate the memory allocated with new.

Syntax for new Operator

// Single object
pointer = new type;

//Array of objects
pointer = new type[size];

Syntax for delete Operator

// Single object
delete pointer;

//Array of objects
delete[] pointer;

Example Code

#include <iostream>
using namespace std;

int main() {
    int* ptr = new int;  
    *ptr = 25;          
    cout << "Dynamically allocated integer: " << *ptr << endl;

    // Deallocate the memory using delete
    delete ptr;

    return 0;
}

When the above code is compiled and executed, it produces the following result −

Dynamically allocated integer: 25
Advertisements