5. Unary Operators

    These operators need a single value to process the expression. The unary Operators are listed below.

1.   Increment operator (++) and Decrement operator(--):

This operator deals with pre-increment(++Operand), post-increment(operand++), pre decrement(--Operand) and post decrement(Operand--).

     Program:

#include <iostream>

using namespace std;

int main() {

    int x,y,z;

    cout<<"Enter the number:";

    cin>>x;

    //pre increment

    y = ++x;

    //post incremnt

    z = x++;

    cout << "Pre increment value: " << y <<endl ;

    cout << "Post increment value: " << x <<endl;

    return 0;

}

Output:

Enter the number:6

Pre increment value: 7

Post increment value: 8

     Note :same program can be used with decrement operator.

 

2.Unary + and unary – operators:

This operator makes the variable as positive(+) and negative(-).

Program:

#include <iostream>

using namespace std;

int main() {

    int x,y,z,a;

    cout<<"Enter the first number:";

    cin>>x;

    cout<<"Enter the second number";

    cin>>y;

    z= +x;

    a= -y;

    cout<<"Using Unary + Operator:+"<<z<<endl;

    cout<<"Using Unary - Operator:"<<a;

    return 0;

}

Output:

Enter the first number:5

Enter the second number6

Using Unary + Operator:+5

Using Unary - Operator:-6

 

3.Logical NOT operator(!):

This operator negates the value of a boolean expression.

    Program:  

    #include <bits/stdc++.h>

    using namespace std;

    int main() {

    bool expr = false;

    bool f_value = !expr;

    cout << f_value;

    cout << "Value of expr:" << expr <<endl;

    cout << "Value of f_value :"<<f_value;

    return 0;

    }

  Output:

Value of expr:0

Value of f_value :1

4.Bitwise NOT operator(~):

It creates a bitwise negation operation on numbers, especially integers.

    Program:

#include <iostream>

using namespace std;

int main() {

      // Binary: 0000 1010

    unsigned int x = 10;

    // Bitwise NOT: Inverts each bit of 'a'

    unsigned int y = ~x;

    cout << "x: " << x << " (Binary: 0000 1010)\n";

    cout << "~x: " << y << " (Binary: 1111 0101)";

 

    return 0;

}

   Output:

x: 10 (Binary: 0000 1010)

~x: 4294967285 (Binary: 1111 0101) 

 5.Addressof Operator(&):

It gets the address of given variable.

    Program:

      #include <iostream>

      using namespace std;

      int main() {

      int no = 42;

      // Let us print the address of no

      cout << "The address of variable 'no':"<< &no;

      return 0;

    }

Output:

The address of variable 'no':0x7ffd9cd4115c 

6. Dereference Operator (*):

This operator is used with pointers.

   Program:

   #include <iostream>

   using namespace std;

   int main() {

   int n = 12;

   int* ptr = &n;

   // let us access the ptr which points n

   cout << "The Variable is 'n':" <<n <<endl;

   cout << "The value stores in the pointer (*ptr):"<<*ptr;

    return 0;

   }

Output:

The Variable is 'n':12

The value stores in the pointer (*ptr):12 

7.Sizeof() operator:

It gives you the size of the given variable.

 Program:

 #include <iostream>

 using namespace std;

 int main() {

    int n = 12;

    cout <<"Size of Operator usage" <<endl;

    cout <<"The sizeof (n):" << sizeof(n) <<endl;

    return 0;

 }

Output:

Size of Operator usage

The sizeof (n):4

That’s all. The Unary Operators in c++ was explained with examples. Hope, you understood this .Keep Coding!!!!!


Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.