int x = -5;
// unary minus: one operand (5)int y = !0;
// logical NOT: one operand (0)int z = sizeof(int);
// sizeof: one operand (int)Unary Operators in C: Working With Single Operands
What is Unary Operators in C? Core Concept & Direct Summary
What are unary operators in C, and how do operators like `+`, `-`, `!`, `~`, `&`, `*`, `sizeof`, and type casts operate on a single operand?
Most operators are social creatures — they need two operands to work.a + b,x < y,p && q: always a pair.
But a special class of operators worksalone. Give them a single operand and they're perfectly content: flipping its sign, inverting its truth, measuring its size, or bumping it up by one. These are theunary operators— and despite working solo, they include some of the most frequently typed symbols in all of C.
In this guide, you'll meet the complete unary family: the sign flippers, the truth inverter, the bit flipper, the legendary++and--, the size detectivesizeof, and the pointer gateway duo&and*. One operand each — zero exceptions.
How Does Unary Operators in C Work? In-Depth Mechanics & Architecture
What Is a Unary Operator?
Aunary operatoroperates on exactlyone operand:
Compare with the operator family tree:
C has six everyday unary operators. Let's meet each one.
1. Unary Minus (-): The Sign Flipper
Place-before a single value and it negates it:
Don't confuse it with subtraction:a - bisbinaryminus (two operands);-aisunaryminus (one operand, sign flipped). Same symbol, different job — C figures out which you meant from context.
2. Unary Plus (+): The Operator That Does (Almost) Nothing
Perfectly legal, mostly decorative:
Unary plus exists mainly for symmetry with unary minus. Behind the scenes it can trigger integer promotion (acharbecomes anint), but in daily code you'll rarely write it. Know it exists; move on.
3. Logical NOT (!): The Truth Inverter
One!flips true to false and false to true — the same rule from : zero becomes 1, non-zero becomes 0:
if (!flag)reading as "if NOT flag" is one of the most common idioms in real C code. The!!double-flip is a sneaky way to crush any value to exactly 0 or 1.
4. Bitwise NOT (~): The Bit Flipper
One operand, and every single bit gets inverted:
On signed values the famous rule applies:~xequals-x - 1(two's complement storage). So~5is-6. This operator is the engine behind clearing bits —flags & ~MASK
5. Increment and Decrement (++ and --): The Famous Pair
The operators so iconic a language was named after them:
Both are unary (one operand), and both come in two flavors with a timing difference:
Post means "use the value, change it after." Pre means "change first, then use." Standing alone (count++;), both do the same thing — the difference only bites inside expressions. This pair gets the full treatment.
6. sizeof: The Size Detective
sizeoflooks like a function but is actually acompile-time operator— it reports how many bytes a type or variable occupies:
That last line powers the classic array-length formula you'll use forever:
Becausesizeofruns at compile time, it costs nothing at runtime — a genuinely free safety tool.
7. The Pointer Gateways: & and *
Two unary operators open the door to C's superpower:
&x— "give me theaddress ofx" (unary — one operand)*p— "give me thevalue atthat address" (unary — one operand)
Again, context distinguishes these from their binary cousins:a & bis bitwise AND,a * bis multiplication — but&xand*pare pointer operators. When you're ready takes you deep.
One Operand, One Table: The Complete Reference
A Complete Working Example
Output:
Six unary operators, one tiny program — each doing its solo act.
Runnable C Code Example & Practical Implementation
#include <stdio.h>
int main() {
int value = 42;
int *ptr = &value;
// Unary & (address-of)
printf("Original value: %d\
", value);
printf("Dereferenced value: %d\
", *ptr);
// Unary * (dereference)
printf("Logical negation: %d\
", !value);
// Unary !
printf("Bitwise inversion: %d\
", ~value);
// Unary ~
return 0;
}Key Takeaways, Best Practices & Memory Safety Rules
sizeof(n++)) inside sizeof because expressions inside sizeof are not executed.Frequently Asked Questions & Technical Interview Insights
Technical Interview & Viva Questions
Frequently asked C programming interview questionsQ: Does `sizeof(i++)` increment `i` at runtime?
Q: What is the unary minus operator?
Related Tools, Tutorials & Authoritative References
Recommended Interactive Tools
- Online C Compiler & Runner — Interactive online GCC sandbox to execute C code in real time.
- Online C Code Editor & IDE — Multi-file C programming environment with code formatting.
- C++ Programming Tutorial — Transition from procedural C to Object-Oriented C++ with STL.
- JavaScript Programming Tutorial — Master modern JavaScript fundamentals, DOM APIs, and async patterns.
Authoritative Standards & Citations
- ISO/IEC 9899 (C Language Standard) — Official international C specification by ISO/IEC JTC 1/SC 22/WG 14.
- GNU GCC Documentation — Official reference manual for the GNU Compiler Collection and C libraries.
- The C Programming Language (K&R) — Definitive classic work by Brian Kernighan & Dennis Ritchie at Bell Labs.
- C Reference Documentation (cppreference.com) — Verified reference covering C89 through C23 language features.