25

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?

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
pvinis
  • 4,059
  • 5
  • 39
  • 59
  • 11
    In general I would use `//` for one line comments at the end of the line. So it would be `// counter` and you could easily wrap the block in `/*...*/` – Felix Kling Oct 15 '10 at 09:53

6 Answers6

81
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
  • 53,498
  • 9
  • 91
  • 140
13
int main(){ 

/*
    char c;
    int i; // counter
    double d;
*/
    return 0;
}
mlusiak
  • 1,054
  • 14
  • 28
  • 4
    @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 :) – Kenny Cason Oct 15 '10 at 10:31
  • I dont want to change the comments from /**/ to // as I am writing linux kernel stuff.. – pvinis Oct 15 '10 at 11:37
  • 4
    @KennyCason `#if 0` is quite common and widely understood, and has the added benefit of working in both C and C++. – user229044 Oct 15 '10 at 14:07
  • @meagar Good to know, I'll make a note of it. I just hadn't seen it used before :) – Kenny Cason Oct 15 '10 at 14:11
  • 1
    @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 Oct 16 '10 at 21:59
5

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
  • 119,590
  • 31
  • 270
  • 376
5

In C99

int main(){ 

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

    return 0;
}
JeremyP
  • 84,577
  • 15
  • 123
  • 161
5

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
  • 2,427
  • 2
  • 24
  • 31
  • 1
    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 Oct 15 '10 at 19:12
  • 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 Oct 16 '10 at 22:04
3

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.

Community
  • 1
  • 1
MOnsDaR
  • 8,401
  • 8
  • 49
  • 70