C++ - Q&A

C++ - Unary Operators



Unary operators are those operators which acts upon a single operand to produce a new value. Types of unary operators in C++ are as follows:

  1. Unary minus operator (-)
  2. Unary plus operator (+)
  3. Increment operator (++)
  4. Decrement operator (--)
  5. NOT operator (!)
  6. Bitwise NOT operator (~)
  7. Addressof operator (&)
  8. Dereference operator (*)
  9. sizeof() operator
  10. new operator (new and new[])
  11. delete operator (delete and delete[])
  12. cast operator ()

  1. Unary minus operator

    It is used change the sign of the operand which means a negative number becomes positive and a positive number becomes positive.

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

  2. Unary plus operator

    It returns the value same as the operand.

    int a = 10;
    int b = +a; //b = 10
    

  3. Increment operator

    It is used to increase the value of operand by 1. The operator can be used in two ways.

    1. Pre-increment: The operator precedes the operand and the value of the operand is increased by 1 before it is used.
      int a = 10;
      int b = ++a; //b = 11
      

    2. Post-increment: The operator follows the operand and the value of the operand is increased by 1 after it is used.
      int a = 10;
      int b = a++; //b = 10
      int c = a;   //c = 11    
      

  4. Decrement operator

    It is used to decrease the value of operand by 1. The operator can be used in two ways.

    1. Pre-decrement: The operator precedes the operand and the value of the operand is decreased by 1 before it is used.
      int a = 10;
      int b = --a; //b = 9
      

    2. Post-decrement: The operator follows the operand and the value of the operand is decreased by 1 after it is used.
      int a = 10;
      int b = a--; //b = 10
      int c = a;   //c = 9   
      

  5. NOT operator

    It is used to reverse the logical state of its operand. The operator converts a TRUE condition into FALSE and vice-versa.

    int x = 10;
    bool y = (x > 5);  // y = TRUE
    y = !(x > 5);     // y = FALSE
    

  6. Bitwise NOT operator

    It is used to change each bit to its opposite - 0 becomes 1 and 1 becomes 0.

    int x = 18;  // binary: 0000000000010010
    int y = ~x;  // binary: 1111111111101101
    

  7. Addressof operator

    It is used to return the memory address of a variable. The returned value is also known as pointer as it points to the variable in memory address.

    int x = 10;
    int *p;      // pointer declaration
    p = &x;      // address of x is copied to pointer p
    

  8. Dereference operator

    It is used to return the value stored in the address pointed by the pointer.

    int x = 10;
    int *p;      // pointer declaration
    p = &x;      // address of x is copied to pointer p
    int y = *p;  // gives the value stored in the address.
    

  9. sizeof operator

    It is used to return the size of the operand in bytes.

    int x = 10;
    int y = sizeof(x);  // size of int is 4 bytes
    

  10. new operator

    It is used to initialize the memory and return the address of the newly allocated and initialized memory to the pointer variable.

    //pointer declared with assignment
    int *p1 = new int; 
    //pointer initialized with 10     
    int *p2 = new int(10);  
    //allocate array of size 10 of memory of type int
    int *p3 = new int[10]; 
    

  11. delete operator

    It is used to deallocate dynamically allocated memory.

    delete p1;    //release pointer p1
    delete[] p2;  //release block of memory p2 
    

  12. cast operator

    It is used to convert the value of one data type to another data type.

    float x = 10.5;
    int y = int (x); // functional notation
    int z = (int) x; // c-like cast notation