# 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?

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

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.

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)

• This makes me uncomfortable. – Will Jun 23 '16 at 22:11
• The gag version makes me more uncomfortable :p – user24855 Jun 23 '16 at 22:57
• 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". – Peregrine Rook Jun 23 '16 at 23:02
• Arrrg, good catch, @PeregrineRook – humn Jun 23 '16 at 23:03
• the gag variation would be even funnier if the line before the print statement would have been a highly used macro :D – hoffmale Jun 26 '16 at 13:49

/* before and */ after

2 lines, 4 characters.

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

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.

• 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! – humn Jun 24 '16 at 2:05
• 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. – supercat Jun 24 '16 at 22:19
• 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 – Madivad Jun 26 '16 at 2:15
• 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.) – Peregrine Rook Jun 26 '16 at 19:43
• @peregrine Indeed, there was a typo #define _0 gshow_me=1; was wrong, fixed it. _x is for toggling on/off. – lexx9999 Jun 26 '16 at 19:51

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

1?:

Explanation

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

• I didn't know that... need to check it out myself :D – user24855 Jun 23 '16 at 22:24

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");

• o.O 10 upvotes and nobody pointed out that technically, 2 of the solutions are c++ only (c++11 or higher) – hoffmale Jun 26 '16 at 22:41
• The nasty ones break the entire application, especially the fclose one. – orion Jun 27 '16 at 14:29
• 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. – hoffmale Jun 27 '16 at 19:59

Bob could place

if(0)

above the line

• There is already an answer exactly like this. – haykam Jun 24 '16 at 17:16
• Sorry, we posted at the same time. – Qfwfq Jun 24 '16 at 17:23
• @Qfwfq duplicate answers should be deleted. – user64742 Jun 25 '16 at 3:53

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.

One line before, four characters:

1?0:

• 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. – Peregrine Rook Jun 26 '16 at 19:32

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 .

• 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! – Peregrine Rook Jun 26 '16 at 19:49
• @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. – niemiro Jun 26 '16 at 20:21
• How is this an answer ? – A---B Jun 27 '16 at 18:41

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

• Actually, that gets compiler warnings because of the &s — take them out. Also, strcpy would work just as well. – Peregrine Rook Jun 26 '16 at 20:20

#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.

• 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. – Peregrine Rook Jun 26 '16 at 20:30

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?

• I guess thia will cause his nasty boss to order him to debug, which will create more printf problems lol – user24855 Jun 27 '16 at 13:47

He can simple add following line after printf:

printf("\r");

Or can add following line above

1?0:

• 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. – Kroltan Jun 24 '16 at 14:36
• 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.     :-)    ⁠ – Peregrine Rook Jun 26 '16 at 19:58