Unary operators in C language: from the list 1) ! 2) sizeof 3) ~ 4) &&, which subset consists only of unary operators?

Difficulty: Easy

Correct Answer: 1, 2, 3

Explanation:


Introduction / Context:
Operator arity (unary, binary, or ternary) determines how many operands an operator uses. This is essential when reasoning about expressions, precedence, and the effects on operands. The question asks you to identify which listed operators are strictly unary in C.



Given Data / Assumptions:

  • Operators provided: ! (logical NOT), sizeof (compile-time size), ~ (bitwise NOT), && (logical AND).
  • Standard C semantics apply.


Concept / Approach:
Unary operators take one operand. In C, !, sizeof, and ~ each operate on a single operand: !a flips truthiness, sizeof(T or expr) yields a size, and ~a flips all bits. By contrast, && is a binary logical operator that requires two operands and short-circuits based on the left operand.



Step-by-Step Solution:
Identify each operator's arity.Group the unary ones: !, sizeof, ~.Exclude && because it requires two operands.Therefore, the correct subset is 1, 2, 3.



Verification / Alternative check:
Compile sample expressions like !x, sizeof x, ~x, and x && y; observe that only the last demands two operands.



Why Other Options Are Wrong:

  • 1, 2 and 1, 3 omit one valid unary operator.
  • 2, 4 incorrectly includes a binary operator.


Common Pitfalls:
Confusing bitwise NOT (~) with logical NOT (!); forgetting that sizeof is an operator, not a function.



Final Answer:
1, 2, 3.

More Questions from Expressions

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion