C++ Comments : Types and Uses

C++ Comments : Types and Uses

Why to use commments ?

You write a program so that it performs the given set of instruction. When you increase the code by adding additional instruction to it, it may become confusing. You can use comments to simplify it by mentioning that how or what the given instruction does. If you give your code to any other programmer, it will be easier for him to understand your code if you use comments wisely. Also It will be helpful for you too. If you use comments you will have better idea about your program even if the program becomes large later on.

Types of comments

There are two types of comments used in C++.
(1) Single line comments :
Single line comments is accomplished by double-slash (//). Everthing that is followed by double-slash till the end of line is ignored by the compiler. It is reffered as C++-style comments as it is originally part of C++ programming.

(2) Multi-line comments :
Multi-line comments starts by using forward slash followed by asterisk (/*) and ends by using asterisk followed by forward slash (*/). Everthing between (/*) and (*/) are ignored by compiler whether it is one or more than one line. It is reffered as C-Style comment as it was introduced in C programming.

A C++ Program example that demonstrates the syntax and use of C++ Comments.


/*
Program : C++ Comments
Source code written by : Mohammed Homam
Last modified date : 8:09 PM 10/21/2009
*/

#include <iostream>

int main ()
{

    using std::cout;
    using std::endl;

    cout << "C++ Programming" << std::endl;    // It will display
                                                                         // C++ Programming
    return 0;
}

Check out this stream