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


All times are GMT -5. The time now is 05:02 PM.