[C] Why do people write comments using /* ... */ ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
[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?
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.
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.
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.
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).
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.