โž• C Operators
Estimated reading: 3 minutes 9 views

โž• 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

OperatorNameDescriptionExample
+Unary PlusReturns the positive value (rarely used)+a
-Unary MinusNegates the value (arithmetic negative)-a
++IncrementIncreases value by 1 (prefix/postfix)++a, a++
--DecrementDecreases value by 1 (prefix/postfix)--a, a--
!Logical NOTInverts Boolean (true to false, vice versa)!flag
~Bitwise NOTFlips all bits (1โ€™s complement)~a
*DereferenceGets value from pointer (used with pointers)*ptr
&Address-ofGets memory address of variable&a
sizeofSize OperatorReturns size in bytes of data type/variablesizeof(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

FormSyntaxAction
Prefix++xIncrements, then uses the new value
Postfixx++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 :

Leave a Reply

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

Share

โž• C Unary & Ternary Operators

Or Copy Link

CONTENTS
Scroll to Top