Thursday, September 26, 2019

computer puzzle - Debugging with printf()


Bob, a C programmer and a believer of printf-debugging, gets hired by a failing software company. The company is not doing well because they adopt an archaic source code management system which does not allow any modification to any lines of code once they are checked in.


As you can imagine, poor Bob is too excited at resolving a bug with his printf debugging technique and accidentally checked in that line of C code:




printf("OK, bug gone - this should make my nasty boss happy");



Now, he desperately wants to silence it. But the stupid system won't allow him to edit that line. He can only add new lines of code around it to cancel its effect. There are many ways to achieve that, but can you find the one which adds the fewest lines, with the fewest characters?



Answer



Well, he can do it by adding



if(0)



on the line before. So that's




one extra line, five extra characters.



Or, slightly better, exploit the fact that



printf returns a value



and make that first line



0&&




for



one line, three characters.



No comments:

Post a Comment

classical mechanics - Moment of a force about a given axis (Torque) - Scalar or vectorial?

I am studying Statics and saw that: The moment of a force about a given axis (or Torque) is defined by the equation: $M_X = (\vec r \times \...