LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [C] Why do people write comments using /* ... */ ? (https://www.linuxquestions.org/questions/programming-9/%5Bc%5D-why-do-people-write-comments-using-%2A-%2A-4175508667/)

displace 06-20-2014 03:45 PM

[C] Why do people write comments using /* ... */ ?
 
There's something I don't understand and has been bugging me for a while. After browsing the GPL'ed C source code for a lot of programs I noticed that people will often write code comments by using the "/* ... */" notation rather than "//". For example consider the following three comments:
Code:

/*
 * This function will count the number of lines in a string
 */

/* This function will count the number of lines in a string */

// This function will count the number of lines in a string

Why the hell are #1 and #2 so often used in place of #3? I understand that #1 allows you to place comments on multiple lines, and I even welcome it in the file header. But people will often put them inside the code too, which I personally find extremely irritating. Why? Because they interfere whenever I try to comment out large chunks of code by using /* --- */. Arse! Also, why even use #2 instead of #3?

Thoughts?

dugan 06-20-2014 03:49 PM

Use #ifdef 0 to comment out blocks of code.

turtleli 06-20-2014 04:10 PM

The // style comment was introduced to C in the C99 standard. Before that, /* ... */ (and #ifdef 0 as dugan pointed out) was the only valid way to comment C code.

metaschima 06-20-2014 04:24 PM

I also find it annoying. I always use // comments, except when excluding code.

displace 06-20-2014 04:41 PM

Ah I see. The "/* ... */" notation predates the "//". Now it makes sense.

sundialsvcs 06-21-2014 05:52 PM

Do it any way that you prefer, but, within a single module or application, do it consistently. Follow the practices of the folks who worked on the code earlier.

schneidz 06-21-2014 06:41 PM

i'll confess to a bad habit. i block comment like this
Code:

/*/
this is a comment.
this is a comment.
this is a comment.
/*/

it results in a warning but its easier to copy-pasta/dd in vi.

mina86 06-23-2014 07:15 AM

I prefer block-comments to line-comments because word-wrapping (as done for example by mail clients) will not break the code. There is no compelling argument for why line-comments are better.

schneidz 06-23-2014 09:02 AM

^ line-comments are easier to type.

mina86 06-23-2014 10:40 AM

No braces around single-statement if bodies are easier to type as well, but omitting them is a bad idea. For me “easier to type” is not a compelling argument in this instance (and if you're using reasonable editor you probably can configure it to enter comments with some simple keystroke).

metaschima 06-23-2014 11:12 AM

I find it more useful to use // for comments and /**/ for excluding code. I don't care how easy or difficult it is to type, I care about functionality. It is true that #ifdef can also be used in case someone used exclusively /**/ for comments.

rtmistler 06-23-2014 12:46 PM

As many have said, the rules are flexible, also because of the evolution of how comments can be added to code.

I have to admit to being consistently, inconsistent:
Code:

/*
 *
 */

for multi-line block comments, file/function/section headers.
Code:

/* ... */
for one-line comments, also many times comments at the end of a code line. My only rule is that I will not put a comment within an if or loop top line statement:
Code:

while(1) { /* I never put a comment here */


while(1) /* nor here - no matter whether I use /**/ or //
{

To me those are potential, if not really; syntax errors.

I will use "//" to comment out one or two lines, for instance if I have a "potential fix" or even one that's definitely a fix; I may copy the line to change, then use // to comment out the original and make my change in the new copy so that I can leave the original code for a while. Similarly I may use "//" when appending a comment to the end of a line, merely being lazy as a typist.

To comment out code blocks, I do use #if 0

smeezekitty 06-28-2014 12:43 PM

I see absolutely nothing wrong with using /* */
both styles are perfectly valid so there should be no reason for it to irritate you.

That said, I HATE #if 0
It is very non-intuitive and many syntax-highlighters will not detect it so it is east to miss that it is "commented" out

metaschima 06-28-2014 01:43 PM

Quote:

Originally Posted by smeezekitty (Post 5195480)
I see absolutely nothing wrong with using /* */
both styles are perfectly valid so there should be no reason for it to irritate you.

That said, I HATE #if 0
It is very non-intuitive and many syntax-highlighters will not detect it so it is east to miss that it is "commented" out

So then, how do you comment out code ?

smeezekitty 06-28-2014 05:29 PM

For a block with no */ I use /* */
Otherwise I guess you would have to resort to something like that. But I avoid it because it is hard to read

mina86 06-29-2014 08:07 AM

Quote:

Originally Posted by metaschima (Post 5195511)
So then, how do you comment out code ?

If it's not needed, just delete it…

metaschima 06-29-2014 11:03 AM

Quote:

Originally Posted by mina86 (Post 5195825)
If it's not needed, just delete it…

Well yes, but what about when you are debugging ? Say you want to test two or more pieces of code ? Yes, you can do it with the preprocessor, but smeezekitty said he hates it. So, if he hates the preprocessor but doesn't mind /**/ comments everywhere, then what is his solution ? I thought maybe he had one. Of course, it could be just a rant.

smeezekitty 06-29-2014 12:01 PM

The problem with abusing the preprocessor like that is the code path become unclear.
Usually I just use /* */ to comment it out. That is one disadvantage of using those
comments in code is that you lose that ability. I still don't mind them though

When I am testing sometimes I mark the code position and cut the code and paste it to another file.

mina86 06-29-2014 12:08 PM

Quote:

Originally Posted by smeezekitty (Post 5195894)
When I am testing sometimes I mark the code position and cut the code and paste it to another file.

Or one can delete the code, build debug version, and then undo changes on the file (any serious editor has that functionality). Or rename a function to something else while they test its different implementation. Or add code at its beginning and add “return” at the end of the new code so that the old one is unreachable. Or even wrap the code in “if (0) { … }”.

There are so many options that I don't recall when was the last time I used “#if 0”.

metaschima 06-29-2014 12:13 PM

Quote:

Originally Posted by mina86 (Post 5195897)
Or even wrap the code in “if (0) { … }”.

That is also a possibility.

NevemTeve 06-29-2014 12:15 PM

I don't really get what are we talking about, but I think that's what "if" is good for.
Code:

if (options.debug>=1) {
    fprintf (stderr, "Debug: x=%d, y=%d\n", x, y);
}


ntubski 06-29-2014 12:30 PM

Quote:

Originally Posted by mina86
and if you're using reasonable editor you probably can configure it to enter comments with some simple keystroke

Yeah, I don't know why anyone would write comment markers by hand. In Emacs, I can just use comment-dwim (M-;) which comments or uncomments the marked region. For example, with all the code marked, comment-dwim toggles between these 2:

Code:

#include <stdio.h>

int main() {
    printf("hello world");      /* a comment */
    return 0;
}

Code:

/* #include <stdio.h> */

/* int main() { */
/*    printf("hello world");      /\* a comment *\/ */
/*    return 0; */
/* } */

As you can see, it handles existing /* */ comments by escaping the comment markers.

metaschima 06-29-2014 01:25 PM

Certainly most IDEs can handle things properly. However, you can't just assume that everyone has a good IDE. I mean why else are Linux kernel devs so strict about style ? With a good IDE you can set your own style, but ... they're still strict about it.

mina86 06-29-2014 02:15 PM

Quote:

Originally Posted by metaschima (Post 5195931)
Certainly most IDEs can handle things properly. However, you can't just assume that everyone has a good IDE. I mean why else are Linux kernel devs so strict about style ? With a good IDE you can set your own style, but ... they're still strict about it.

For one, code is not always read in an IDE. For example, a lot of kernel code I read, I read in my mail client (or LKML archives).

If someone writing code does not have a decent editor, then their first priority is to get one, and not worry about comment style.

rtmistler 06-30-2014 06:31 AM

I end up using #if 0 #else #endif a lot because there are some prototype projects where they change the lower sensory layers a lot because they want to test which are better, but we want to keep the capability to talk to the other ones until we reach a final conclusion. As a result it's better to take the entire function and #if 0 it out until we know which direction we're going in.

The other thing there is if you try to /* */ the whole function, any random comment in the middle of that screws your attempt up.

And #if 0 is not perfect either, you have to have legal code within it. Make a file with bad syntax or just non-code within #if 0 and it won't always compile if you happen to have syntax based terms in there, for instance ";".

All a matter of preference though. I'm glad that C is C and I rarely encounter anyone's code that is unreadable; bad maybe, but not unreadable.

mina86 06-30-2014 08:54 AM

Quote:

Originally Posted by rtmistler (Post 5196196)
I end up using #if 0 #else #endif a lot because there are some prototype projects where they change the lower sensory layers a lot because they want to test which are better, but we want to keep the capability to talk to the other ones until we reach a final conclusion. As a result it's better to take the entire function and #if 0 it out until we know which direction we're going in.

In that case, define a macro and do “#if THAT_MACRO” and not “#if 0”, e.g.:

Code:

#define USE_NEW_API

/* … */

#ifdef USE_NEW_API

void do_foo(void) {
        do_something();
        do_something_else_using_new_api();
        finalise_or_something();
}

#else

void do_foo(void) {
        do_something_completely_different();
        use_the_old_api();
}

#endif

/* … */

int main(void) {
        /* … */
        do_foo()
        /* … */
}

#endif


rtmistler 06-30-2014 09:00 AM

LOL Mina86, not in the project I'm on; the hardware changes come rapidly for "experiementation" purposes and then suddenly I have to support both in one software release (therefore no preprocessor directive but instead true if-else). The coupe de grace is "can we get BOTH of those at the same time so we can parallel their data under the same test conditions?"

Nobody uses it, then everybody uses it, the suddenly an entirely new sensor is miraculously found that does it all. ... and then I'm keeping track of three sensory outputs.

mina86 06-30-2014 09:47 AM

So you're saying you can use “#if 0”, but you cannot use “#if TEST_MACRO”? I'm confused.

Also, VCS is your friend, delete what is not needed at given time, and bring it back if it later is needed again.

rtmistler 06-30-2014 10:10 AM

No I could do that if it were my choice, it isn't.

I am saying that ultimately when they ask me to support "either or" as a configuration option, that preprocessor directives are useless at that point because all code will need to be included in the final result. And then further when they ask me to have parallel processes or threads talking to both sensors; recording the data from both at the same time so they can run a field test and compare the data - once again preprocessor directives are a moot point. In fact, in those situations, the "either or" case is a minor headache, the "both at the same time" case is an architectural expansion where I have to write a lot more code; also fully knowing that eventually much of it will be going away ... until someone long in the future says "You know ... we were getting better readings way back when we were using the <blah-blah> sensor ..." So a lot of this is the "R" part of R&D.

In thinking a bit further though on #if 0 versus #ifdef _SOME_WORDS_; I actually do not prefer unclear words for the reason that I see _TEST_MACRO_ or something like that and then I have to FIGURE OUT if that is ever declared. That's inconvenient. #if 0 is 100% clear, "This code is NOT included in the compilation".

I have a colleague who uses #ifdev _NEVER_ where they have #define _NEVER_ 0 somewhere. Yeah ... cool.

I don't mind that you choose to do what you do, please don't worry about my style. But like I say, this brief discussion has made me realize that I do like #if 0 because it is clear as to its intention.

AnanthaP 07-01-2014 12:42 AM

Back to /* */ pair .v. //

/* */ pairs permit comments to spread over more than one line.
Unless that is you prefer a long, loopy winding, wrapped around line where the entire comment is only visible if you have set your editor to wrap lines).

So it is more useful to comment out entire chunks of code or text spread over many lines with one pair. (in // you would have to comment out each and every line and god help you in debugging if you forgot one line and more specially if you had to revert back after testing and forgot one line).

Also old c compilers may not permit //.

On the flip side, /* */ type comments within comments don't always work correctly. (// has eliminated the problem).

My choice : use both appropriately.

OK

AnanthaP 07-21-2014 01:28 AM

What happens if you comment #defines?

#define FIELD_VALUE CONSTANT_NUMBER // no closing semi colon
.v.
#define FIELD_VALUE CONSTANT_NUMBER /* no closing semi colon */

if( DataValue == FIELD_VALUE )

OK

mina86 07-21-2014 07:31 AM

Nothing. They behave as you'd expect.

AnanthaP 07-22-2014 01:25 AM

Quote:

Nothing. They behave as you'd expect.
I expect that using // would comment out everything including the closing right bracket and anything that follows.

What I was trying to say is that the /* .. */ pair works with #define while // does not.

OK

smeezekitty 07-22-2014 01:32 AM

The preprocessor does not treat the comment as part of the define or in other words the preprocessor removes the comment before
parsing the define and adding it to the compiler's symbol table

mina86 07-22-2014 07:18 AM

Quote:

Originally Posted by AnanthaP (Post 5207681)
I expect that using // would comment out everything including the closing right bracket and anything that follows.

Ah, then sorry, it behaves the opposite way to what you'd expected.

In other words, // and /* … */ work the very same way in the example you've shown. As smeezekitty described, comments are stripped prior to having the macro added to the table of available macros.

rtmistler 07-22-2014 07:28 AM

Quote:

Originally Posted by AnanthaP (Post 5207681)
I expect that using // would comment out everything including the closing right bracket and anything that follows.

What I was trying to say is that the /* .. */ pair works with #define while // does not.

OK

Closing right bracket where? Put your code in [code][/code] brackets, and put in actual code, because whatever you wrote in your earlier post is not "code", the .v. is meaningless and would cause a syntax error. Your IF statement is irrelevant except in the fact that the definition you employed in it was not shown to be defined. Nor did you illustrate your point clearly.

The following code compiles without errors or warnings using gcc -Wall to compile it:

Code:

#include <stdio.h>

#define ABC 23 // this is a comment
#define QRY 45 /* this is another comment */
#define GOFORIT // this is a third comment
#define DONTDOIT /* this is a fourth comment */

int main(void)
{
    int i, j;

    i = ABC;
    j = QRY;

    if( i == QRY ) {
        printf("i is %d\n", QRY);
    }

    if( j == ABC ) {
        printf("j is %d\n", ABC);
    }

#ifdef GOFORIT
    printf("GOFORIT is defined\n");
#endif

#ifdef DONTDOIT
    printf("DONTDOIT is defined\n");
#endif

    return 0;
}

Preprocessor substitutions which have been assigned values are usable within the code for assignment and tests. Substitutions which have been defined to exist are usable within the code for tests.

And I do not believe that it is valid to code the following:

Code:

#define THIS_IS_DEFINED

int main(void)
{
    if(THIS_IS_DEFINED) {
        printf("The directive is defined");
    }

    return 0;
}

THAT will cause a compilation error stating that an expression was expected before the ')' on the if test line.

AnanthaP 08-03-2014 07:30 PM

Quote:

#define THIS_IS_DEFINED

int main(void)
{
if(THIS_IS_DEFINED) {
printf("The directive is defined");
}

return 0;
}
You're right. AFAIR, #ifdef, #else and #ifndef are used for this.

And as you said earlier, the complier seems to strip all comments from #defines.

OK


All times are GMT -5. The time now is 11:28 AM.