DEV Community

Cover image for Commenting out the Comment ?
Rahul Dahal
Rahul Dahal

Posted on

Commenting out the Comment ?

Disclaimer: This is not essentially a productivity tip. But rather, something I experienced while coding.
Enter fullscreen mode Exit fullscreen mode

The Context

I was casually sharing some programming differences to my friends. The programming language was C, the topic was:

Difference between if-else if and if if conditions.
And the code was like:

void main(){

    // if-if statement

    if(2+2 == 4){
       printf("\nI execute because 2+2 is 4.");
    }
    if(5+3 == 8){
       printf("\nI execute because 5+3 is 8.");
    }

    // if-else if statement

    if(9+8 == 17){
       printf("\nI execute because 9+8 is 17.");
    }else if(9+3 = 12){
       printf("\nI execute because 9+3 is 12.")
    }
}
Enter fullscreen mode Exit fullscreen mode

At first, I wanted to demonstrate the if-if statement. Therefore I commented the if-else if statement out.

void main(){

    // if-if statement

    if(2+2 == 4){
       printf("\nI execute because 2+2 is 4.");
    }
    if(5+3 == 8){
       printf("\nI execute because 5+3 is 8.");
    }

    // if-else if statement

    /*
    if(9+8 == 17){
       printf("\nI execute because 9+8 is 17.");
    }else if(9+3 = 12){
       printf("\nI execute because 9+3 is 12.")
    }
    */
}
Enter fullscreen mode Exit fullscreen mode

After the first demonstration, I had to demonstrate the if-else if statement. So, in order to make it executable, obviously I had to uncomment it by removing those /* */ symbols (aka multiple line comment).

But, I being Rahul Dahal, had a crazy thought like,

"What if I put // (aka single line comment) before the /* & */ (multiple line comment start and end), to comment them out and make the text inside it executable ?"

Basically, I wanted to check if we can comment out a comment. So that comment will no longer be a comment.

I did that.

void main(){

    // if-if statement

    if(2+2 == 4){
       printf("\nI execute because 2+2 is 4.");
    }
    if(5+3 == 8){
       printf("\nI execute because 5+3 is 8.");
    }

    // if-else if statement

    // /*
    if(9+8 == 17){
       printf("\nI execute because 9+8 is 17.");
    }else if(9+3 = 12){
       printf("\nI execute because 9+3 is 12.")
    }
    // */
}
Enter fullscreen mode Exit fullscreen mode

And guess what, it worked!

That trick worked. As I expected.

But Why? Why did it work ?

Answer,
Actually what happens is, whatever we type after the comment starter (both // & /*), no matter what, the compiler understands it as a comment text.

That is why the /* itself was treated as a normal text.

And since, /* is treated as a normal text, the code after it will no longer be commented out. Hence, making it executable.

Top comments (1)

Collapse
 
nidhi01 profile image
Mahi

Great informative post euchre game Thanks for sharing your ideas. This is helpful