33
$\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$
  • 12
    $\begingroup$ Is this actually something that just happened to you? $\endgroup$ – Dr Xorile Jun 23 '16 at 22:06
  • 12
    $\begingroup$ Lol I haven't seen such crappy version control system in real life yet $\endgroup$ – Pigpag Jun 23 '16 at 22:22
  • 24
    $\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 '16 at 7:07
  • 7
    $\begingroup$ This might fit better over at CodeGolf. $\endgroup$ – Engineer Toast Jun 24 '16 at 12:19
  • 5
    $\begingroup$ This is definitely code-golf and should belong there. $\endgroup$ – noɥʇʎԀʎzɐɹƆ Jun 24 '16 at 16:03

14 Answers 14

45
$\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$
51
$\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$
  • 16
    $\begingroup$ This makes me uncomfortable. $\endgroup$ – Will Jun 23 '16 at 22:11
  • 1
    $\begingroup$ The gag version makes me more uncomfortable :p $\endgroup$ – Pigpag Jun 23 '16 at 22:57
  • 6
    $\begingroup$ The second version is not a correct answer.  The "source code management system … does not allow any modification to any lines of code";  "He can only add new lines of code". $\endgroup$ – Peregrine Rook Jun 23 '16 at 23:02
  • 1
    $\begingroup$ Arrrg, good catch, @PeregrineRook $\endgroup$ – humn Jun 23 '16 at 23:03
  • 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 '16 at 13:49
23
$\begingroup$

How about

/* before and */ after

2 lines, 4 characters.

$\endgroup$
  • $\begingroup$ Simple and good, but not good enough :) $\endgroup$ – Pigpag Jun 23 '16 at 22:24
  • 6
    $\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 '16 at 22:33
19
$\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$
  • 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 '16 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 '16 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 '16 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$ – Peregrine Rook Jun 26 '16 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 '16 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$
  • 4
    $\begingroup$ I didn't know that... need to check it out myself :D $\endgroup$ – Pigpag Jun 23 '16 at 22:24
12
$\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$
  • $\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 '16 at 22:41
  • 1
    $\begingroup$ The nasty ones break the entire application, especially the fclose one. $\endgroup$ – orion Jun 27 '16 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 '16 at 19:59
9
$\begingroup$

Bob could place

if(0)

above the line

$\endgroup$
  • 1
    $\begingroup$ There is already an answer exactly like this. $\endgroup$ – haykam Jun 24 '16 at 17:16
  • 1
    $\begingroup$ Sorry, we posted at the same time. $\endgroup$ – Qfwfq Jun 24 '16 at 17:23
  • 2
    $\begingroup$ @Qfwfq duplicate answers should be deleted. $\endgroup$ – The Great Duck Jun 25 '16 at 3:53
8
$\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$
  • $\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$ – Peregrine Rook Jun 26 '16 at 19:49
  • 1
    $\begingroup$ @PeregrineRook Brainfuck is already a highly unusual language. None of the characters within "printf" have specific meaning within the language. It doesn't function even remotely like a C-style language and when you look at how it does work, the way it treats the comma makes a semblance more sense. It's a cool language to look at if you've got time to play around for a while. $\endgroup$ – niemiro Jun 26 '16 at 20:21
  • $\begingroup$ How is this an answer ? $\endgroup$ – A---B Jun 27 '16 at 18:41
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
    $\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$ – Peregrine Rook Jun 26 '16 at 19:32
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
    $\begingroup$ Actually, that gets compiler warnings because of the &s — take them out. Also, strcpy would work just as well. $\endgroup$ – Peregrine Rook Jun 26 '16 at 20:20
3
$\begingroup$

The accepted answer is obviously the best, 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$
  • 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$ – Peregrine Rook Jun 26 '16 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$
  • 2
    $\begingroup$ I guess thia will cause his nasty boss to order him to debug, which will create more printf problems lol $\endgroup$ – Pigpag Jun 27 '16 at 13:47
1
$\begingroup$

He can simple add following line after printf:

printf("\r");

Or can add following line above

1?0:

$\endgroup$
  • 3
    $\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 '16 at 14:36
  • $\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$ – Peregrine Rook Jun 26 '16 at 19:58

protected by Aza Jun 26 '16 at 8:47

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.