answersLogoWhite

0


Best Answer

Yes, a unary operator is an operator that only has one operand. Examples of unary operators are negative (-), positive (+), increment (++), decrement (--), address of (&), dereference (*), logical not (!), sizeof, one's complement (~), new, and delete.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What operator is a unary operator as it works with only one operand?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is plus operator is it unary or binary?

There is no unary plus in C, but if there were, it would have only one operand, unlike the binary plus which has two: x = a + b; /* binary plus */ x = + b; /* unary plus -- not in C*/ x = a - b; /* unary plus */ x = - b; /* unary minus */


What is operand in c plus plus?

An operand is the value that is being operated upon by an operator. For instance, the C++ increment operator (++) is a unary operator, which means it has only one operand, the variable that we wish to increment. This in the expression x++, x is the operand. The addition operator (+) is a binary operator and therefore has two operands. Thus in the expression x + y, x and y are the operands.


What is conditional expression operator?

The conditional operator (? :) is a ternary operator (it takes three operands). The conditional operator works as follows:The first operand is implicitly converted to bool. It is evaluated and all side effects are completed before continuing.If the first operand evaluates to true (1), the second operand is evaluated.If the first operand evaluates to false (0), the third operand is evaluated.The result of the conditional operator is the result of whichever operand is evaluated - the second or the third. Only one of the last two operands is evaluated in a conditional expression.


Do you consider a crane as a robot?

If it only works with an operator, no.


Which operand should be passed in the binary overloaded operator function as a second operand?

