Home >Backend Development >C++ >Unary operators in C/C++
Here we will see what are the unary operators in C/C. Unary operators are operators that act on a single operand to produce a new value. The unary operators are shown below.
Operator | Description |
---|---|
Indirect operator (*) | 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. |
Get address operator (&) | The unary address operator (&) gets the operand address. The operand of the address operator can be a function indicator or an l value that specifies an object that is not a bit field and has not been declared with a register storage class specifier. |
Unary addition operator ( ) | The result of the unary addition operator ( ) is the value of its operand . The operands of the unary plus operator must be of arithmetic type. |
Unary subtraction operator (-) | -(unary subtraction) operator negates the value of the operand . The operands can be of any arithmetic type. The result is not an l value. |
Logical NOT operator(!) | Logical NOT operator(!) inverts the value of its operand meaning. The operands must be of an arithmetic or pointer type (or an expression that evaluates to an arithmetic or pointer type). The operand is implicitly converted to type bool. |
Bitwise negation operator (~) | The bitwise negation operator is sometimes called the "bitwise negation operator" Bitwise negation or bitwise NOT operator produces the bitwise negation of its operand. Operands must be of integer type. |
Prefix increment operator ( ) | Prefix increment operator ( ) adds one to its operand; increments The value after is the result of the expression. The operand must be a non-const value. The result is an l value of the same type as the operand. |
The prefix decrement operator (--) | The prefix decrement operator (--) removes the value from its operand subtracts one from ; the decremented value is the result of the expression. The operand must be a non-const value. The result is an l value of the same type as the operand. |
Type conversion operator () | Type conversion provides the explicit representation of the type of an object under certain circumstances. formula conversion method. After type conversion, the compiler treats cast-expression as type type-name. |
sizeof operator | It is a compile-time unary operator that can be used to calculate the size of its operands . |
new operator | It is a memory allocation operator used to dynamically allocate memory. |
delete operator | It is a memory allocation operator used to release dynamically allocated memory. |
These operators are associative from right to left. Unary expressions usually involve syntax that precedes a suffix or main expression
Let’s look at an example of the - (minus sign) and casting() unary operators.
Real-time demonstration
#include<iostream> using namespace std; int main() { int x; float y = 1.23; x = (int) y; x = -x; cout << x; return 0; }
-1
The above is the detailed content of Unary operators in C/C++. For more information, please follow other related articles on the PHP Chinese website!