Skip to content Skip to sidebar Skip to footer

Unary Operators in C++

In this blog, we will see the definition of unary operators in C++, types of unary, and a detailed explanation of each; if you haven’t checked the previous blog on operators, check now and add some precious knowledge to the library of your smart brain. Let’s go.

What is a Unary operator?

Unary operators are those operators who act upon a single operand to produce a new value.

Types of Unary operator

There are divided into five different types

  1. Unary minus(-)
  2. Increment(++)
  3. Decrement(- -)
  4. Logical NOT(!)
  5. Addressof(&)
  6. Sizeof()

Let’s understand each for better clarification.

1. Unary minus(-)

A unary minus operator is denoted by the symbol “-” and this operator makes changes to the operand value, and as a result, it makes the given value negative. Convert a positive value to a negative value and a negative value to a positive value.

syntax

keyword_name variable_name = -value;

example

#include <iostream>
using namespace std;
 
int main()
{
    int positive = 100;
    int negative = -positive;
 
    cout << "Positive : " << positive << endl;
    cout << "Negative : " << negative << endl;
 
    return 0;
}

output

Positive : 100
Negative : -100

2. Increment

The symbol “++” denotes the increment operator. Increment operators always increase the value by 1. 

Increment operators are sub-divided into two types 

1. Pre Increment operator – In the pre-increment operator, the value of the operand will be changed before it is used.

int a= 100;
int b= ++a; // Hence the value of b= 101

2. Post Increment operator – In the Post-increment operator, the value of the operand will be changed after it is used.

int a = 1;
int b = a++; // Hence the value of b = 1
int c = a; // Hence the value of c = 2

 

3. Decrement

The symbol “–” denotes the decrement operator. Decrement operators always decrease the value by 1. 

Decrement operators are sub-divided into two types 

  1. Pre-decrement or Prefix decrement
  2. Post-decrement or Postfix decrement

1. Pre-decrement – In the pre-decrement operator, the value of the operand will be changed before it is used.

int a= 100;
int b= --a;  // Hence the value of b= 99

2. Post-decrement – In the Post-decrement operator, the value of the operand will be changed after it is used.

 int a = 1;
 int b = a--;   // Hence the value of b = 1
 int c = a;     // Hence the value of c = 0

4. Logical NOT

The symbol denotes the logical NOT operator “!”. This operator reverses the meaning of its operand as in if a condition is true, then the Logical NOT operator will make it false. This operator is also known as a Logical Negation Operator.

syntax

!value

Example

#include <iostream>
using namespace std;
int main()
{
    int x = 0;
    if (!x)
        cout << "x is not zero" << endl;
    else
        cout << "x is zero " << endl;

    return 0;
}

output

x is not zero

5. AddressOF 

The addressof the operator is denoted by the symbol “&” This operator returns the addressof any variable. These addresses returned by the address-of-operator are known as pointers because they “point” to the variable in memory.

syntax

pointer = &variable;

example

#include <iostream>
using namespace std;

int main()
{
    int Geek;
    int *ptr;  	  //creating pointer
    ptr = &Geek; //address of Geek variable is stored in ptr pointer

    cout << ptr << endl;

    return 0;
}

output

0x61fe14

6. Sizeof 

The size of the operator is denoted by the symbol “sizeof()”. The size of the operator acts like a function. This operator always returns the variable/object occupied size.

syntax

sizeof() // parameter in brackets

example

#include <iostream>
using namespace std;
int main()
{
    int a = 20;

    cout << "The size of a : " << sizeof(a) << endl;
    cout << "The size of 10 : " << sizeof(10) << endl;
    cout << "The size of int : " << sizeof(int) << endl;
    cout << "The size of char : " << sizeof(char) << endl;
    cout << "The size of float : " << sizeof(float) << endl;
    return 0;
}

output

The size of a : 4
The size of 10 : 4
The size of int : 4
The size of char : 1
The size of float : 4

Pheww, so that was all about the unary operators. If you find any difficulty understanding this, you can leave a comment below, and don’t forget to subscribe to us for more❣️.

FOLLOW-UP

You must also know that GeekonPeak has just begun and is looking for some loyal supporters so we, the new-gen geeky friends, can grow and create more comprehensive and easy-to-grasp courses for you (for free). Follow us on FacebookInstagram & Twitter as well. Also, don’t miss our newsletter subscription. We provide exclusive blogs, tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners with excellent experiences to share with you. Until next time 🙂

Leave a comment

Sign Up to Our Newsletter

Be the first to know the latest updates