The right-hand operand; the r-value of the operator. Unary operators have one operand while tertiary operators have three operands. All binary operators have two operands, the l-value and the r-value. The l-value is the operand to the left of the operator while the r-value is the operand to the right of the operator. Thus, in the expression x + y, x is the l-value while y is the r-value. When overloading binary operators in a class, you need only specify the r-value. The l-value is the instance of the class to which the operator applies and therefore does not need to be specified. For instance: class MyClass { public: MyClass(int data=0):m_data(data){} // default constructor int operator+ (const int rhs) const {return(m_data+rhs);} private: int m_data; }; While this allows you to return the sum of your class instance and an integer, it does not allow you to return the sum of an integer and an instance of your class. For example: MyClass obj(5); int x = 10; int y = obj + x; // OK! y is 15. int z = x + obj; // Compiler error! No operator exists that accepts an r-value of type MyClass. To fix this error and allow for two-way addition, you must declare a binary operator overload outside of the class. You cannot do it inside the class because the l-value is an int, not an instance of MyClass. The external overload requires two parameters, the l-value and the r-value of the operator: int operator+(const int lhs,const MyClass& rhs) {return(rhs+lhs);} Note that the implementation simply reverses the operands. This is functionally equivalent to making the following explicit call: return(rhs.operator+(lhs)); Note also that since MyClass::operator+ is a public operator of MyClass, this overload does not need to be declared a friend of MyClass (a common misconception). However, the overload must be declared in the same file where the class is declared since it is only of relevance to MyClass but should be made available wherever MyClass is accessible.

Related questions

Does a unary expression consists of only one operand with no operator?

No, a unary expression consists of one operand and one operator


What is plus operator is it unary or binary?

There is no unary plus in C, but if there were, it would have only one operand, unlike the binary plus which has two: x = a + b; /* binary plus */ x = + b; /* unary plus -- not in C*/ x = a - b; /* unary plus */ x = - b; /* unary minus */


What is operand in c plus plus?

An operand is the value that is being operated upon by an operator. For instance, the C++ increment operator (++) is a unary operator, which means it has only one operand, the variable that we wish to increment. This in the expression x++, x is the operand. The addition operator (+) is a binary operator and therefore has two operands. Thus in the expression x + y, x and y are the operands.


What is conditional expression operator?

The conditional operator (? :) is a ternary operator (it takes three operands). The conditional operator works as follows:The first operand is implicitly converted to bool. It is evaluated and all side effects are completed before continuing.If the first operand evaluates to true (1), the second operand is evaluated.If the first operand evaluates to false (0), the third operand is evaluated.The result of the conditional operator is the result of whichever operand is evaluated - the second or the third. Only one of the last two operands is evaluated in a conditional expression.


What is the difference between unary and binary operations?

UNARY AND BINARY a unary operation is an operation with only one operand, i.e. an operation with a single input, or in other words, a function of one variable. eg- * Increment: ++x, x++ * Decrement: −−x, x−− * Address: &x * Indirection: *x * Positive: +x * Negative: −x * One's complement: ~x * Logical negation: !x * Sizeof: sizeof x, sizeof(type-name) * Cast: (type-name) cast-expression int i = 0; printf (" %d \n %d ", i++, i++); a binary operation is a calculation involving two operands.


Which is dummy operator in c?

In C, the sizeof operator can be considered a dummy operator because it does not perform any operations on the data but simply returns the size in bytes of a variable or a data type.


What is operational conditioning?

The conditional operator (? :) is a ternary operator (it takes three operands). The conditional operator works as follows:The first operand is implicitly converted to bool. It is evaluated and all side effects are completed before continuing.If the first operand evaluates to true (1), the second operand is evaluated.If the first operand evaluates to false (0), the third operand is evaluated.The result of the conditional operator is the result of whichever operand is evaluated - the second or the third. Only one of the last two operands is evaluated in a conditional expression.


Do you consider a crane as a robot?

If it only works with an operator, no.


Which operand should be passed in the binary overloaded operator function as a second operand?

The right-hand operand; the r-value of the operator. Unary operators have one operand while tertiary operators have three operands. All binary operators have two operands, the l-value and the r-value. The l-value is the operand to the left of the operator while the r-value is the operand to the right of the operator. Thus, in the expression x + y, x is the l-value while y is the r-value. When overloading binary operators in a class, you need only specify the r-value. The l-value is the instance of the class to which the operator applies and therefore does not need to be specified. For instance: class MyClass { public: MyClass(int data=0):m_data(data){} // default constructor int operator+ (const int rhs) const {return(m_data+rhs);} private: int m_data; }; While this allows you to return the sum of your class instance and an integer, it does not allow you to return the sum of an integer and an instance of your class. For example: MyClass obj(5); int x = 10; int y = obj + x; // OK! y is 15. int z = x + obj; // Compiler error! No operator exists that accepts an r-value of type MyClass. To fix this error and allow for two-way addition, you must declare a binary operator overload outside of the class. You cannot do it inside the class because the l-value is an int, not an instance of MyClass. The external overload requires two parameters, the l-value and the r-value of the operator: int operator+(const int lhs,const MyClass& rhs) {return(rhs+lhs);} Note that the implementation simply reverses the operands. This is functionally equivalent to making the following explicit call: return(rhs.operator+(lhs)); Note also that since MyClass::operator+ is a public operator of MyClass, this overload does not need to be declared a friend of MyClass (a common misconception). However, the overload must be declared in the same file where the class is declared since it is only of relevance to MyClass but should be made available wherever MyClass is accessible.


Distinguish between operator overloading and function overloading?

in C++ there is no real difference as operators are overloaded by implementing them as functions. However, while we differentiate between function overloads by the function signature (the number and type of parameters), operator overloads are distinguished only by the parameter types. The parameters are interpreted as operands, and the number of operands will depend upon whether the operator is unary, binary or ternary. That is, for any given operator, the number of operands will be the same for each overload you implement. The only exceptions are the unary increment (++) and decrement (--) operators as they each have postfix and prefix variants. In order to differentiate their signatures, an unreferenced or dummy parameter must be passed to the postfix variants.


Unary relationship in database?

Unary RelationshipsUnary relationships have only one participant-the relation is associated with itself. The classic example of a unary relationship is Employee to Manager. One's manager is, in most cases, also an employee with a manager of his or her own.


Why there is only 2 plus in c plus plus languagewhy not 3 or 4 plus?

I assume by 2 plus you really mean ++. This is the increment operator which is used to increment the operand. If placed before the operand, the operator evaluates the incremented operand (prefix increment). If placed after the operand, the operator evaluates the non-incremented operand (postfix increment). +++ and ++++ are meaningless but are assumed to mean incrementing an increment. If you wish to increment an increment, you must use the compound expression ++(++) or (++)++. Thus for the variable x, prefix incrementing twice would be achieved with ++(++x), while postfix incrementing twice would be achieved with (x++)++. You can also mix the two, such as ++(x++) or (++x)++, both of which would increment x twice but would evaluate the increment of x. If postfix increment is not a requirement, it would be much easier to use the compound expression x+=n, where n is the amount you wish to increment. This is the same as saying x=x+n.