37
$\begingroup$

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?

$\endgroup$
17
  • 13
    $\begingroup$ Is this actually something that just happened to you? $\endgroup$
    – Dr Xorile
    Jun 23, 2016 at 22:06
  • 12
    $\begingroup$ Lol I haven't seen such crappy version control system in real life yet $\endgroup$
    – user24855
    Jun 23, 2016 at 22:22
  • 25
    $\begingroup$ Just add £ in any line. The system will fail to compile, the code line can not be changed. The company is out of bussiness and the printf will never ever be executed again.... (I chose £ on purpose, becuse it is going to be an obsolete character soon... 8-[ ) $\endgroup$
    – BmyGuest
    Jun 24, 2016 at 7:07
  • 9
    $\begingroup$ This might fit better over at CodeGolf. $\endgroup$ Jun 24, 2016 at 12:19
  • 5
    $\begingroup$ This is definitely code-golf and should belong there. $\endgroup$ Jun 24, 2016 at 16:03

14 Answers 14

50
$\begingroup$

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.

$\endgroup$
0
53
$\begingroup$

Works in gcc, with 3 characters on the line above:

//\   (same-line comment + line continuation)

Side note:   To reactivate the printf() add a blank line above it.

//\

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

Another side note:   As learned from an answer to ‘‘Loopy’’ C loop, with 5 characters this approach can really puzzle that nasty boss by using, what else, ??.

//??/
  ( ??/ is a trigraph that translates to \ in service of an obsolete text-encoding system from the same era, no doubt, as the source code management system in question. )

Gag variation, 1 character total, no new line: (not allowed, appends to an existing line)

\
(added to the end of the previous line, if that line happens to already have a “//...” comment)

$\endgroup$
5
  • 17
    $\begingroup$ This makes me uncomfortable. $\endgroup$
    – Will
    Jun 23, 2016 at 22:11
  • 1
    $\begingroup$ The gag version makes me more uncomfortable :p $\endgroup$
    – user24855
    Jun 23, 2016 at 22:57
  • 1
    $\begingroup$ the gag variation would be even funnier if the line before the print statement would have been a highly used macro :D $\endgroup$
    – hoffmale
    Jun 26, 2016 at 13:49
  • 1
    $\begingroup$ the question states "shortest". not fastest. + this is better by utilizing the pre processor... so the bin doesnt even have the bad code. $\endgroup$
    – Tomer W
    Jun 26, 2016 at 15:22
  • $\begingroup$ @TomerW:  (1) Interesting point, but are you sure?  I believe that comments are not handled by the preprocessor.  (2) With the accepted answer, any decent compiler will see that the new code is testing a constant, and that the printf can never be executed, and accordingly it will not generate the “dead code”. $\endgroup$ Jun 26, 2016 at 18:14
24
$\begingroup$

How about

/* before and */ after

2 lines, 4 characters.

$\endgroup$
2
  • $\begingroup$ Simple and good, but not good enough :) $\endgroup$
    – user24855
    Jun 23, 2016 at 22:24
  • 7
    $\begingroup$ Wasn't really expecting it to win as I don't know c code. Really just a 2 minute search on how comments work. $\endgroup$
    – gtwebb
    Jun 23, 2016 at 22:33
20
$\begingroup$

Assume that's not the first time Bob had that problem, therefore he added on a central include.

extern int gshow_me; // initialized with 0 in c file
#define _ if(gshow_me)
#define _x gshow_me^=1;
#define _0 gshow_me=0;

Then he just adds a line containing a single _ before (one line one char), like:

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

To recover the text add _x before the _ line and _0 after the printf-line, like:

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

Using the int variable the compiler doesn't complain for constant condition. _x inverts the meaning of _, so it can be reenabled.

Adding more _x in front toggles printing on and off.

Though .. the crappy file system won't allow to store the additional cvs data, so the filesystem problem cannot be resolved.

$\endgroup$
8
  • 2
    $\begingroup$ Vote of approval for minimal marginal cost, for built-in reversibility, for showing the solution in place and for no compile-time warnings. Not crappy at all! $\endgroup$
    – humn
    Jun 24, 2016 at 2:05
  • $\begingroup$ I'd been thinking that he'd meant to use dprintf which he'd defined elsewhere as a macro that could etiher chain to "printf" or a do-nothing function. Fortunately, since he doesn't believe in indenting, that would allow him to use `d` on the line preceding the printf. $\endgroup$
    – supercat
    Jun 24, 2016 at 22:19
  • 1
    $\begingroup$ I liked this question so much, I was about to re-ask it in a reversal scenario, that for whatever reason, you needed the line back, and you've not only answered the original question, but added the reversal as well. Great stuff. And now it just means I have to come up with my own original idea lol $\endgroup$
    – Madivad
    Jun 26, 2016 at 2:15
  • $\begingroup$ Why are you using ^=1?  (I know what it does; I'm asking why you use it rather than just _0 and _1.)  This way, if this is in a loop, the code (the printf) will execute on the first iteration and no others.   (Unless the definition for _0 is a typo, and you meant for it to do what its name implies.) $\endgroup$ Jun 26, 2016 at 19:43
  • $\begingroup$ @peregrine Indeed, there was a typo #define _0 gshow_me=1; was wrong, fixed it. _x is for toggling on/off. $\endgroup$
    – lexx9999
    Jun 26, 2016 at 19:51
13
$\begingroup$

Using a GNU language extension, 3 characters on the line before:

1?:

Explanation

Null coalescing operator - https://en.wikipedia.org/wiki/%3F:#C

$\endgroup$
1
  • 1
    $\begingroup$ Note that this is a GNU extension and does not work in standard (ISO) C. $\endgroup$
    – wovano
    Jul 18, 2022 at 7:30
11
$\begingroup$

some solutions not yet (explicitly) mentioned

1 line, 3 chars:

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

2 lines, 5 chars:

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

2 lines, 9 chars:

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

The following solutions don't have to be directly before the printf line, so they might be harder to detect.

1 line, 15 chars: (nasty!)

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

1 line, 24 chars (also nasty):

auto printf=[](char*){};
printf("OK, bug gone - this should make my nasty boss happy");


Also, I tried variants of this (1 line, 2 chars) but it breaks on not having enough arguments :( Maybe there's some variation of printf that I'm missing? (note that this requires no indenting on the line with the printf statement)

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

$\endgroup$
3
  • 1
    $\begingroup$ o.O 10 upvotes and nobody pointed out that technically, 2 of the solutions are c++ only (c++11 or higher) $\endgroup$
    – hoffmale
    Jun 26, 2016 at 22:41
  • 2
    $\begingroup$ The nasty ones break the entire application, especially the fclose one. $\endgroup$
    – orion
    Jun 27, 2016 at 14:29
  • $\begingroup$ That why I marked them as nasty :) Well, the second one (the c++ one) can be scoped, so depending on its position it might only break the relevant function, not the whole program. $\endgroup$
    – hoffmale
    Jun 27, 2016 at 19:59
10
$\begingroup$

Bob could place

if(0)

above the line

$\endgroup$
3
  • 2
    $\begingroup$ There is already an answer exactly like this. $\endgroup$ Jun 24, 2016 at 17:16
  • 1
    $\begingroup$ Sorry, we posted at the same time. $\endgroup$
    – Qfwfq
    Jun 24, 2016 at 17:23
  • 3
    $\begingroup$ @Qfwfq duplicate answers should be deleted. $\endgroup$
    – user64742
    Jun 25, 2016 at 3:53
8
$\begingroup$

This one will depend on the compiler. Some compilers may accept it, others won't. But it's only two characters :-)

#\ which turns the printf into an illegal preprocessor line, which may or may not be ignored.

$\endgroup$
7
$\begingroup$

One line before, four characters:

1?0:

$\endgroup$
1
  • 1
    $\begingroup$ This and its abbreviated companion are especially good because they work even if printf doesn't return a value, unlike Gareth McCaughan's three-character answer. $\endgroup$ Jun 26, 2016 at 19:32
7
$\begingroup$

While that line may be valid in C, it is also a valid line in many other languages, such as Brainfuck. Brainfuck will only interpret the comma as asking for input, and will treat the rest of the characters as a comment. Bob was working in a Brainfuck interpreter instead of a C one. Bob needs to add no new lines.

Obviously this isn't the intended solution, but I just wanted to add some .

$\endgroup$
2
  • $\begingroup$ Seriously?  A comma in a string argument to a printf turns the remainder of the string into a comment?  That's one f***ed up language!    :-)    I gotta go and learn me that language … … … NOT! $\endgroup$ Jun 26, 2016 at 19:49
  • 1
    $\begingroup$ How is this an answer ? $\endgroup$
    – user23151
    Jun 27, 2016 at 18:41
5
$\begingroup$

Because Bob is a C programmer he knows all about memory and decides to:

memcpy(&"OK, bug gone - this should make my nasty boss happy", &"\0", 1); // copy a null character to the start of the string in the static memory segment

which would probably:

Cause a segmentation fault (compiler / system dependant). If not, then the printf will print nothing, and the new code could be hidden somewhere else in the program (before execution of the printf).

1 line, 73 characters

$\endgroup$
1
  • 1
    $\begingroup$ Actually, that gets compiler warnings because of the &s — take them out. Also, strcpy would work just as well. $\endgroup$ Jun 26, 2016 at 20:20
3
$\begingroup$

Another possible answer is:

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

If the

macro is defined i.e. we have a #define DEBUG something

Then

the printfstatement will be printed, else nothing.

Note:

These(the macros) are used in online judges.

$\endgroup$
1
  • 2
    $\begingroup$ Unless you really want to make it a conditional (which is out of the scope of this question), you could reduce the character count by using #if 0. $\endgroup$ Jun 26, 2016 at 20:30
2
$\begingroup$

He can add the following line of code

1/0;

On the line before.

1 line of code, 4 characters

Also,

This code will cause the system to crash, but no such restriction was given so... Yay?

$\endgroup$
1
  • 3
    $\begingroup$ I guess thia will cause his nasty boss to order him to debug, which will create more printf problems lol $\endgroup$
    – user24855
    Jun 27, 2016 at 13:47
2
$\begingroup$

He can simple add following line after printf:

printf("\r");

Or can add following line above

1?0:

$\endgroup$
2
  • 4
    $\begingroup$ On my system, printing \r returns the caret back to the start of the line, but keeps the contents. E.g. the final output of printf("aaaa\rbb") would be bbaa. $\endgroup$
    – Kroltan
    Jun 24, 2016 at 14:36
  • 1
    $\begingroup$ And the second answer was suggested over eight hours ago.  After you think of an answer, you should read all the other answers that have already been posted (yes, I know; the spoiler markdown makes it such a pain; it takes forever!    :-)    ) and, if somebody else has already posted your idea, and you aren't improving on it, then don't.  Just keep your hands where we can see them, and walk away.     :-)    ⁠ $\endgroup$ Jun 26, 2016 at 19:58