Using Comments in a C++ Program

By: Priya Emailed: 1646 times Printed: 2114 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

When you are writing a program, it is always clear and self-evident what you are trying to do. Funny thing, though--a month later, when you return to the program, it can be quite confusing and unclear. I'm not sure how that confusion creeps into your program, but it always does.

To fight the onset of confusion, and to help others understand your code, you'll want to use comments. Comments are simply text that is ignored by the compiler, but that may inform the reader of what you are doing at any particular point in your program.

Types of Comments

C++ comments come in two flavors: the double-slash (//) comment, and the slash-star (/*) comment. The double-slash comment, which will be referred to as a C++-style comment, tells the compiler to ignore everything that follows this comment, until the end of the line.

The slash-star comment mark tells the compiler to ignore everything that follows until it finds a star-slash (*/) comment mark. These marks will be referred to as C-style comments. Every /* must be matched with a closing */.

As you might guess, C-style comments are used in the C language as well, but C++-style comments are not part of the official definition of C.

Many C++ programmers use the C++-style comment most of the time, and reserve C-style comments for blocking out large blocks of a program. You can include C++-style comments within a block "commented out" by C-style comments; everything, including the C++-style comments, is ignored between the C-style comment marks.

Using Comments

As a general rule, the overall program should have comments at the beginning, telling you what the program does. Each function should also have comments explaining what the function does and what values it returns. Finally, any statement in your program that is obscure or less than obvious should be commented as well.

Listing below demonstrates the use of comments, showing that they do not affect the processing of the program or its output.

HELP.CPP demonstrates comments.

1: #include <iostream.h>
2:
3: int main()
4: {
5:  /* this is a comment
6:  and it extends until the closing
7:  star-slash comment mark */
8:    cout << "Hello World!\n";
9:    // this comment ends at the end of the line
10:   cout << "That comment ended!\n";
11:
12:  // double slash comments can be alone on a line
13: /* as can slash-star comments */
14:     return 0;
15: }
Hello World!
That comment ended!

The comments on lines 5 through 7 are completely ignored by the compiler, as
are the comments on lines 9, 12, and 13. The comment on line 9 ended with the
end of the line, however, while the comments on lines 5 and 13 required a closing comment mark.

Comments at the Top of Each File

It is a good idea to put a comment block at the top of every file you write. The exact style of this block of comments is a matter of individual taste, but every such header should include at least the following information:

  • The name of the function or program.
  • The name of the file.
  • What the function or program does.
  • A description of how the program works.
  • The author's name.
  • A revision history (notes on each change made).
  • What compilers, linkers, and other tools were used to make the program.
  • Additional notes as needed.

For example, the following block of comments might appear at the top of the Hello World program.

/************************************************************

Program:      Hello World

File:         Hello.cpp

Function:     Main (complete program listing in this file)

Description:  Prints the words "Hello world" to the screen

Author:       Jesse Liberty (jl)

Environment:  Turbo C++ version 4, 486/66 32mb RAM, Windows 3.1
              DOS 6.0.  EasyWin module.

Notes:        This is an introductory, sample program.

Revisions:    1.00  10/1/94 (jl) First release
              1.01  10/2/94 (jl) Capitalized "World"

************************************************************/

It is very important that you keep the notes and descriptions up-to-date. A common problem with headers like this is that they are neglected after their initial creation, and over time they become increasingly misleading. When properly maintained, however, they can be an invaluable guide to the overall program.


C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(4)


1. View Comment

pLZ LET US FIND SOME PROGRAMS INVOLVING CONVERSION

View Tutorial          By: Ishaq at 2009-02-12 00:27:59
2. View Comment

If you use comments you will have better idea about your program even if the program becomes large later on.

.


View Tutorial          By: Mohammed Homam at 2009-12-06 04:43:51
3. View Comment

At a writing of the big program in a team comments are important necessity as some one can be finish your code

View Tutorial          By: vlad at 2011-05-13 12:29:01
4. View Comment

Please provide some more examples ...It will be very useful..

View Tutorial          By: it interview questions at 2012-09-29 05:07:55

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Priya
Getting started with ASP
EJB Entity Beans
Error Handling - Stderr and Exit - sample program in C
Pointers and Arrays in C
Data Types and Sizes in C
virtual inheritance example in C++
ctime() sample program in C++
TextBox sample program in J2ME
Advantages of Java Beans
list() contents of a Directory - sample program in Java
File example program in Java
continue and break statements in C++
Use of 'throw' in Java
Passing parameters to a function by value in C++
Demonstrating global and local variables in C++

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Stray or Dangling Pointers in C++
Sorting an array of Strings in C++
A Brief History of C++
C++ and Object-Oriented Programming
Getting Started with C++
Using Comments in a C++ Program
Advanced if Statements in C++
C++ Recursion function explained using Fibonacci series
Classes with Other Classes as Member Data in C++
The indirection operator (*) - dereference operator.
Dot (.) vs Arrow (->) to access data members in C++
What Is a Reference in C++?
Converting Default Function Arguments in C++ to Java
How to handle Exceptions in C++
atoi(), itoa() in C++
Most Emailed Articles (in C++)
Returning values from a function in C++
while (1) Loops in C++
Using Comments in a C++ Program
Specifying default values to function parameters in C++
continue and break statements in C++
The indirection operator (*) - dereference operator.
Difference between Procedural, Structured, and Object-Oriented Programming
C++ and Object-Oriented Programming
How to use Enumerated constants in C++
Advanced if Statements in C++
while Loops in C++
Converting Pointer Parameters in C++ to Java
assert() Versus Exceptions in C++
Compute the square root of the sum of the squares of an array in C++
A Brief History of C++