If s and b are integers then purpose of the following program fragment is to
b = s + b ;
s = b - s ;
b = b - s ;
Answer: C
Correct answer: exchange (swap) the contents of s and b
Trace the three assignments with initial values \( s \) and \( b \):
\[ b \leftarrow s+b, \qquad s \leftarrow (s+b)-s = b, \qquad b \leftarrow (s+b)-b = s \]
so the two values are exchanged without a temporary. The technique is a curiosity rather than good practice: the intermediate sum can overflow, and a compiler produces better code from an ordinary swap using a temporary variable.
Please log in or register to report this question.