LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2014, 03:45 PM   #1
displace
Member
 
Registered: Jan 2013
Location: EU
Distribution: Debian
Posts: 268

Rep: Reputation: 25
[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?

Last edited by displace; 06-20-2014 at 03:47 PM.
 
Old 06-20-2014, 03:49 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Use #ifdef 0 to comment out blocks of code.
 
Old 06-20-2014, 04:10 PM   #3
turtleli
Member
 
Registered: Aug 2012
Location: UK
Posts: 206

Rep: Reputation: Disabled
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.
 
Old 06-20-2014, 04:24 PM   #4
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
I also find it annoying. I always use // comments, except when excluding code.
 
Old 06-20-2014, 04:41 PM   #5
displace
Member
 
Registered: Jan 2013
Location: EU
Distribution: Debian
Posts: 268

Original Poster
Rep: Reputation: 25
Ah I see. The "/* ... */" notation predates the "//". Now it makes sense.
 
Old 06-21-2014, 05:52 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
 
Old 06-21-2014, 06:41 PM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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.
 
Old 06-23-2014, 07:15 AM   #8
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
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.
 
Old 06-23-2014, 09:02 AM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
^ line-comments are easier to type.
 
Old 06-23-2014, 10:40 AM   #10
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
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).
 
Old 06-23-2014, 11:12 AM   #11
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 06-23-2014, 12:46 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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
 
Old 06-28-2014, 12:43 PM   #13
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
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
 
Old 06-28-2014, 01:43 PM   #14
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by smeezekitty View Post
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 ?
 
Old 06-28-2014, 05:29 PM   #15
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I get people to use Linux? I'm bad at converting people over. Mr. Hill Linux - Newbie 50 07-11-2020 10:41 AM
How do people learn to modify-patch Windows 9X and write programs? Holering Programming 5 05-10-2014 07:30 AM
read/write,write/write lock with smbclient fails swatidas11 Linux - Networking 1 03-10-2010 12:27 PM
LXer: People Who’ve Never Run Linux Shouldn’t Write About Linux LXer Syndicated Linux News 0 12-28-2007 09:50 AM
How do people write 4.3GB data DVDs? zoubidoo Linux - General 4 02-01-2007 01:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration