Comments
Now let's take a close look at the C program in Listing 2.1.
The first line contains a comment:
/* cprog01.C: This is my first C program */
You notice that this line starts with a combination of a slash and an asterisk, /*, and ends with */. In C, /* is called the opening comment mark, and */ is the closing comment mark. The C compiler ignores everything between the opening comment mark and closing comment mark. That means the comment in the first line of Listing 2.1, cprog01.C: This is my first C program, is ignored by the compiler.
The only purpose of including comments in your C program is to help you document what the program or some specific sections in the programs do. Remember, comments are written for programmers like you. For example, when you read someone's code, the comments in the code help you to understand what the code does, or at least what the code intends to do.
You don't need to worry about the size or performance speed of your C program if you add many comments into it. Adding comments into a C program does not increase the size of the binary code of the program (that is, the executable file), although the size of the program itself (that is, the source code) may become larger. Also, the performance speed of the executable file made from your C program is not affected by the comments inside your C program.
Most C compilers allow you to write a comment that crosses more than one line. For instance, you can write a comment in C like this:
/*
This comment does not increase the size of
the executable file (binary code), nor does
it affect the performance speed.
*/
which is equivalent to this:
/* This comment does not increase the size of */
/* the executable file (binary code), nor does */
/* it affect the performance speed. */
NOTE
These days, there is another way to put comments into a C program. C++ started using two slashes (//) to mark the beginning of a comment line; many C compilers now use this convention as well. The comment ends at the end of the line. For instance, if I write a C program in Borland C++ or Visual C++, the following two comments are identical:
/*
This comment does not increase the size of
the executable file (binary code), nor does
it affect the performance speed.
*/
// This comment does not increase the size of
// the executable file (binary code), nor does
// it affect the performance speed.
Note that this new style of using // as the beginning mark of a comment has not been approved by ANSI. Make sure your C compiler supports // before you use it.
One thing that needs to be pointed out is that the ANSI standard does not support nested comments, that is, comments within comments. For instance, the following is not allowed by the ANSI standard:
/* This is the first part of the first comment
/* This is the second comment */
This is the second part of the first comment */
TIP
You can use the opening comment mark, /*, and closing comment mark, */, to help you test and fix any errors found in your C program. You can comment out one or more C statements in your C program with /* and */ when you need to focus on other statements and watch their behaviors closely. The C compiler will ignore the statements you comment out.
Later, you can always restore the previously commented-out statements simply by removing the opening comment and closing comment marks. In this way, you don't need to erase or rewrite any statements during the testing and debugging.
- 58 views