×

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

C++ Infinite loop

The term "infinite loop" refers to a loop that does not terminate the loop according to the condition. In some cases, an infinite loop may be required in programming, or...

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.

Queue in C++

What is Queue? As the name suggests, the queue is the type of data structure that follows the FIFO (First In - First Out) mechanism. In simple words, it is...

4 minutes read.

swap() function in C++

Swap() function: swap() function in C++: swap() function is a pre-define function in c++ present in STL( Standard template library ). It is used to swap two numbers. It takes two mandatory...

6 minutes read.

C++ Program to move all zeros to the end of the array

Write a program to move all the zeros in the arr[] to the end. The order of the non-zero elements should not be altered and all the zeros should be...

3 minutes read.

Palindrome using For loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

Inheritance and Friendship in C++

In this tutorial, we will look into what Inheritance and Friendship in C++ are, as well as the differences between the two. What is Inheritance in C++: In C++, inheritance is an...

2 minutes read.

C++ Comments

Comment/Remark A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated...

3 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

C++ Overloading

C++ Overloading is a condition when two or more members have the same name with different parameter type or a different number of parameter. C++ overloading is two types: Function...

1 minute read.

Reverse function in C++

The function std::reverse() is included in the standard template library of C++. It takes in a beginning and ending iterator, reversing the order. To use the reverse statement, we need...

2 minutes read.

C++ Polymorphism

Polymorphism refers to the fact that something exists in several forms. Polymorphism, in simple terms, is the ability of a message to be presented in several formats. A real-life example...

4 minutes read.

Desired Capabilities in Selenium Web Driver in C++

A class called Desired Capabilities is used to specify a set of fundamental requirements, such as browser, operating system, and version combinations.to carry out automated testing of a web application...

7 minutes read.

Priority Queue in C++

Priority Queue in C++ Introduction: We have already come across Queues by concluding that they are linear data structures that follow FIFO (First-In-First-Out) approach. We had also discussed the syntax of queues...

4 minutes read.

Pointer to Object in C++

What is a pointer? A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we...

4 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++ Continue in Do-while loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

Accumulate() and partial_sum() in C++ STL Numeric header

The C++ STL's numeric library includes the numeric header. This library provides efficient numeric arrays, support for random number generation, and fundamental mathematical operations and types. Several of the numeric...

3 minutes read.

How to create the Processes with Fork in C++

 In this article, we are going to explain the different methods that help us to create processes with a fork(). There are two methods to do so. These methods are...

2 minutes read.

Sizeof() Operators in C++

sizeof() Operators in C++ The sizeof() operator in C++ defines the size of variables, constants, or data types. It is a unique operator that manipulates other operators and returns the size...

4 minutes read.