Skip to content
Related Articles

Related Articles

Unary operators in C/C++

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 15 Jun, 2022

Unary operators: are operators that act upon a single operand to produce a new value.

Types of unary operators:

  1. unary minus(-)
  2. increment(++)
  3. decrement(- -)
  4. NOT(!)
  5. Addressof operator(&)
  6. sizeof()

1. unary minus: The minus operator changes the sign of its argument. A positive number becomes negative, and a negative number becomes positive.

 int a = 10;
 int b = -a;  // b = -10

unary minus is different from the subtraction operator, as subtraction requires two operands.
Below is the implementation of unary minus (-) operator:

C++




// C++ program to demonstrate the use of 'unary minus'
// operator
 
#include <iostream>
using namespace std;
 
int main()
{
    int positiveInteger = 100;
    int negativeInteger = -positiveInteger;
 
    cout << "Positive Integer: " << positiveInteger << endl;
    cout << "Negative Integer: " << negativeInteger << endl;
 
    return 0;
}
 
// This code is contributed by sarajadhav12052009

Output

Positive Integer: 100
Negative Integer: -100

2. increment: It is used to increment the value of the variable by 1. The increment can be done in two ways:

2.1 prefix increment: In this method, the operator precedes the operand (e.g., ++a). The value of the operand will be altered before it is used.

  int a = 1;
  int b = ++a;  // b = 2

2.2 postfix increment: In this method, the operator follows the operand (e.g., a++). The value operand will be altered after it is used.

 int a = 1;
 int b = a++;   // b = 1
 int c = a;     // c = 2

3. decrement: It is used to decrement the value of the variable by 1. The decrement can be done in two ways:
3.1 prefix decrement: In this method, the operator precedes the operand (e.g., – -a). The value of the operand will be altered before it is used.

  int a = 1;
  int b = --a;  // b = 0

3.2 postfix decrement: In this method, the operator follows the operand (e.g., a- -). The value of the operand will be altered after it is used.

 int a = 1;
 int b = a--;   // b = 1
 int c = a;     // c = 0

4. NOT(!): It is used to reverse the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false.

   If x is true, then !x is false
   If x is false, then !x is true

Below is the implementation of the NOT (!) operator:

C++




// C++ program to demonstrate the use of '!(NOT) operator'
 
#include <iostream>
using namespace std;
 
int main()
{
 
    int a = 10;
    int b = 5;
 
    if (!(a > b))
        cout << "b is greater than a" << endl;
    else
        cout << "a is greater than b" << endl;
 
    return 0;
}
 
// This code is contributed by sarajadhav12052009

Output

a is greater than b

5. Addressof operator(&): It gives an address of a variable. It is used to return the memory address of a variable. These addresses returned by the address-of operator are known as pointers because they “point” to the variable in memory.

& gives an address on variable n
int a;
int *ptr;
ptr = &a; // address of a is copied to the location ptr. 

Below is the implementation of Addressof operator(&):

C++




// C++ program to demonstrate the use of 'address-of(&)'
// operator
 
#include <iostream>
using namespace std;
 
int main()
{
 
    int a;
    int* ptr;
 
    ptr = &a;
 
    cout << ptr;
 
    return 0;
}
 
// This code is contributed by sarajadhav12052009

Output

0x7ffddcf0c8ec

6. sizeof(): This operator returns the size of its operand, in bytes. The sizeof() operator always precedes its operand. The operand is an expression, or it may be a cast.
Below is the implementation of sizeof() operator:

C++




#include <iostream>
using namespace std;
 
int main()
{
    float n = 0;
    cout << "size of n: " << sizeof(n);
    return 1;
}

Output

size of n: 4

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!