C Comment Fun

Posted

Hello, I was wasting time doing silly things so I thought I would let you in on the fun.

Occasionally, even my programs have a error when I run them for the first time. You can of course whip out the debugger but sometimes it is quicker and easier to add a diagnostic print statement. Now you have solved your problem and remove the statement, but what if you want it again? You could use a fancy logging framework where you can turn on and off debug messages for sections of your code but that is often overkill. That is where the following commenting techniques come in. These work with C style languages provided you have both C (/* */) and C++ (//) style comments at your disposal.

The Line Comment

This isn't a trick but I will include it for completeness.

fprintf(stderr, "x = %d\n", x);   // Runs
//fprintf(stderr, "x = %d\n", x); // Doesn't run.

Multi-line

This is where the fun starts to happen. You can of course use regular multi-line comments, but then you have to add or remove 4 characters just to include or comment out something, that is way two much work for us programmers. It's not that we are lazy, we are just trying to be efficient.

//*
struct thing *t = get_crazy_thing(); // Runs
fprintf(stderr, "Thing is %s\n", t->good?"good":"bad");
free(t);
//*/

/*
struct thing *t = get_crazy_thing(); // Doesn't Run.
fprintf(stderr, "Thing is %s\n", t->good?"good":"bad");
free(t);
//*/

Multi-line Multi-action

Now that we can comment out a number of lines with a single character lets try to take this further. What if you have two things and you only want to run one. Using the previous technique you can do it in two characters, which isn't bad, but they aren't even on the same line. That sounds like too much work to me (again, wasted time, not laziness).

struct thing *t;
//*
t = get_crazy_thing_using_meathod_1(a); // Runs.
/*/
t = get_crazy_thing_using_meathod_2(b); // Doesn't.
//*/
free(t);

/*
t = get_crazy_thing_using_meathod_1(a); // Doesn't.
/*/
t = get_crazy_thing_using_meathod_2(b); // Does.
//*/
free(t);

Fun with ifs

Most moderately experienced programmers have realized that you can comment out the if line to make a statement always run.

if (condition)
{
	// Will run if condition is true.
	fprintf(stderr, "Yup, it ran!\n");
}

//if (condition)
{
	// Will always run.
	fprintf(stderr, "Yup, it ran!\n")
}

But there is no easy way to make an if statement never run. Especially if there multiline comments inside of it. This is where our previous technique comes in.

/*
if (0)
/*/
if (condition)
//*/
{
	// Will run if condition is true.
	fprintf(stderr, "Yup, it ran!\n");
}

//*
if (0)
/*/
if (condition)
//*/
{
	// Will never run.
	fprintf(stderr, "Yup, it ran!\n")
}

This is rarely useful (are you really reading this article for that reason). But we can use a block to comment out code with multi line comments inside of it (D's nesting comments are genius).

//if(0)
{
	/* This is a really long comment
	 * describing the next line.
	 * It prints "Yup, it ran!" followed
	 * by a platform-appropriate newline
	 * to standard error.
	 */
	fprintf(stderr, "Yup, it ran!\n")
}
if(0)
{
	// Effectively commented out.
	
	/* This is a really long comment
	 * describing the next line.
	 * It prints "Yup, it ran!" followed
	 * by a platform-appropriate newline
	 * to standard error.
	 */
	fprintf(stderr, "Yup, it ran!\n")
}

The disadvantage to this is that the code still needs to compile, it can't have syntax errors or other problems.

Conclusion

Conclusion? What is this an essay? I just wasted your time sharing the almost useless commenting structures I have developed over the ages. I hope you enjoy, and I hope you have syntax highlighting enabled.

//*
struct problem = piss_off_coworkers();
/*/
fprintf(stderr, "You don't know if this will print :D\n");
//*/