- 2068 Chaitra · 2 marks
Unary operators in C
Short answer
Unary operators operate on a single operand. They include unary +/-, ++, --, !, ~, &, *, sizeof, and type cast. The term unary means 'one' because they require only one operand, unlike binary operators.
What are unary operators in C? Why are they named so?
Unary operators are operators that work on a single operand. They are called "unary" because uni means one: they need only one operand, unlike binary operators (a + b) which need two.
| Operator | Meaning | Example |
|---|---|---|
+ , - | unary plus / minus | -a |
++ , -- | increment / decrement | ++a, a-- |
! | logical NOT | !a |
~ | bitwise complement | ~a |
& | address of | &a |
* | value at address (pointer) | *p |
sizeof | size in bytes | sizeof(a) |
(type) | type cast | (float)a |
#include <stdio.h>
int main(void)
{
int a = 5, b;
b = -a;
printf("%d %d\n", b, +a);
printf("%d\n", !a);
printf("%d\n", ++a);
printf("%d\n", a--);
printf("%d %d\n", a, (int)sizeof(a));
return 0;
}
Output:
-5 5
0
6
6
5 4
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- NO₂ and SO₂ sources; role in acid rain · Engineering Chemistry, chapter 3
- Acid rain; sources and control of CO and SO₂ · Engineering Chemistry, chapter 3
- Transition elements; why so called; 3d configurations · Engineering Chemistry, chapter 5