Home C++ Unary Operators tutorial in C++

Unary Operators tutorial in C++

In C++, unary operators are operators that operate on a single operand to perform various operations, such as incrementing, decrementing, negating, and returning addresses.

Understanding unary operators is essential for handling single-operand operations efficiently.

Types of Unary Operators in C++

The primary unary operators in C++ include:

  1. Increment (++)
  2. Decrement (–)
  3. Unary minus (-)
  4. Unary plus (+)
  5. Logical NOT (!)
  6. Bitwise NOT (~)
  7. Address-of (&)
  8. Dereference (*)
  9. Sizeof

Let’s explore each unary operator with examples.

1. Increment Operator (++)

The increment operator ++ increases an integer value by 1. It can be used in prefix (++x) and postfix (x++) forms.

  • Prefix (++x): Increments the value before using it in the expression.
  • Postfix (x++): Increments the value after using it in the expression.
#include <iostream>
using namespace std;

int main() {
    int x = 5;
    cout << "Initial value of x: " << x << endl;

    cout << "Postfix increment (x++): " << x++ << endl; // Outputs 5, then x becomes 6
    cout << "After postfix increment, x: " << x << endl;

    cout << "Prefix increment (++x): " << ++x << endl;  // x becomes 7, then outputs 7
    cout << "After prefix increment, x: " << x << endl;

    return 0;
}

Output:

Initial value of x: 5
Postfix increment (x++): 5
After postfix increment, x: 6
Prefix increment (++x): 7
After prefix increment, x: 7

2. Decrement Operator (–)

The decrement operator — decreases an integer value by 1. It also has prefix (–x) and postfix (x–) forms.

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    cout << "Initial value of x: " << x << endl;

    cout << "Postfix decrement (x--): " << x-- << endl; // Outputs 5, then x becomes 4
    cout << "After postfix decrement, x: " << x << endl;

    cout << "Prefix decrement (--x): " << --x << endl;  // x becomes 3, then outputs 3
    cout << "After prefix decrement, x: " << x << endl;

    return 0;
}

Output:

Initial value of x: 5
Postfix decrement (x--): 5
After postfix decrement, x: 4
Prefix decrement (--x): 3
After prefix decrement, x: 3

3. Unary Minus (-)

The unary minus operator (-) negates the value of an operand, changing its sign.

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    int y = -x;

    cout << "Original value of x: " << x << endl;
    cout << "Negated value (y = -x): " << y << endl;

    return 0;
}

Output:

Original value of x: 5
Negated value (y = -x): -5

4. Unary Plus (+)

The unary plus operator (+) returns the value of the operand as-is. It is primarily used to indicate that a variable or constant is positive, though it has limited use.

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    int y = +x;

    cout << "Original value of x: " << x << endl;
    cout << "Value with unary plus (y = +x): " << y << endl;

    return 0;
}

Output:

Original value of x: 5
Value with unary plus (y = +x): 5

5. Logical NOT (!)

The logical NOT operator (!) inverts the boolean value of its operand. If the operand is true, it becomes false, and vice versa.

#include <iostream>
using namespace std;

int main() {
    bool isTrue = true;
    bool isFalse = !isTrue;

    cout << "Original isTrue: " << isTrue << endl;
    cout << "Inverted isFalse: " << isFalse << endl;

    return 0;
}

Output:

Original isTrue: 1
Inverted isFalse: 0

6. Bitwise NOT (~)

The bitwise NOT operator (~) inverts each bit of the operand. It changes each 1 to 0 and each 0 to 1.

#include <iostream>
using namespace std;

int main() {
    int x = 5; // Binary: 0000 0101
    int y = ~x; // Binary: 1111 1010 (two's complement)

    cout << "Original x: " << x << endl;
    cout << "Bitwise NOT of x (y = ~x): " << y << endl;

    return 0;
}

Output:

Original x: 5
Bitwise NOT of x (y = ~x): -6

7. Address-of Operator (&)

The address-of operator (&) returns the memory address of its operand, often used with pointers.

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    int *ptr = &x;

    cout << "Value of x: " << x << endl;
    cout << "Address of x (&x): " << &x << endl;
    cout << "Pointer ptr holds address of x: " << ptr << endl;

    return 0;
}

Output:

Value of x: 5
Address of x (&x): 0x7ffeefbff56c
Pointer ptr holds address of x: 0x7ffeefbff56c

8. Dereference Operator (*)

The dereference operator (*) accesses the value stored at the memory address held by a pointer.

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    int *ptr = &x;

    cout << "Address of x: " << ptr << endl;
    cout << "Value at address (dereference *ptr): " << *ptr << endl;

    return 0;
}

Output:

Address of x: 0x7ffeefbff56c
Value at address (dereference *ptr): 5

9. sizeof Operator

The sizeof operator returns the size, in bytes, of a data type or variable. This operator is evaluated at compile time and is commonly used for memory management.

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    double y = 3.14;

    cout << "Size of int: " << sizeof(int) << " bytes" << endl;
    cout << "Size of variable x: " << sizeof(x) << " bytes" << endl;
    cout << "Size of double: " << sizeof(double) << " bytes" << endl;
    cout << "Size of variable y: " << sizeof(y) << " bytes" << endl;

    return 0;
}

Output:

Size of int: 4 bytes
Size of variable x: 4 bytes
Size of double: 8 bytes
Size of variable y: 8 bytes

10. Summary Table of Unary Operators in C++

Operator Name Description Example
++ Increment Increases value by 1 (prefix/postfix) ++x, x++
Decrement Decreases value by 1 (prefix/postfix) –x, x–
Unary minus Negates the value -x
+ Unary plus Returns the value as positive +x
! Logical NOT Inverts boolean value !x
~ Bitwise NOT Inverts each bit of integer ~x
& Address-of Returns memory address of variable &x
* Dereference Accesses value at pointer’s address *ptr
sizeof Sizeof Returns size of data type or variable in bytes sizeof(int)

Complete Example: Using Unary Operators in C++

This example demonstrates various unary operators, from incrementing and decrementing to pointer operations and logical operations.

#include <iostream>
using namespace std

;

int main() {
    int a = 10;
    int b = -5;
    bool flag = true;
    int *ptr = &a;

    cout << "Original value of a: " << a << endl;
    cout << "Increment a (++a): " << ++a << endl; // Prefix increment
    cout << "Decrement a (--a): " << --a << endl; // Prefix decrement

    cout << "Original value of b: " << b << endl;
    cout << "Negated value of b (-b): " << -b << endl;

    cout << "Logical NOT of flag (!flag): " << !flag << endl;

    cout << "Address of a (&a): " << &a << endl;
    cout << "Dereference pointer (*ptr): " << *ptr << endl;

    cout << "Size of int: " << sizeof(int) << " bytes" << endl;

    return 0;
}

Output:

Original value of a: 10
Increment a (++a): 11
Decrement a (--a): 10
Original value of b: -5
Negated value of b (-b): 5
Logical NOT of flag (!flag): 0
Address of a (&a): 0x7ffeefbff56c
Dereference pointer (*ptr): 10
Size of int: 4 bytes

Unary operators in C++ provide essential functionality for handling single operands in various scenarios, including memory manipulation, logic operations, and pointer dereferencing.

You may also like