In C, what will be the output of the following program that uses an unsigned int and the bitwise NOT operator? main() { unsigned int a = 0xffff; /* hexadecimal constant */ ~a; printf("%x", a); }

Difficulty: Medium

Correct Answer: ffff

Explanation:


Introduction / Context:
This question tests understanding of how expressions, side effects and the bitwise NOT operator work in C when applied to an unsigned int. Many learners focus only on the mathematical effect of the operator and forget that the result of an expression is not automatically stored back into the original variable unless there is an explicit assignment. The program also uses hexadecimal formatting with printf, so you must combine knowledge of bitwise operators and output formatting.


Given Data / Assumptions:

  • a is declared as unsigned int and initialised with the constant 0xffff, which is 65535 in decimal.
  • The expression ~a is evaluated but its result is not assigned back to a.
  • printf is called with the format "%x" to print an unsigned value in lowercase hexadecimal.
  • We assume a typical implementation where unsigned int is at least 16 bits or larger.


Concept / Approach:
The key idea is that the statement ~a; computes the bitwise complement of a but discards the result, because there is no assignment. Side effects occur only when operators modify objects, for example with +=, *= or the increment operators. A plain unary operator like ~ produces a temporary value. Therefore the value stored in a remains 0xffff. When printf prints a using "%x", it will display that unchanged value in hexadecimal.


Step-by-Step Solution:
Step 1: Start with a = 0xffff, so the stored value is hexadecimal ffff. Step 2: Evaluate the expression ~a. This computes the bitwise complement but does not update a because there is no assignment operator. Step 3: After the expression, the variable a still contains the original value 0xffff. Step 4: Call printf with "%x" and a. The function prints the value of a in hexadecimal without any leading prefix. Step 5: The output is therefore the string "ffff".


Verification / Alternative check:
If the intention were to store the complemented value back into a, the code would need to use a statement like a = ~a; or a ^= 0xffff; instead of just ~a;. Since that is not the case, reasoning about sequence points or integer width is not required. On common compilers, you can quickly test a similar program and see that the printed output is ffff, confirming the analysis.


Why Other Options Are Wrong:
Option B 0000 would be correct only if the complement were assigned back and the width matched exactly, which is not what this code does. Option C 0001 has no basis in the operations shown. Option D suggests that the result depends on the compiler or is undefined, but in this case the behaviour is well defined because the expression is evaluated and discarded and a retains its initial value.


Common Pitfalls:
A common mistake is to assume that writing an expression like ~a; somehow mutates a. In C, expressions without assignment or increment or decrement operators do not change variables. Another pitfall is to overcomplicate the question by worrying about the exact bit width of unsigned int. Here, the important point is that the value of a is never modified, so the initial hexadecimal value is printed unchanged.


Final Answer:
The program prints ffff because the bitwise NOT expression does not assign its result back to a, so printf prints the original value 0xffff in hexadecimal.

More Questions from Programming

Discussion & Comments

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