โ C Unary and Ternary Operators โ Prefix, Postfix, and Short if-else
๐งฒ Introduction โ What Are Unary and Ternary Operators in C?
Unary and ternary operators in C provide concise ways to manipulate variables and evaluate conditions. Unary operators act on a single operand, such as incrementing or negating a value, while the ternary operator evaluates a condition and chooses between two values in a compact if-else
form.
๐ฏ In this guide, youโll learn:
- What unary and ternary operators are
- The syntax and behavior of each operator
- Examples demonstrating how they work
- Use cases and best practices
โ Unary Operators in C
Unary operators require only one operand. They are commonly used for incrementing, decrementing, and negating values.
๐น List of Unary Operators
Operator | Name | Description | Example |
---|---|---|---|
+ | Unary Plus | Returns the positive value (rarely used) | +a |
- | Unary Minus | Negates the value (arithmetic negative) | -a |
++ | Increment | Increases value by 1 (prefix/postfix) | ++a , a++ |
-- | Decrement | Decreases value by 1 (prefix/postfix) | --a , a-- |
! | Logical NOT | Inverts Boolean (true to false, vice versa) | !flag |
~ | Bitwise NOT | Flips all bits (1โs complement) | ~a |
* | Dereference | Gets value from pointer (used with pointers) | *ptr |
& | Address-of | Gets memory address of variable | &a |
sizeof | Size Operator | Returns size in bytes of data type/variable | sizeof(int) |
๐งช Example: Increment and Decrement
int x = 5;
printf("%d\n", x++); // Output: 5 (postfix)
printf("%d\n", ++x); // Output: 7 (prefix)
โ Prefix vs Postfix
Form | Syntax | Action |
---|---|---|
Prefix | ++x | Increments, then uses the new value |
Postfix | x++ | Uses the current value, then increments it |
โ Example:
int a = 5;
int b = ++a; // b = 6, a = 6
int c = a++; // c = 6, a = 7
โ Logical NOT and Bitwise NOT
int flag = 0;
printf("%d", !flag); // Output: 1 (Logical NOT)
int num = 5; // Binary: 00000101
printf("%d", ~num); // Output: -6 (Bitwise NOT in 2โs complement)
โ Ternary Operator in C
The ternary operator is the only operator in C that works with three operands. It is a shorthand version of the if-else
condition.
๐น Syntax
condition ? expression_if_true : expression_if_false;
โ Example:
int max = (a > b) ? a : b;
๐งช Ternary Operator in Action
int x = 10, y = 20;
printf("Greater: %d\n", (x > y) ? x : y);
Output:
Greater: 20
๐ Summary โ Recap & Next Steps
Unary and ternary operators provide compact and efficient ways to perform single-variable operations and make quick decisions. Mastering these helps simplify and clean up your C code.
๐ Key Takeaways:
- Unary operators operate on a single operand (
++
,--
,!
,~
, etc.) ++
and--
come in prefix and postfix forms with different behaviors- The ternary operator is a concise replacement for
if-else
expressions - Use unary for optimization and ternary for inline conditionals
โ๏ธ Real-World Relevance:
These operators are used in loop counters, conditions, pointer manipulation, and inline logic across virtually every kind of C program.
โ Frequently Asked Questions (FAQ)
โ What is the difference between ++x
and x++
?
โ
++x
(prefix) increments first, then uses the value.
โ
x++
(postfix) uses the value first, then increments.
โ Can I use multiple unary operators together?
โ Yes, but with caution. For example:
int a = 5;
int b = ++a + a++;
โ ๏ธ This can lead to undefined behavior. Avoid modifying a variable more than once in a single expression.
โ When should I use the ternary operator?
โ Use the ternary operator when you need a simple if-else in one line:
int result = (a > b) ? a : b;
โ Can I nest ternary operators?
โ Yes, but it reduces readability:
int max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
โ What is the output of !0
?
โ
!0
evaluates to 1
because 0
is considered false in C.
Share Now :