In C programming, evaluate the character printed by this expression that mixes arithmetic and bitwise operators. What will be the output? #include<stdio.h> int main() { printf("%c ", ~('C' * -1)); return 0; }

Difficulty: Medium

Correct Answer: B

Explanation:


Introduction / Context:
This program tests how C promotes character constants to int, how unary negation works on integers, and how the bitwise NOT operator interacts with two’s-complement arithmetic. Understanding usual arithmetic conversions and identities like ~x = -x - 1 is key to predicting the exact character printed.



Given Data / Assumptions:

  • The character constant 'C' has ASCII code 67.
  • In expressions, character constants are promoted to type int.
  • Two’s-complement arithmetic is assumed (the de facto representation on modern systems).
  • The operator precedence is: multiplication before unary ~, and unary - binds tightly to the literal -1.


Concept / Approach:
The expression is ~('C' * -1). First compute the product as an int, then apply the bitwise NOT. For two’s complement integers, there is a helpful identity: ~x = -x - 1. We will use this on x = -67 to avoid writing long bit patterns.



Step-by-Step Solution:
Interpret 'C' → 67.Compute product: 67 * -1 = -67.Apply identity: ~(-67) = -(-67) - 1 = 67 - 1 = 66.Convert 66 to a character for %c → ASCII 66 is 'B'.printf prints B followed by a newline.



Verification / Alternative check:
If you explicitly write out two’s-complement bits, -67 has all ones except the magnitude pattern; inverting flips all bits, which numerically equals 66. Printing as %d would show 66; as %c it shows 'B'.



Why Other Options Are Wrong:
'A' and 'D' are off by more than one step and ignore the exact arithmetic. 'C' would imply no change after the operations. The “implementation-defined” choice does not apply here because the arithmetic result is well defined under two’s complement, which is overwhelmingly standard.



Common Pitfalls:
Confusing bitwise NOT with arithmetic negation, or assuming 'C' remains a char without integer promotion. Also, forgetting that %c prints the character for the computed code.



Final Answer:
B

Discussion & Comments

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