LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-13-2005, 08:10 AM   #1
joesbox
Member
 
Registered: Feb 2003
Location: hampton va
Distribution: ubuntu
Posts: 502

Rep: Reputation: 30
Question proper commenting in C


i am just starting to learn C and had a simple question for anyone that would like to answer. i picked up a book called Visual Quickstart Guide: C Programming by Larry Ullman and Marc Liyanage. in the book it makes reference to commenting using the /* comment */ style but i have seen one comment using // with no end comment or anything like */. example from the book
Code:
/* hello3.c - Script 1.4
 * Created by Larry Ullman 5/6/2004
 * This script says "Hello, world!"
*/

#include <stdio.h>

int main (void) {

printf("Hello, world!");
getchar(); // wait for the user before continuing. 

return 0;

}
now the commenting is understandable but i was wondering which is best to use? i have seen all types of commenting styles from # to ! to /* to //.
i hope that this isn't a stupid question and that it really doesn't relate to actual coding but i would like to remain standard so that my comments are readable to the rest of the world since i plan on making all my future code open source.
thanks in advance
Joe
*any mistakes in the code are due to typo of me.
 
Old 02-13-2005, 08:21 AM   #2
Starch
Member
 
Registered: Jan 2005
Posts: 77

Rep: Reputation: 15
The standart for C is using /* */, but compilers now accept "//" for C code. Use which ever is more convenient For Example if you will explain about a function with about 3 lines of explanation than /* */ is better and all C/C++ compilers support it.

If you are explaining code line by line than using "/* */" would look more messy than "//"

If you plan on releasing your code make sure you comment what each relevant function does. The more you will comment the easier it will make it for others to upgrade or add add-ons to your project. But remember don't comment too much or too little.

Have fun!
 
Old 02-13-2005, 08:22 AM   #3
Miaire
LQ Newbie
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Fedora Core 3
Posts: 11

Rep: Reputation: 0
Most I have seen are /* */ and personally, I find that they look neater even if you're just commenting in/on one line.
 
Old 02-13-2005, 09:09 AM   #4
joesbox
Member
 
Registered: Feb 2003
Location: hampton va
Distribution: ubuntu
Posts: 502

Original Poster
Rep: Reputation: 30
thanks guys. i thought that either worked but i just wanted to make sure if there was a standard to use or not. for simplicity sake i will probably just use the /* */ even if it is a little messy.
 
Old 02-13-2005, 10:11 AM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Using // is C++ style commenting, but as stated, most C compilers also support it. Using // is nice for simple comments for individual lines not only for convenience, but because it is then easier to comment out whole blocks of code. For instance, consider something like the following:

Code:
/* Comment out this whole function for whatever reason
int someFunction()
{
    blah blah; /* something something */   
}
*/
The above won't work to comment the entire function because the commenting inside the function will close out the comment early. If you commented it inside the function with // instead, you wouldn't have that problem.

Last edited by deiussum; 02-13-2005 at 10:12 AM.
 
Old 02-13-2005, 11:53 AM   #6
joesbox
Member
 
Registered: Feb 2003
Location: hampton va
Distribution: ubuntu
Posts: 502

Original Poster
Rep: Reputation: 30
that is a very important point that will help in debugging later. thanks a lot deiussum. i probably would have done this later and not knows the reason for the error. i will definitely keep this in mind.
 
Old 02-13-2005, 11:11 PM   #7
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 392

Rep: Reputation: 55
Quote:
Originally posted by Starch
The standard for C is using /* */, but compilers now accept "//"
The newest C language standard, C99, includes "//" style comments.

Lyle
 
  


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
Specify proper x server banner Linux - Newbie 1 04-03-2005 04:38 PM
proper use of wine UGT001 Linux - Software 4 11-28-2004 02:25 PM
Finding a proper OS devit Linux - Distributions 2 06-22-2004 10:18 PM
Proper use of permissions ? wmcase Linux - General 4 02-04-2004 03:41 PM
Proper way to shutdown? snocked Linux - Newbie 12 02-11-2003 08:26 AM

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

All times are GMT -5. The time now is 03:22 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