Topic 27 of 100 Operators in C Interactive Tutorial

Unary Operators in C: Working With Single Operands

11 mins read Last updated: 22 Aug 2026 18,800 views
C Compiler 0.00%

What is Unary Operators in C? Core Concept & Direct Summary

Core Objective & Problem Statement

What are unary operators in C, and how do operators like `+`, `-`, `!`, `~`, `&`, `*`, `sizeof`, and type casts operate on a single operand?

Direct Answer Summary

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:

main.c
Run Online
int x = -5;
// unary minus: one operand (5)int y = !0;
// logical NOT: one operand (0)int z = sizeof(int);
// sizeof: one operand (int)

Compare with the operator family tree:

ArityOperandsExamples
Unary1-x,!flag,count++
Binary2a + b,x < y
Ternary3cond ? a : b

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:

main.c
Run Online
int temperature = 25;
int freezing = -temperature;
// -25
int debt = -100;
int credit = -debt;
// +100 — double negative!

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:

main.c
Run Online
int x = +5;
// 5 — yes, really, that's it

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:

main.c
Run Online
int isEmpty = 0;
if (!isEmpty) {
    printf("Has items\n");
} else {
    printf("Empty!\n");
    // this runs — !0 is 1 (true)}
    printf("%d\n", !5);
    // 0 — truth flipped to falseprintf("%d\n", !!5);   // 1 — double flip: the "normalize to 1" trick

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:

main.c
Run Online
unsigned char a = 5;
// 00000101// ~a flips to:          11111010printf("%u\n", (unsigned char)~a);
// 250

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:

main.c
Run Online
int count = 5;
count++;
// 6 — add one
count--;
// 5 — subtract one

Both are unary (one operand), and both come in two flavors with a timing difference:

main.c
Run Online
int a = 5, b;
b = a++;
// POST: b gets 5, THEN a becomes 6
b = ++a;
// PRE:  a becomes 7 FIRST, then b gets 7

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:

main.c
Run Online
#include <stdio.h>
int main() {
    int marks[10];
    printf("%zu\n", sizeof(char));
    // 1
    printf("%zu\n", sizeof(int));
    // 4 (typical)
    printf("%zu\n", sizeof(double));
    // 8 (typical)
    printf("%zu\n", sizeof(marks));
    // 40 — the WHOLE array!
    return 0;
}

That last line powers the classic array-length formula you'll use forever:

main.c
Run Online
int length = sizeof(marks) / sizeof(marks[0]);
// 40 / 4 = 10 elements

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:

main.c
Run Online
int x = 42;
int *p = &x;
// & — address-OF: grabs x's memory address
printf("%d\n", *p);
// * — value-AT: reads what's at that address → 42
  • &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

OperatorNameExampleResult
-Unary minus-x(x = 5)-5
+Unary plus+x5(no change)
!Logical NOT!50
~Bitwise NOT~5-6
++Incrementx++x + 1
--Decrementx--x - 1
sizeofSize in bytessizeof(int)4
&Address-of&xmemory address
*Dereference*pvalue at address

A Complete Working Example

main.c
Run Online
#include <stdio.h>
int main() {
    int temperature = -8;
    int isSunny = 0;
    int counter = 0;
    // unary minus flips the sign
    printf("Absolute temp: %d\n", -temperature);
    // 8
    // logical NOT flips the truth
    if (!isSunny) {
        printf("No sun today.\n");
    }
    // ++ counts sightings
    counter++;
    counter++;
    printf("Cloud sightings: %d\n", counter);
    // 2
    // sizeof measures the toolbox
    printf("int size here: %zu bytes\n", sizeof(temperature));
    // & and * play pointer tag
    int *p = &counter;
    *p += 10;
    // modify through pointer
    printf("Counter now: %d\n", counter);
    // 12
    return 0;
}

Output:

main.c
Run Online
Absolute temp: 8
No sun today.
Cloud sightings: 2
int size here: 4 bytes
Counter now: 12

Six unary operators, one tiny program — each doing its solo act.

Runnable C Code Example & Practical Implementation

main.c
Run Online
#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

Best Practice & Key Takeaways
Never write side-effecting code (e.g., 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 questions
Q

Q: Does `sizeof(i++)` increment `i` at runtime?

A: No! `sizeof` is evaluated completely at compile-time by the compiler's type analyzer; code inside `sizeof()` is not executed at runtime.
Q

Q: What is the unary minus operator?

A: It negates the numeric value of its operand (e.g. `int x = 5; int y = -x; // y is -5`).

Related Tools, Tutorials & Authoritative References

Recommended Interactive Tools

Authoritative Standards & Citations