Unary Operators in C Programming
C includes a class of operators that act upon a single operand to produce a new value. Such operators are known as unary operators.
Unary operators usually precede their single operand, though some unary operators are written after their operand.
Unary Operators
- Unary Minus (-) :
The most common unary operator is unary minus, where a numerical constant, variable, or expression is preceded by a minus sign.
For example: -23.4, -34 etc. - Increment Operator (++) :
The increment operator causes its operand to be incremented by 1.
This operator can be used in two ways i.e. as a post increment or pre increment.
Post increment:
If a = 2 then a++ causes value of a to be incremented by one i.e. after executing a++ we get value of a =3.
Pre Increment:
If a = 2 then ++a also causes value of a to be incremented by one i.e. after executing ++a we get value of a =3. - Decrement Operator (--) :
The decrement operator causes its operand to be decremented by 1.
This operator can be used in two ways i.e. as a post decrement or pre decrement.
Post Decrement:
If a = 2 then a-- causes value of a to be decremented by one i.e. after executing a-- we get value of a =1.
Pre Decrement:
If a =2 then --a also causes value of a to be decremented by one i.e. after executing --a we get value of a =3. - sizeof() Opearator :
sizeof is keyword in C which is used to find size occupied by different data types in memory.
For example:
a. sizeof(int) gives value of 2.
b. If we declare variable like: float x; then sizeof(x) gives 4. - Logical Not (!) Operator :
This operator reverses the meaning of condition. For example: !(False) gives True and !(True) gives False.