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.
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
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.
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.
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. 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.
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.
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
What are the 3 unary operators?
The 3 unary operators include the following:
Unary minus ( – )
Increment ( ++ )
Decrement ( — )
What are unary operators?
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.
Is ++ unary or binary?
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.
What is unary vs binary C++?
Unary operators can perform the action with one single operand. But for binary operators, they need two operands or variables to perform actions.
What are the types of unary?
The types of unary operators in C include the below-offered ones:
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.