Unary Operators in C++

DevOps & Cloud Engineering
Internship Assurance
DevOps & Cloud Engineering

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.

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

 

 

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:
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

     

    DevOps & Cloud Engineering
    Internship Assurance
    DevOps & Cloud Engineering

    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. Install Script 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.

     

    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.

     

     

    FAQs
    The 3 unary operators include the following:
    • Unary minus ( – )
    • Increment ( ++ )
    • Decrement ( — )
    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:
    • Unary plus (+)
    • Unary minus (-)
    • Decrement (- -)
    • Not (!)
    • Increment (++)
    • Address of operator (&)
    • Sizeof ()

    Book a free counselling session

    India_flag

    Get a personalized career roadmap

    Get tailored program recommendations

    Explore industry trends and job opportunities

    left dot patternright dot pattern

    Programs tailored for your Success

    Popular

    Data Science

    Technology

    Finance

    Management

    Future Tech

    Upskill with expert articles
    View all
    Hero Vired logo
    Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.

    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 Specialization for Web and Mobile

    Certificate Program in DevOps and Cloud Engineering

    Certificate Program in Application Development

    Certificate Program in Cybersecurity Essentials & Risk Assessment

    Finance

    Integrated Program in Finance and Financial Technologies

    Certificate Program in Financial Analysis, Valuation and Risk Management

    Management

    Certificate Program in Strategic Management and Business Essentials

    Executive Program in Product Management

    Certificate Program in Product Management

    Certificate Program in Technology-enabled Sales

    Future Tech

    Certificate Program in Gaming & Esports

    Certificate Program in Extended Reality (VR+AR)

    Professional Diploma in UX Design

    Blogs
    Reviews
    Events
    In the News
    About Us
    Contact us
    Learning Hub
    18003093939     ·     hello@herovired.com     ·    Whatsapp
    Privacy policy and Terms of use

    © 2024 Hero Vired. All rights reserved