GCC C++: Comment Text Introduction Comment text is text included in source code that is ignored by the compiler and does not cause any machine-language object code to be generated. It is written into the source code to add clarity for the reader. This article demonstrates inclusion of comment text in source code. The comment program expands on the hello program and adds comments. Concepts construct Programs are made up of (or constructed from) programming language constructs. A programming language construct is a portion of the program that defines an entity within it. A programming language construct can be defined as any building block of the program. comment A comment is a programming language construct used to embed information into the source code of a computer program. When the source code is compiled, comments are ignored. Comment text does not affect the running of the compiled program or increase the size of the executable file. It is written into the source code to add clarity for the reader. Comments are normally used to document code for future reference and to clarify it. The comment program adds comments to source code from the hello program.
Source Code The source code listing is as follows: /* comment.cpp Program containing comment text. environment: language C++ platform Windows console */ #include <stdio.h> // Entry point. int main() { // The following line is not compiled: \ printf( "Hello World" ) ; } printf( "Hello World" ) ; // Display "Hello World". return 0 ;
Compiling and Running 1. Save the source code listing into a file named comment.cpp. Make sure that the code ends with a carriage return. 2. Launch a Windows command prompt. 3. Navigate to the directory comment.cpp was saved in. 4. To compile the program, type: > g++ comment.cpp -o comment.exe 5. To run the program, type: > comment
Code Explanation Comment text is inserted into the code in one of the following ways: // The two sequential characters // followed by any sequence of characters on the same line of source code are ignored by the compiler. The ignored text begins with // and ends with a carriage return (new line) not immediately preceded by a backslash \. This enables a single line of comments to be inserted into the code either following an instruction or on a line by itself. This kind of comment is therefore commonly called a single-line comment. /* */ The two sequential characters /* followed by any sequence of characters and then followed by the two sequential characters */ are ignored by the compiler. The ignored text begins with /* and ends with */. This enables multiple lines of comments to be inserted. This kind of comment is therefore commonly called a multi-line comment. /* comment.cpp Program containing comment text. environment: language C++ platform Windows console */ Text beginning with characters /* and ending with */ is ignored by the compiler. This enables inclusion of comment text which extends to more than one line. This multi-line option is used to provide introductory text for the program. // Entry point. Text that starts with characters // and continues to the end of the line of code is ignored by the compiler. This single-line comment is used to describe the main function. In this example all text from // to point. is ignored by the compiler. // The following line is not compiled: \ printf( "Hello World" ) ; Comment text beginning with // ends with a carriage return not immediately preceded by a backslash \. In this example the first carriage return is preceded by a backslash, so the comment text ends with the next carriage return. All text from // to printf( "Hello World" ) ; is ignored by the compiler. If the backslash at the end of the first line was omitted, the string literal Hello World would be displayed twice.
printf( "Hello World" ) ; // Display "Hello World". Comment text can be inserted into the code following an instruction. This comment explains the purpose of statement printf( "Hello World" ) ;.