Unary operators in C/C++ https://www.skillvertex.com/blog Wed, 24 Jan 2024 06:59:41 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png Unary operators in C/C++ https://www.skillvertex.com/blog 32 32 Unary Operators In C/C++ https://www.skillvertex.com/blog/unary-operators-in-c-cpp/ https://www.skillvertex.com/blog/unary-operators-in-c-cpp/#respond Wed, 24 Jan 2024 06:59:41 +0000 https://www.skillvertex.com/blog/?p=2097 Read more]]>

Table of Contents

Unary Operators In C/C++

Unary operators in C and C++ are special tools used in programming that work with a single piece of data at a time. They help programmers change values, perform actions like counting, and make decisions based on data. In this article, we’ll take a closer look at these unary operators in C and C++, explaining what they are, what they do, and how they can be useful in programming.

Types of Unary Operators

  1. Unary minus ( – )
  2. Increment ( ++ )
  3. Decrement ( — )
  4. NOT ( ! )
  5. Addressof operator ( & )
  6. Sizeof()

1. Unary Minus

The minus operator (-) is a simple but powerful tool in programming. It does one thing: it changes the sign of a number. If you have a positive number, it turns it into a negative number, and if you have a negative number, it makes it positive. This operator is handy for flipping the direction of values in your code when needed.

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

 The implementation of the unary minus (-) operator:

// 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;
}

Output

Positive Integer: 100
Negative Integer: -100

2. Increment

The increment operator (++), a useful tool in programming, serves one primary purpose: to increase the value of a variable by 1. This operator provides two ways to perform the increment operation: prefix and postfix. These options offer flexibility in how you manipulate variable values in your code.

2.1 prefix increment

In the prefix method, the operator comes before the operand, like this: ++a. When you use the prefix increment operator, the value of the operand is changed before it is used in any other operations. This means that if you have a variable a with a value of 5 and you do ++a, a will become 6, and this new value will be used in any subsequent calculations or assignments.

Example

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

2.2 postfix increment

In the postfix method, the operator follows the operand, like this: a++. When you use the postfix increment operator, the current value of the operand is used in any ongoing operations or assignments, and then the operand’s value is increased by 1. So, if you have a variable a with a value of 5 and you do a++, a will first be used as 5 in any calculations, and then it will be incremented to 6 afterward.

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

 The implementation of the increment ( ++ )

// C++ Program to illustrate increment operator
#include <iostream>
using namespace std;
 
int main()
{
 
    int a = 5;
    int b = 5;
    cout << "Pre-Incrementing a = " << ++a << endl;
    cout << "Post-Incrementing b = " << b++ << endl;
    return 0;
}

Output

Pre-Incrementing a = 6
Post-Incrementing b = 5

3. Decrement

The decrement operator (–), a handy tool in programming, has a straightforward purpose: it decreases the value of a variable by 1. Similar to the increment operator, the decrement operation can be performed in two ways, providing flexibility in how you manipulate variable values in your code.

3.1 prefix decrement

In the prefix method, the decrement operator comes before the operand, like this: –a. When you use the prefix decrement operator

Example

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

3.2 postfix decrement

In the postfix method, the decrement operator follows the operand, like this: a–. When you use the postfix decrement operator, the current value of the operand is used in any ongoing operations or assignments

Example

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

 The implementation of the increment ( ++ ):

// C++ Program to illustrate decrement operator
#include <iostream>
using namespace std;
 
int main()
{
 
    int a = 5;
    int b = 5;
    cout << "Pre-Decrementing a = " << --a << endl;
    cout << "Post-Decrementing b = " << b-- << endl;
    return 0;
}

Output

Pre-Decrementing a = 4
Post-Decrementing b = 5

4. NOT ( ! )

The logical NOT operator (!) is a valuable tool in programming used to flip the logical state of its operand. When you apply the logical NOT operator to a condition, it changes it from true to false, and vice versa. This operator is essential for altering the truth value of expressions, which is crucial for decision-making in programming.

Example

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

 The implementation of the NOT (!) operator

// 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;
}

Output

a is greater than b

5. Addressof operator ( & )

The address-of-operator (&) in programming is like a tool that helps us find where a variable is stored in the computer’s memory. When we use this operator, it gives us the exact memory address of a variable. These memory addresses are often called “pointers” because they act as signposts, pointing to where the variable is kept in the computer’s memory. Pointers are important because they allow us to work with variables indirectly, which is useful in various programming tasks.

Example

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

The implementation of the Address of operator(&)


// C++ program to demonstrate the use of 'address-of(&)'
// operator
#include <iostream>
using namespace std;
 
int main()
{
 
    int a = 10;
    int* ptr = &a; //& will give the address of a
    cout << "Address of a = " << ptr;
 
    return 0;
}

Output

Address of a = 0x7ffcbffd5b24

6. Size of()

The size of the () operator is a handy tool in programming that tells us the size of its operand in terms of bytes. This operator always comes before the operand, which can be an expression or a cast. Essentially, it helps us figure out how much memory space a particular piece of data occupies in bytes.

The implementation of the size of the () operator

/// C++ program to illustrate the sizeof operator
#include <iostream>
using namespace std;
 
int main()
{
    cout << "Size of double: " << sizeof(double) << endl;
    cout << "Size of int: " << sizeof(int) << endl;
 
    return 0;
}

Output

Size of double: 8
Size of int: 4

FAQ – Unary Operators In C/C++

Q1. What is a unary operator in C?

Ans. Unary operator is operators that act upon a single operand to produce a new value. The unary operators are as follows. It operates on a pointer variable and returns an l-value equivalent to the value at the pointer address. This is called “dereferencing” the pointer

Q2. What is unary and binary operators in C++?

Ans. Arithmetic operators in programming perform mathematical operations, like addition and subtraction. There are two types: unary (operate on one value) and binary (combine two values). They are crucial for calculations in programming.

Q3. Is unary or binary in C++?

Ans. The plus sign (+) in programming serves as both a unary and binary operator. In its unary form (+a), it enforces the evaluation of the operand as a number or a pointer. In its binary form (a + b), it performs addition between two values.


Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/unary-operators-in-c-cpp/feed/ 0