βž• C++ Operators
Estimated reading: 2 minutes 23 views

βž• C++ Unary Operators – Operate on a Single Operand


🧲 Introduction – What Are Unary Operators in C++?

Unary operators in C++ operate on a single operand to perform operations such as incrementing, decrementing, negating, or logical testing. These operators are essential for loop control, logical expressions, pointer manipulation, and mathematical transformations.

🎯 In this guide, you’ll learn:

  • The list and function of unary operators
  • Pre- and post-increment/decrement usage
  • Logical and address-related operators
  • Common use cases and coding tips

πŸ“‹ List of C++ Unary Operators

OperatorSymbolDescriptionExample
Unary plus+Returns the positive value (identity)+a
Unary minus-Negates the value-a
Logical NOT!Inverts a boolean value!flag
Bitwise NOT~Inverts all bits~x
Pre-increment++aIncrements value before use++i
Post-incrementa++Increments value after usei++
Pre-decrement--aDecrements value before use--i
Post-decrementa--Decrements value after usei--
Address-of&Returns the memory address&x
Dereference*Accesses value at a pointer*ptr

✏️ Example – Pre vs Post Increment

int x = 5;

int a = ++x;  // x becomes 6, a = 6
int b = x++;  // b = 6, x becomes 7

🧠 Logical NOT !

bool flag = false;
cout << !flag;  // Outputs 1 (true)

🧠 Bitwise NOT ~

int a = 5;         // 00000101
int b = ~a;        // 11111010 (Two's complement of 6)

πŸ“¦ Address-of & and Dereference *

These are pointer-related unary operators:

int x = 100;
int* ptr = &x;  // Address-of
cout << *ptr;   // Dereference β†’ outputs 100

⚠️ Common Mistakes

❌ Mistakeβœ… Fix
Confusing pre and post opsKnow when value changes vs when it’s returned
Dereferencing uninitialized pointersAlways assign valid address to pointer
Using ~ on float/doubleOnly use ~ on integers
Applying & on temporary valuesUse & on actual variables, not rvalues

πŸ“Œ Summary – Recap & Next Steps

πŸ” Key Takeaways:

  • Unary operators work with a single operand
  • Pre/post increment/decrement behave differently in expressions
  • Logical and bitwise NOT serve distinct purposes
  • * and & are critical in pointer operations

βš™οΈ Real-World Relevance:
Unary operators are used in looping constructs, bitmasking, boolean logic, and pointer dereferencing, making them fundamental to efficient and effective C++ coding.


❓ FAQs – C++ Unary Operators

❓ What is the difference between ++i and i++?
βœ… ++i increments before using the value; i++ increments after using it.

❓ Can I use ++ with float or double?
βœ… Yes. All numeric types support ++ and --.

❓ What does ~ do to a number?
βœ… It flips all the bits (bitwise complement), resulting in -n - 1.

❓ Are & and * also unary operators?
βœ… Yes. & returns an address, * accesses the value at an address.

❓ Can I chain unary operators?
βœ… Yes. Example: --*ptr decrements the value pointed to by ptr.


Share Now :

Leave a Reply

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

Share

C++ Unary Operators

Or Copy Link

CONTENTS
Scroll to Top