Unary Operator in C
In this section, we will discuss the unary operator in the C programming language. Operators are the special symbols used to perform mathematical and logical operations to the given numbers or operands and returns results based on passed operator between the operands.
A unary operator is an operator used to operate on a single operand to return a new value. In other words, it is an operator that updates the value of an operand or expression's value by using the appropriate unary operators. In Unary Operator, operators have equal priority from right to left side associativity.
Types of the Unary Operator
Following are the types of the unary operators in the C programming language.
Unary Minus (-)
The Unary Minus operator is represented using the symbol (-). The unary operator is used to change the sign of any positive value to a negative value. It means it changes the positive number to the negative, and a negative number becomes the positive number using the unary minus operator.
The value of a: 5 The value of b: -5 The value of -n1: -20 The value of -n2: 30
Unary plus (+)
The unary plus operator is represented as the "+" symbol, and it does not change to the operand value.
The value of a: 10 The value of b: -10
Unary Increment Operator (++)
It is the unary increment operator, which is denoted by the "++" symbol. The "++" symbol represents the operand's value is increased by 1. It can be used in two ways, as the post-increment and the pre-increment.
Pre Increment: The pre-increment operator is represented as (++a), which means the value of variable 'a' is increment by 1 before using operand to the expression.
The initial value of x is 10, and using the pre-increment operator (++x) increases the operand value by 1 before assigning it to the variable 'A'.
Post Increment: The (a++) symbol represents the post-increment operator, which means the value of 'a' is incremented by 1 after assigning the original value to the expression or another variable.
Here the initial value of the x variable is 10 and using the post-increment operator (x++) to assign increment value of the 'x' to the variable 'A'.
Pre Increment Operator The value of x is 11. The value of a is 11. Post Increment Operator The value of y is 20. The value of b is 21.
Unary Decrement Operator (--)
The unary decrement operator is opposite to the unary increment operator. The Unary decrement operator is represented by the double minus (--) symbol, and it is used to decrease the operand value by 1 according to the decrement's types. The Unary decrement operator is of two types: the Pre decrement operator and the Post Decrement operator.
Pre Decrement: The pre decrement operator is denoted as (--a) symbol, meaning the operand value is decreased by 1 before assigning to another variable or expression.
Post Decrement: The Post decrement operator is denoted as (a--) symbol, which means the original value is decreased by 1 after assigning to another variable or expression.
Pre Decrement Operator The value of x is 9. The value of a is 9. Post Decrement Operator The value of y is 20. The value of b is 19.
Unary Sizeof() Operator
The sizeof is a keyword used to find the size of different data types or operands like int, float, char, double, etc.
The size of the int (x) variable is: 4 The size of the float (y) variable is: 4 The size of the char (ch) variable is: 1 The size of the double (z) variable is: 8
Logical Not (!) Operator
The logical not operator is used to reverse the given condition. For example, if the operand is true, the logical not operator (!) reverses and return false; if the operand is false, the logical operator returns true.
The Boolean value of a is: 1 The Boolean value of b is: 0 The Boolean value of c is: 0 The Boolean value of d is: 1
AddressOf Operator (&)
The Unary AddressOf Operator is denoted as ampersand (&) symbol, which is used to find the address of a variable defined in computer memory.
The value of variable a is: 10 The address of variable b is: 6487704