What are Unary Operators in C? See Examples

Unary operators in C operate on a single operand. They are used for operations like incrementing, decrementing, negation, and more. These operators are highly efficient and form the foundation of many expressions and loops in C programming.

C provides several unary operators, including:

  • Increment (++)
  • Decrement (–)
  • Unary minus (-)
  • Unary plus (+)
  • Logical NOT (!)
  • Bitwise NOT (~)
  • Address-of (&)
  • Dereference (*)
  • Sizeof operator (sizeof)

Let’s understand each one in detail.


1. Increment Operator ( ++ )

The increment operator increases the value of a variable by 1.

It comes in two forms:

a) Pre-increment (++a)

Increases the value first, then uses it.

Example

int a = 5;
int b = ++a;  // a becomes 6, b = 6

b) Post-increment (a++)

Uses the current value first, then increases it.

Example

int a = 5;
int b = a++;  // b = 5, a becomes 6

2. Decrement Operator ( — )

Decreases the value of a variable by 1.

a) Pre-decrement (–a)

int a = 10;
int b = --a;  // a becomes 9, b = 9

b) Post-decrement (a–)

int a = 10;
int b = a--;  // b = 10, a becomes 9

3. Unary Minus ( – )

Used to change the sign of a numeric value.

Example

int x = 7;
int y = -x;   // y = -7

4. Unary Plus ( + )

Indicates a positive value (rarely used, mostly for clarity).

Example

int x = +25;   // same as x = 25;

5. Logical NOT ( ! )

Used to reverse the logical state of an expression:

  • If the expression is true (non-zero) → becomes false (0)
  • If the expression is false (0) → becomes true (1)

Example

int a = 0;
int b = !a;   // b = 1

Another example:

int x = 5;
printf("%d", !x);   // Output: 0

6. Bitwise NOT ( ~ )

Flips each bit of the operand.

Example

int x = 5;   // binary: 00000101
int y = ~x;  // binary: 11111010 (two's complement)

Output depends on system architecture, but it essentially inverts all bits.


7. Address-of Operator ( & )

Returns the memory address of a variable.

Example

int x = 40;
printf("%p", &x);

8. Dereference Operator ( * )

Used to access the value stored at a memory address (pointers).

Example

int x = 20;
int *p = &x;

printf("%d", *p);   // prints 20

9. sizeof Operator

Returns the size (in bytes) of a data type or variable.

Example

printf("%lu", sizeof(int));     // typically 4 bytes
printf("%lu", sizeof(float));   // typically 4 bytes

10. Combined Example of Unary Operators

#include <stdio.h>

int main() {
    int a = 5;

    printf("Pre-increment: %d\n", ++a);
    printf("Post-increment: %d\n", a++);
    printf("After post-increment value of a: %d\n", a);

    printf("Logical NOT: %d\n", !a);
    printf("Unary minus: %d\n", -a);
    printf("Size of a: %lu bytes\n", sizeof(a));

    return 0;
}

Conclusion

Unary operators are powerful tools in C that enable concise and efficient manipulation of values, memory addresses, and logical expressions. Understanding how pre/post increment and decrement work is essential for writing clean and error-free code.


🔗 View other articles about C Programming:

https://savanka.com/category/learn/c-programming

🔗 External C Documentation:

https://www.w3schools.com/c

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *