newbie-question: comments

M

merman

Hi,

Can I comment bigger code-chunks with #if and #if def?
I read this - a (simple;-)) sample would be great.

Thanks.

Thomas
 
N

Nicolas Pavlidis

merman said:
Hi,
Hi!

Can I comment bigger code-chunks with #if and #if def?
I read this - a (simple;-)) sample would be great.

Why not using the comment sequences /* and */

To answer your question:

#ifdenf COMMENT_THIS_OUT
// code
#endif

But you have do define COMMENT_THIS_OUT somewhere, the best place is
IMHO while compiling, if you use gcc it could look like this:

gcc -Wall -o test -DCOMMENT_THIS_OUT test.c

Or see the manual for the compiler of your choice.

HTH && Kind regards,
Nicolas
 
M

Michael Mair

Hello Thomas,

Can I comment bigger code-chunks with #if and #if def?

That would be #ifdef/#ifndef/#if defined/#if !defined ... #endif,
but in principle: Yes.

I read this - a (simple;-)) sample would be great.

#define COMMENT_TEST

int main (void)
{
....
#ifndef COMMENT_TEST
/* Code with many comments */
....
#endif
....
return 0;
}

the #if... preprocessor directives are used for conditional
compilation, so you say: If COMMENT_TEST is not defined, keep
this code; otherwise everything between #ifndef and #endif
is not "seen" by the compiler.
You have, however, another problem:
If there are already #if... directives in your code, you can
unintentionally get errors:

/*
#define COMMENT_TEST
*/
#include <stdio.h>

int main (void)
{
puts("NCT");
#ifndef COMMENT_TEST
/* Code with many comments */
puts("CT");
#ifdef SPECIAL_CASE
puts("SC");
#else
puts("NSC");
#endif /* !defined COMMENT_TEST */
puts("NSC");
puts("NCT");
#endif /* SPECIAL_CASE */
puts("NCT");
return 0;
}

Now, #endif /* !defined COMMENT_TEST */ is the nearest #endif
to #ifdef SPECIAL_CASE, so it belongs to that if; that is, we would
expect that for SPECIAL_CASE, we get an "SC" output line, but
in truth we get "SC" and "NSC" which is not what we intended
(from the comments at least).

If you are using C99, I would recommend using an editor which
can comment selected lines with // line comments and can get
rid of the first line comments of a line in a selected region.


Cheers
Michael
 
S

s_t_o_r_a_g_e

merman said:
Hi,

Can I comment bigger code-chunks with #if and #if def?
I read this - a (simple;-)) sample would be great.

Thanks.

Thomas

i always use something like this for bigger code chunks.

int main (void)
{
....
#if 0
/* Code which you dont wanna compile */
....
#endif
....
....
}
 
K

Keith Thompson

Nicolas Pavlidis said:
Why not using the comment sequences /* and */

Because /**/ comments don't nest.

For example:

/* Commenting out a block of code:
statement1; /* internal comment */
statement2;
*/

The comment is terminated by the end of the "internal comment", and
"statement2;" is visible to the compiler; the trailing "*/" is then a
syntax error.
 
G

Giorgos Keramidas

Nicolas Pavlidis said:
Why not using the comment sequences /* and */

Because they don't nest. This doesn't quite work, because the comment
terminates at the first occurence of */.

/*
int x; /* This is a comment. */
*/
To answer your question:

#ifdenf COMMENT_THIS_OUT
// code
#endif

A common way of commenting out regions of the source is to use `#if 0':

#if 0
int x; /* This is a comment. */
#endif

This requires manual labor to remove the #if 0 blocks later on, but it
shouldn't be much of a problem for regions of the source that are known
to be buggy or even not C code at all. For parts of the source that are
working fine but depend on some host system characteristic a properly
named #ifdef conditional that enables the features on demand is more
appropriate anyway ;-)

#ifdef HAVE_FOO
int x; /* This is a comment. */
#endif

The negative logic of COMMENT_THIS_OUT doesn't quite fit my taste, but I
have to admit that it works and this is more a style/taste issue than
something related to the way #ifdef works.
 
F

Flash Gordon

A common way of commenting out regions of the source is to use `#if
0':

#if 0
int x; /* This is a comment. */
#endif

This requires manual labor to remove the #if 0 blocks later on, but it
shouldn't be much of a problem for regions of the source that are
known to be buggy or even not C code at all.

Depending on what you are doing and whether you might want to disable
the code again you can just replace "#if 0" with "#if 1" to switch the
code block back on. I'm sure you (Giorgos) know this, the comment is
more for the OP.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top