tags:

views:

682

answers:

7

Hello.. I have a program like this

int main(){ 

    char c;
    int i; /* counter */
    double d;

    return 0;
}

if I want to comment out char, int and double, and just have return uncommented, can I do it? the comment that's already there stops the comment.. Is there an easy/fast way to comment that out?

+11  A: 
int main(){ 

/*
    char c;
    int i; // counter
    double d;
*/
    return 0;
}
kMike
@Steve's answer is pretty clever, but I find this to be the most common and least likely to leave your dev manager asking questions :)
KennyCason
I dont want to change the comments from /**/ to // as I am writing linux kernel stuff..
pvinis
@KennyCason `#if 0` is quite common and widely understood, and has the added benefit of working in both C and C++.
meagar
@meagar Good to know, I'll make a note of it. I just hadn't seen it used before :)
KennyCason
@KennyCason, you usually won't see it in reviewed and released code, as it is bad practice to leave orphaned blocks of code laying around. But it is a common technique while coding and such.
RBerteig
Yeah, that is a good point :)
KennyCason
+56  A: 
int main(){ 
#if 0
    char c;
    int i; /* counter */
    double d;
#endif
    return 0;
}

Not strictly a comment, but the effect is what you want and it's easy to revert.

This also scales well to larger code blocks, especially if you have an editor that can match the start and end of the #if..#endif.

Steve Townsend
+1 ... and the "preprocessor comments" (unlike `/* ... */`) nest
pmg
#IF FALSE is also an option, and is more explicit
Merlyn Morgan-Graham
awesome! thanks
pvinis
...Except that `FALSE` isn't one of the standard predefined macros and will result in a warning from some compilers under certain conditions (e.g., `-Wundef` being in effect under `gcc`).
Blrfl
Awesome idea :D
Green Code
What all languages support this?
John Isaacks
Clever, but I hope folks only use this to quickly debug something then undo it. I'd hate to scan code like this.
webbiedave
@John Isaacks - C and C++, via the preprocessor. In C# you can use `#if false`, I think. Not sure of others.
Steve Townsend
@webbiedave - cannot speak for the general case but in Visual Studio code that is excluded like this displays in faint grey, making reading what's actually used in the build easy.
Steve Townsend
+4  A: 

If your compiler supports the // notation for comments (non-standard in C, but quite commonly supported), use an editor that can toggle a whole block of lines with these.

Bruno
`//` comments are available in standard C99 compilers :-)
pmg
Indeed, it depends on which flavour of C is used ;-)
Bruno
+4  A: 

In C99

int main(){ 

//    char c;
//    int i; /* counter */
//    double d;

    return 0;
}
JeremyP
+1  A: 

There are a lot of Editors / IDEs which support commenting/uncommenting with Hotkeys. This is a very useful feature. In Kate/KDevelop the hotkey is Ctrl+D.

This also is described (along with other IDEs supporting this feature) in THIS question.

MOnsDaR
Ctrl+K, Ctrl+C in MSVC++.
Pedro d'Aquino
+7  A: 

If you ever are tempted to put the name of a variable in comments like in the example, do this instead:

int counter; 
iterationx
+3  A: 

I'm partial to:

int main(){ 

#ifdef USE_DISABLED_CODE
    char c;
    int i; /* counter */
    double d;
#endif

    return 0;
}

Use a terse name like 'CODE_REMOVED_FOR_TESTING_PURPOSES' or 'REMOVED_FROM_E3_BUILD', and don't define it, and you've left yourself a terse comment about why the code is disabled (which will show up if you do a find in all files for #ifdef).

Clinton Blackmore
I recently (in 2010) removed #ifdef POST_JUNE_DEVELOPMENT blocks from about 30 files. No-one is now sure which year was under discussion (probably about 1994, but it might have been older than that; we changed CM systems about then, so older history is lost), much less what the planned 'post June development' was going to be. You should probably document the meaning of the define in at least one place.
Jonathan Leffler
I use this when trading off approaches. I try to use a phrase that describes what the block *meant* rather than anything quite as vague as `POST_JUNE_DEVELOPMENT`. I know I'm going to be seeing this code again a few years later and wondering who June was, what post she held, or why her departure allowed something to develop. ;-)
RBerteig