Comment out a block by uncommenting a line

Probably old news to many C++ experts here. But it just occurred to me that one can easily comment out a block, by uncommenting a line. All one have to do is remove one character:

Example:

    [glow]/[/glow]/*   Uncomment this line to comment out a block
    ... some test or debugging code
    //*/ // End of block. Do not uncomment this line!

The offending characer to remove or add back in is highlighted.

EDIT: I forgot to add an additional // in front of the end-of-block comment text. :stuck_out_tongue: Fixed.

EDIT #2: Or alternatively:

    [glow]/[/glow]/*   Uncomment this line to comment out a block
    ... some test or debugging code
    // End of block. Do not uncomment this line! */

Of course one must not have text in the last line either. Originally I just had this:

    [glow]/[/glow]/*
    ... some test or debugging code
    //*/

But wanting to fix it a bit before posting I messed it up a bit.

But wanting to fix it a bit before posting I messed it up a bit.

This may indicate that it is a bit fragile.

The colour coding in the Arduino IDE helps show what is happening.

The traditional C way to remove code (or make it inactive is)

#if 0
           any stuff
           that needs switching off
           i.e. not part of the program
#endif

The nice thing about this technique is that it nests nicely, and you only need change the #if 0 to #if 1 to switch it all back on.

The problem with the comment technique is you may have to edit in several places, which makes it a bit more error prone.

Also, #if supports proper boolean tests (at compile time), and #else.

#if 0
           any stuff
           that needs switching off
           i.e. not part of the program
#else
           stuff that is needed
           when the other stuff isn't included
#endif

It is also possible to create variables which the compiler uses, so you can do arithmetic at compile time, and include code accordingly.

If you're interested in this sort of thing, look at the Arduino libraries, which use these techniques extensively to adjust for the differences between the different processors used on various Arduino's.

You'll find stuff like this:

#if !defined(__AVR_ATmega8__)
      sbi(TCCR0A, WGM01);
      sbi(TCCR0A, WGM00);
#endif  
      // set timer 0 prescale factor to 64
#if defined(__AVR_ATmega8__)
      sbi(TCCR0, CS01);
      sbi(TCCR0, CS00);
#else
      sbi(TCCR0B, CS01);
      sbi(TCCR0B, CS00);
#endif
      // enable timer 0 overflow interrupt
#if defined(__AVR_ATmega8__)
      sbi(TIMSK, TOIE0);
#else
      sbi(TIMSK0, TOIE0);
#endif

HTH
GB

The nice thing about this technique is that it nests nicely, and you only need change the #if 0 to #if 1 to switch it all back on.

Thanks for the explanation!

Your welcome.

I forgot to explain how things like this work

#if defined(__AVR_ATmega8__)
      sbi(TCCR0, CS01);
      sbi(TCCR0, CS00);
#else
      sbi(TCCR0B, CS01);
      sbi(TCCR0B, CS00);
#endif

Somewhere there may be a bit of code which says:

#define __AVR_ATmega8__

Then the two statements

#if defined(__AVR_ATmega8__)
#if ! defined(__AVR_ATmega8__)

can be used to include or exclude code.
The second one, ! defined(), is true when something has not been #define'd.

A nifty thing about #define'd names is they can be defined when running the compiler, without even editing the source program. I think this is how the Arduino IDE tells the compiler which board has been chosen.

I don't think this is available from the Arduino IDE to users, but maybe someone will correct me.

If you want to find out more, google for "C pre-processor", or CPP.
Wikipedia has this to say C preprocessor - Wikipedia

The C pre-processor is a little programming language in it's own right (but no loops). It can be used for more than this.
[edit]For example

#include <Stepper.h>

inserts some program code which lets your program easily use the Stepper motor library.[/edit]

HTH
GB

Another good reason for not using the commenting method is that C doesn't like nested block comments, so if any of your debug code used them, you'd run into difficulties.

Embrace the C preprocessor!

Doh!
Extremely good point!

This doesn't work:

/*
/* A simple comment about 'pin' */
int pin = 7;
*/

The Arduino IDE does help because it shows the 'live' code in colour (including the black '*/'), and not grey. But in monochrome it'd be difficult to spot.

Thanks Groove.
GB

PS - Here is a comment about nested comments.

Does the Arduino IDE support the #if (stuff) #endif system?

Am I right in thinking that the #if is an instruction to the compiler, and that if the condition is false, the compiler just skips over the "stuff", and thus it carries no penalty in terms of program size?

Thanks...

Does the Arduino IDE support the #if (stuff) #endif system?

Yes, the language is full C++, with nothing taken away.

Am I right in thinking that the #if is an instruction to the compiler, and that if the condition is false, the compiler just skips over the "stuff", and thus it carries no penalty in terms of program size?

Yes, exactly.

GB

(the general impemetation details vary from system to system, so the work might be done by a program called the C pre-processor, cpp, rather than the compiler, but the effect is the same)

Embrace the C preprocessor!

I'm trying, I'm trying :slight_smile:

When I think about it I actually have bumped into it before, not least the "#include " command. And while I did understand the idea behind those preprocessor commands, I haven't really used them a lot.. So of course suddenly I thought I had a good idea about commenting out a block at a time :stuck_out_tongue:

Still, if I may be so bold as to humbly say this, the commenting method, while not nestable, make the code show up in a commented-out color in the IDE.
But yeah, I see the advantages of the preprocessor. And also to preserve comments for comments/documentation (usually).

Thanks for answering all!

See also:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1271517197

Great info, thanks!

I have seens those defines which seems to transfers arguments, but I haven't looked into them yet. So, as far as I can see, when the define is not defined (or is it rather, defined as nothing, like in your example: #define DEBUG_PRINT(x) ), it all disappears when used in the source code.

Still, if I may be so bold as to humbly say this, the commenting method, while not nestable, make the code show up in a commented-out color in the IDE.

I agree, some IDE's do highlight code inside #if ... #endif, but it isn't as clear.

So, as far as I can see, when the define is not defined (or is it rather, defined as nothing, like in your example: #define DEBUG_PRINT(x) ), it all disappears when used in the source code.

Be careful, there are two different things happening.

#define FOO
creates a symbol (name) that the preprocessor knows about, and that is different from it not knowing the symbol.
It is so different that there is a mechanism for forcing it to forget:
#undef FOO
makes the preprocessor forget it has seen #define FOO.
From that point on in the code, every use of FOO (in code, not comments or strings) would be an error.

#define PRINT(x) Serial.println(x)
and
#define PRINT(x)

Both create a symbol, just the definition (for example the text "Serial.println(x)") are different.

Every use of PRINT(...) is okay, but one only inserts nothing. That is different from forgetting it had ever seen PRINT().

Does that make sense?

The other point to note is
#ifdef FOO
and
#if defined(FOO)
are the same, but the second form lets you write more complex tests.

HTH
GB