×

Unary Operators in C++

Unary operators in C++

Unary operator: is operations that function to produce a new value on a single operand.

a) unary minus: A minus operator modifies the argument's symbol. A positive number becomes negative, so it becomes a positive negative number.

int x = 15;
int y = -x;  // y = -15

Unary minus is dissimilar from decrement operator since two operands are required for subtraction.

b) Increment: It's being used to raise the variable value by 1. The raise can be done in two different ways

prefix increment

The operator precedes the operand by this method (+ + a). Until it is was using the operand value would be altered.

int x = 4;
int y = ++x;  // y = 8

postfix increment

The operator follows the operand in this procedure (a + +). After it has been were using the value operand will be changed.

int x = 2;
 int y = x++;   // y = 2
 int z = x;     // z = 4

c) decrement

It's being used to decrease the variable value by 1. The rise can be affected in 2 directions

prefix decrement

The operator precedes the substring in this technique (– -a). Until it has been was using, the operand value would be altered.

  int x = 2;
  int y = --x;  // y = 1

posfix decrement

The operator tends to follow the iterator in that method ( a--). Once it's been included, the operand value is modified.

int x = 1;
 int y = x--;   // y = 1
 int z = x;     // z = 0

C++ program for Prefix and Postfix combination operations:

// C++ program to demonstrate working of unary increment
// and decrement operators
#include <iostream>
using namespace std;
int main()
{
          // Post increment
          int x = 2;
          cout << "x value: " << x << endl;
          int y = x++;
          cout << "y value after x++ : " << y << endl;
          cout << "x value after x++ : " << x << endl;
          // Pre increment
          x = 2;
          cout << "x value:" << x << endl;
          y = ++x;
          cout << "y value after ++x : " << y << endl;
          cout << "x value after ++x : "<< x << endl;
          // Post decrement
          x = 7;
          cout << "x value before decrement: " << x << endl;
          y = x--;
          cout << "y value after x-- : " << y << endl;
          cout << "x value after x-- : " << x << endl;
          // Pre decrement
          x = 7;
          cout << "x value: "<< x<<endl;
          y = --x;
          cout << "y value after --x : " << y << endl;
          cout << "x value after --x : " << x << endl;
          return 0;
}

Output:

Unary operators in C++

d) NOT(!): This is used to change its operand 's logical condition. If a statement is fulfilled, therefore the operator Logical NOT will make it not true.

If y is true, then !y is false
If y is false, then !y is true

e) Addressof operator(&): This provides a variable url. It serves to revert back a variable's memory location. Such addresses returned by the operator's address are recognized as reference points because they "point" in memory to the factor.

& gives an address on variable n
int x;
int *ptr;
ptr = &x;

f) sizeof(): That operator produces its operand length, in bytes. The operator's length every time invokes its operand. An expression is an operand, or it may be a cast.

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

Output:

Unary operators in C++

Related Topics

Dynamic Binding in C++

The notion of dynamic binding solved the challenges associated with static binding. Static binding refers to bindings that can be resolved by the compiler at runtime. All storage, stationary, and...

3 minutes read.

Compile Time Polymorphism in C++

What is Polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. One application of polymorphism in...

4 minutes read.

Similarities between C++ and Java

People who are preparing for software engineering roles must have come across either one of these languages because as all the companies do not accept python for hiring a fresher...

3 minutes read.

C++ vardiac() function

In the C++ programming language, the flexibility feature is provided by the variadic function. To understand more about flexibility, let's see the following syntax. Syntax: If we have to add two numbers,...

3 minutes read.

C++ Reading file

In file handling, read() function is used to read data from the file into the program. The read() uses ifstream library to read data from a file. Syntax file-stream-class   file-stream-object;   file-stream-object.read((char *)&var , sizeof (var)); <h3">Example ofstream  outfile;         outfile . read((char*)&emp,sizeof(emp)); C++ File Handling read() Function Example Reading the content of existing...

2 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.

Initialize Array of objects with parameterized constructors in C++

Initialize Array of objects with parameterized constructors in C++ When a class is defined, only the specification for the object is specified; no memory or capacity is allocated. You need to...

3 minutes read.

Vector in C++

Vector in C++ In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++. So basically, vector is very...

3 minutes read.

Singleton Design Pattern in C++

Singleton is similar to the global variable. Singleton helps to have only one object of its kind and provides only single access. One of the key features of the singleton...

2 minutes read.

C++ Mutable keyword

C++ mutable keyword Mutable class storage specifier in C++ Storage class specifiers in C are auto, register, static, and external. Typedef is also considered as a specifier of the storage...

4 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

C++ Virtual Destructor

In C++, a destructor is a class member function that is used to free up space or remove an object of the class that has gone out of scope. The...

4 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

C++ STL Components

C++ STL Components In today’s article, we are going to learn about all the points things that is related to STL in C++ so stay connected because you are going to...

6 minutes read.

While Loop Examples in C++

While Loop A while loop repeats all code in its body, also known as a while statement, as long as a specific condition is satisfied. The loop ends if or when...

4 minutes read.

C++ Static

What is the Static keyword? In C++, the keyword static is used to give an element some particular properties. Static elements are only given storage in the static storage region once...

4 minutes read.

C++ vs C#

What exactly is C++ programming? Bjorne Stroustrup is the creator of the C++ programming language. His goal was to create a powerful object-oriented programming language with the capabilities of C. It...

4 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.

Different Ways to Compare Strings in C++

This section will go over the many methods for comparing strings in the C++ programming language. The string comparison checks if the first string is equal to another string. HELLO...

6 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.