Unlike binary operators, which need two values, the unary operator only needs one operand. This makes it the optimal approach for all kinds of scenarios, from increasing or decreasing a variable’s value, flipping signs, working with memory addresses, and more.
Types of Unary Operators in C (with Examples)
Unary operators in C may be small, but they can do a lot with just a single value. They help us tweak numbers, flip signs, work with memory, and even evaluate conditions, without needing a second operand.
Let’s look at the different types of unary operators in C, along with simple unary operator examples to show how they work in real code.
1. Unary Plus (+)
This one’s kind of a formality. It returns the value as-is and doesn’t really change anything. You might see it used to make code more readable.
Output:
2. Unary Minus (-)
This flips the sign of a number.
Output:
3. Increment (++)
Adds 1 to a variable. Comes in two forms:
- Prefix: increments first, then uses the value
- Postfix: uses the value, then increments
Output:
4. Decrement (- -)
Subtracts 1 from a variable. Also comes in prefix and postfix forms.
Output:
5. Address-of Operator (&)
Returns the memory address of a variable.
6. Dereference Operator (*)
This operator can be used to modify the actual value stored at a memory address. When a pointer variable is holding the address of another variable, the dereference operator lets you reach into that address and work with the value directly. This is useful when you want to work with pointers, dynamic memory, or passing variables by reference.
Output:
7. Logical NOT Operator (!)
Reverses a boolean condition. If the value is 0 (false), it becomes 1 (true), and if it’s any non-zero value (true), it becomes 0 (false). It’s commonly used in if statements to check for “not” conditions in a simple, readable way.
Output:
8. Bitwise NOT Operator (~)
Reverses every bit in an integer, turning 1s into 0s and 0s into 1s. It’s often used in low-level programming for bit manipulation and creating bitwise complements.
Output:
9. sizeof() Operator
Determines how much memory (in bytes) a data type or variable occupies. It’s especially useful when working with arrays, structs, or dynamic memory allocation, where knowing the exact size helps you write efficient and portable code.
Output:
These are the core unary operators in C. While some, like ++ and –, are used almost everywhere, others, like *, &, or ~, become important once you dive into more advanced topics like pointers and bitwise operations.
Real-World Applications of Unary Operators in C
Now that you understand the syntax and the differences between all the unary operators in C, let’s learn some real-world applications and use cases.
1. Controlling Loop Counters
Unary operators are frequently used in loops to increase or decrease the value of i and keep he loop going. For example:
Output:
Here we are using the increment operator to increment the value of i by 1 at every iteration of the loop.
2. Working with Pointers
When working with pointers, the address-of (&) and dereference (*) operators are essential. They’re used in everything from dynamic memory management to function arguments.
Output:
- &a gives the address of a
- *ptr lets us access the value at that address
3. Logical Checks and Flags
The logical NOT (!) operator comes in handy when you want to check if something is false, like when a variable is set to 0. You’ll often see it in if statements to keep conditions short and easy to read.
Output:
4. Bit Manipulation
The bitwise NOT (~) operator is commonly used in systems or embedded programming where you need direct control over bits. It’s particularly useful for inverting binary values, toggling flags, or applying bitmask operations efficiently.
Output:
5. Memory Size Calculations
The sizeof() operator tells you how much memory a particular data type or variable occupies. It’s especially useful when you’re working with arrays, structures, or allocating memory dynamically, where knowing the exact size can make a big difference.
Output:
Unary Operators in C Quick Cheat Sheet
Operator | Name | Action |
+ | Unary Plus | Leaves the value unchanged |
– | Unary Minus | Flips the sign of the value (positive ↔ negative) |
++a | Pre-Increment | Increases the value by 1, then uses the updated value |
a++ | Post-Increment | Uses the current value, then increases it by 1 |
–a | Pre-Decrement | Decreases the value by 1, then uses the updated value |
a– | Post-Decrement | Uses the current value, then decreases it by 1 |
! | Logical NOT | Reverses a condition (true becomes false, false becomes true) |
~ | Bitwise NOT | Flips all bits in the binary representation of the value |
& | Address-of | Returns the memory address of a variable |
* | Dereference | Accesses the value stored at a given memory address |
sizeof() | Sizeof Operator | Tells you how many bytes a variable or data type takes up in memory |
Best Practices for Using Unary Operators in C
Unary operators are simple to use, but like everything in programming, a little care goes a long way. Misusing them, especially in more complex expressions, can make your code harder to read or even introduce subtle bugs.
Here are some tried-and-true best practices to keep in mind when working with unary operators in C:
1. Use a Prefix When You Don’t Need the Original Value
When using the increment or decrement operators, try to use the prefix form when you do not need the original value before the change, as it is more efficient and faster.
2. Avoid Overusing Unary Operators in Complex Expressions
While it is tempting to write compact code, try not to chain multiple unary operators in one line, as it can get confusing when debugging.
int result = ++a + a++; // Not recommended
Instead, you can break it into multiple lines so it’s easier to follow for your future self.
3. Use Parentheses for Clarity
Writing clear code that is easy to read is paramount, especially when you are working with a large team. Add parentheses in your code to show the intended order of operations.
if (!(a > 5)) // Clearer than if !a > 5
This comparison checks if a is not greater than 5.
- If a = 6, then a > 5 is true (1)
- If a = 4, then a > 5 is false (0)
4. Don’t Use Logical NOT (!) as a Replacement for Meaningful Conditions
The ! operator is great, but be sure it’s making your logic more readable, not less. For example:
if (!isAvailable) // Good: clear meaning
if (!a == 1) // Confusing: avoid this
5. Be Cautious with Pointers and Dereferencing
Always make sure your pointer is valid before dereferencing it with *. Accessing an invalid memory address can crash your program.
if (ptr != NULL) {
printf("%d", *ptr);
}
6. Use sizeof() Instead of Hardcoding Sizes
Instead of assuming the size of a type (e.g., 4 bytes for int), always use sizeof() for portability and accuracy.
malloc(10 * sizeof(int)); // Safer and clearer
7. Stick to a Consistent Style
Whether you prefer i++ or ++i, consistency in your codebase is key. Pick a style and stick with it across your project.
Pros and Cons of Using Unary Operators
Unary operators in C are incredibly powerful and help you write cleaner and shorter code. But like any tool, they come with trade-offs. Let’s have a look at the pros and cons of using unary operators in C.
Pros | Cons |
Shorter, cleaner syntax | Can reduce code clarity if misused |
Ideal for loops and flags | Prefix/postfix can cause confusion |
Powerful with pointers | Easy to introduce side effects |
Generally efficient | Needs caution in expressions |
Practice Problems on Unary Operators in C
Now that we have a solid understanding of the various types of operators at our disposal and their pros and cons, let’s go ahead and test how much we have learned.
- Complete the line to increment a before assigning it to b.
int main() {
int a = 5;
int b = // code goes here;
printf("a = %d, b = %d", a, b);
return 0;
}
Goal: Make b equal to 6 and a equal to 6.
- Use the logical NOT operator to trigger the message when isAvailable is 0.
int main() {
int isAvailable = 0;
if (// code goes here) {
printf("Item is not available");
}
return 0;
}
- What do you think this code will print?
int main() {
int a = 0;
printf("%d", ~a); // code goes here
return 0;
}
Conclusion
Even though unary operators in C might look like a small topic at first glance, once you start using them, you won’t be able to imagine life without them. They make the code more compact and let you do various operations on a single operand. Understanding how each unary operator works and when to use it will give you more control over your programs and help you avoid subtle bugs that trip up many beginners.
If you are looking to build your C programming skills with projects and a structured curriculum, check out our free C programming certification course.
Unary Operator in C – FAQs
1. What is the most commonly used unary operator in C?
Ans. The most commonly used operator in C is the increment operator(++). You will see it all the time in loops, where it helps iterate through elements or control repetition.
2. How does unary minus differ from binary minus?
Ans. The unary minus (-a) flips the sign of a single value, for example, turning 5 into -5.
The binary minus (a – b) performs subtraction between two values.
3. When should I use prefix over postfix increment?
Ans. Use prefix (++a) when you want the value to be updated before it’s used in an expression. Use postfix (a++) when you want the original value to be used before it’s incremented.
In loops, both forms are often interchangeable, but the prefix is slightly more efficient in some cases because it avoids creating a temporary copy.
4. Can I chain multiple unary operators in one line?
Ans. Yes, you can, but it’s usually not a good idea. Chaining too many unary operations—like *++ptr or –*a++, can make your code confusing and error-prone. It’s best to keep expressions clean and easy to read, especially if you’re just starting out.
5. Is sizeof a function or an operator?
Ans. Although it looks like a function, sizeof is actually an operator. It’s evaluated at compile-time when used with data types or known variables. You don’t need parentheses when using it with a variable (e.g., sizeof a), but they’re required when used with a type (e.g., sizeof(int)).
6. What is the difference between a unary and a binary operator?
Ans. The main difference lies in how many values (operands) the operator works on:
– A unary operator works with just one operand.
– A binary operator works with two operands.