C C++

C Unary Operators


Introduction

A unary operator is an operator that operates on a single operand. Unlike binary operators, which require two operands, unary operators perform an operation using only one value or variable.

Unary operators are commonly used to negate values, increment or decrement variables, obtain memory addresses, dereference pointers, determine the size of a data type, perform logical negation, and manipulate bits.

Understanding unary operators is essential because they appear frequently in loops, pointer operations, memory management, and low-level programming.

What Is a Unary Operator?

A unary operator performs an operation on exactly one operand and produces a result.

Syntax

operator operand

For example:

Example

-a

++count

sizeof(int)

!flag

List of Unary Operators

Operator Name Description
+ Unary Plus Indicates a positive value.
- Unary Minus Negates a numeric value.
++ Increment Increases the operand by one.
-- Decrement Decreases the operand by one.
! Logical NOT Reverses a logical value.
~ Bitwise NOT Inverts every bit.
& Address-of Returns the memory address of an object.
* Dereference Accesses the value pointed to by a pointer.
sizeof Sizeof Returns the size of a type or object in bytes.

Unary Plus (+)

The unary plus operator explicitly indicates that a value is positive. It usually has no effect on the value itself.

Example

int value = +10;

Although rarely used, it is valid C syntax.

Unary Minus (-)

The unary minus operator changes the sign of its operand.

Example

#include <stdio.h>

int main(void)
{
    int number = 15;

    printf("%d\n", -number);

    return 0;
}

Output

-15

Increment Operator (++)

The increment operator increases the value of its operand by one.

It has two forms:

  • Prefix increment (++x)
  • Postfix increment (x++)

Prefix Increment

The operand is incremented before its value is used.

Example

int x = 5;

int y = ++x;

After execution:

  • x is 6.
  • y is 6.

Postfix Increment

The current value is used first, and then the operand is incremented.

Example

int x = 5;

int y = x++;

After execution:

  • x is 6.
  • y is 5.

Decrement Operator (--)

The decrement operator decreases the operand by one.

Like the increment operator, it has both prefix and postfix forms.

Example

int x = 10;

--x;

x--;

Logical NOT Operator (!)

The logical NOT operator reverses the logical meaning of an expression.

Operand Result
0 1
Non-zero 0

Example

int finished = 0;

if (!finished)
{
    printf("Still running\n");
}

Bitwise NOT Operator (~)

The bitwise NOT operator flips every bit in an integer value.

Example

int value = 5;

int result = ~value;

Bitwise operations are commonly used in embedded systems, device drivers, networking, and systems programming.

Warning: The result of the bitwise NOT operator depends on the binary representation of signed integers, which may be confusing for beginners.

Address-of Operator (&)

The address-of operator returns the memory address of an object.

Example

int number = 100;

int *ptr = &number;

The variable ptr stores the memory address of number.

Dereference Operator (*)

The dereference operator accesses the value stored at the memory location pointed to by a pointer.

Example

int number = 100;

int *ptr = &number;

printf("%d\n", *ptr);

The expression *ptr retrieves the value stored in number.

Sizeof Operator

The sizeof operator returns the size, in bytes, of a data type or object.

Example

printf("%zu\n", sizeof(int));

printf("%zu\n", sizeof(double));

The actual sizes depend on the platform and compiler.

Tip: Use sizeof instead of hardcoding type sizes to write portable C programs.

Operator Precedence

Most unary operators have higher precedence than arithmetic, relational, and logical binary operators.

Operator Associativity
++ -- + - ! ~ * & sizeof Right to Left

For example:

Example

int x = 5;

int y = -x * 2;

The unary minus is applied before the multiplication, so the result is -10.

Complete Example

The following program demonstrates several unary operators.

Example

#include <stdio.h>

int main(void)
{
    int number = 10;

    printf("Unary minus: %d\n", -number);

    printf("Prefix increment: %d\n", ++number);

    printf("Postfix increment: %d\n", number++);

    printf("Current value: %d\n", number);

    printf("Logical NOT: %d\n", !0);

    printf("Size of int: %zu\n", sizeof(int));

    return 0;
}

Common Beginner Mistakes

  • Confusing prefix and postfix increment or decrement.
  • Using multiple increment or decrement operators on the same variable in a single expression.
  • Assuming ! changes the numeric value instead of its logical value.
  • Using the dereference operator on an uninitialized pointer.
  • Hardcoding type sizes instead of using sizeof.
Warning: Avoid writing expressions such as i = i++; or printf("%d %d", i, i++);. Modifying the same variable multiple times within a single expression can result in undefined behavior.

Summary

Unary operators perform operations on a single operand and are widely used throughout C programming.

Operator Purpose
+ Positive value
- Negate a value
++ Increment
-- Decrement
! Logical NOT
~ Bitwise NOT
& Address-of
* Dereference
sizeof Determine object or type size

Mastering unary operators is an important step toward understanding pointers, memory management, expressions, and many advanced features of the C programming language.