Hero Vired Logo
Programs

More

Masterclasses

Home
Blogs
Unary Operators

To perform logical and mathematical operations on the numerical values, C++ offers different operators. One of them is: unary operator in C. It is used for operating on one operand, thereby producing a value. In short, unary operators in this programming language include dereferencing operator, increment operator, bitwise NOT, address of operator, decrement operator, logical NOT, unary plus, minus operators, and size.

Table of Content:

What are Unary Operators?

A unary operator in C is an operator that can update an operand's value or expression value. Operators in unary operators have equal priority from the right to the left side associativity.

The Role and Functions of Unary Operator in C

The function of the unary operator is to update the particular value of the operand or expression value. The operator uses specified unary operators. 

Read More: DevOps & Cloud Engineering

Unary Operators

6 Types of Unary Operators

Given below are the following types of unary operators found in the C language:

  • Unary Minus (-)
    Unary minus changes the given argument's sign. It means that the positive number becomes negative and vice-versa. However, it is quite different from the subtractor operator. The reason is here you need two operands for subtraction:

    • int p = 20;
    • int q = -p; // q = -20
  • Unary Plus (+)
    This type of unary operator changes the negative argument's sign. Here, the negative number becomes positive. The positive number also becomes positive. So, it is different from the subtractor operator as there are two operands required for subtraction:

    • int p = -20;
    • int q = +p; // q = 20
  • Decrement (- -)
    The next comes the decrement. An operator is used to decrement a certain variable's overall value by 1. One can perform decrement in the following ways:

    • Prefix Decrement
    • Postfix Decrement
  • Increment (++)
    This type of unary operator unlike decrement, the operator is used to increment the value by 1. One can perform it using these ways:

    • Prefix increment
    • Postfix Increment
  • NOT (!)
    NOT (!) is used to reverse an available operand's logical state. It means the logical NOT operator makes a condition to be false in case it is true. Simply put, when p is true, the !p becomes false. Similarly, when the p is false,!p becomes true.
  • Sizeof ()
    Its purpose is to return the operand's original size considering bytes. It precedes the already given operand, which is an expression or might be a cast too. Here's the best example to note down:

    #include 
    using namespace std;
    int main()
    {
    float n = 0;
    cout << “size of n: ” << sizeof(n);
    return 1;
    }
    So, the output is:
    size of n: 4
    
  • Address of operator (&)
    This type of unary operator offers users the variable's address. Its purpose is returning the address of a variable. The addresses returned are better known as pointers. The reason is they point toward the variable that's already present in the memory. 

Learn more about Java Operators here.

Elucidating the Common Unary Operators

The common unary operators in C++ include the following:

  • Logical NOT
  • Dereferencing operator
  • Bitwise NOT
  • Increment operator
  • Decrement operator
  • Address of operator
  • The operator’s size
  • Unary plus 
  • Minus operators

Evaluating 3 Differences Between Prefix and Postfix

The following are differences between the prefix and postfix increment/decrement are tabulated below. Read more about the increment and decrement operators in c with examples:

  Prefix Postfix
The operator in the method precedes the already given operand The operator in the method follows a given operand
The best examples are: ++p and –p The best examples are: p++, p–
The operand's value gets changed before finally using it The available operand's value gets altered right after using it

 

What Do You Mean by Increment (++) and Decrement (--) Operators?

The increment and decrement operators in C is a major topic of discussion. In unary operators in C, the decrement operators(--) and increment(++) are crucial. So, the increment operator increases the variable's value by one, while the decrement operator decreases it by one.

A Brief on the Arithmetic Unary Operators (+ and -)

Unary arithmetic operators can perform a certain action on a particular operand. InstallScript compiler can recognize two operators, positive ( + ) and negative ( - ). Here, negative unary operators reverse the sign from negative to positive or positive to negative.

Understanding the Logical NOT (!) Operator

The function of the logical NOT ( ! ) is to take the falsity to truth and truth to falsity. It is used with the boolean (logical) values. If it is used with the non-Boolean values, the return will be false (provided the single operand is converted to true or returned true.

Get an insight into Object Oriented Programming from this link.

An Insight into Bitwise NOT (~) Operator

In C++, the bitwise NOT operator is the tilde character. It is known by the sign ~. It yields the bitwise complement of an operand. Bitwise NOT can change every bit to the opposite. That means if the value is 0, it becomes 1. And if the value is 1, it becomes 0.

Outlining What Are Precedence and Associativity

While precedence is a priority that groups various operators with respective operands, associativity is the right-to-left or left-to-right order. The latter is used to group operands to the operators with the same precedence.Unary Operators

A Brief on Unary Operator Overloading

Unary operator overloading is the overloading of the unary operator. In simple words, unary operators are the ones that operate on one operand. They produce a completely new value. So, it works with a single class object.

One can overload it with absolutely either a nonmember function or member function. With the member function, there's no argument. On the other hand, the nonmember function has only one argument.

Read: Top Programming Languages to Learn for Working with Databases

Conclusion

So, this post has demonstrated everything about unary operators in C. It has evaluated details on how it is used on one operand or variable. Now that this guide has offered an insight into the unary operator C++, here's presenting some FAQs.

FAQ's

The 3 unary operators include the following: <ul><li>Unary minus ( – )</li> <li>Increment ( ++ )</li> <li>Decrement ( — )</li></ul>

The unary operator in C++ is used for calculating the result on a single variable or operand. It is used on one operand to calculate a new value of the variable.

The binary operator operators on two variables. It manipulates them for a result. The operators are represented by special characters or keywords. They offer a seamless way to compare the character strings or values.

Unary operators can perform the action with one single operand. But for binary operators, they need two operands or variables to perform actions.

The types of unary operators in C include the below-offered ones: <ul><li>Unary plus (+)</li> <li>Unary minus (-)</li> <li>Decrement (- -)</li> <li>Not (!)</li> <li>Increment (++)</li> <li>Address of operator (&)</li> <li>Sizeof ()</li></ul>

Related Blogs

Blogs from other domain

Carefully gathered content to add value to and expand your knowledge horizons

Hero Vired logo
Hero Vired is a premium LearnTech company offering industry-relevant programs in partnership with world-class institutions to create the change-makers of tomorrow. Part of the rich legacy of the Hero Group, we aim to transform the skilling landscape in India by creating programs delivered by leading industry practitioners that help professionals and students enhance their skills and employability.

Data Science

Accelerator Program in Business Analytics & Data Science

Integrated Program in Data Science, AI and ML

Accelerator Program in AI and Machine Learning

Advanced Certification Program in Data Science & Analytics

Technology

Certificate Program in Full Stack Development with Cloud for Web and Mobile

Certificate Program in DevOps and Cloud Engineering

Finance & Management

Certificate Program in Financial Analysis, Valuation and Risk Management

Certificate Program in Strategic Management and Business Essentials

Executive Program in Product Management

Certificate Program in Product Management

Future Tech

Certificate Program in Gaming & Esports

Certificate Program in Extended Reality (VR+AR)

Privacy Policy And Terms Of Use
©2023 Hero Vired. All Rights Reserved.