Comments

Every programming language I’ve worked with allowed the programmer to add comments to the program. Comments are essentially programmer notes which the compiler ignores. In the small programs I’ve had here so far comments haven’t really been necessary. However, when you have a program that has 10,000 lines of code and you haven’t looked at it in a couple years, comments come in handy. A second instance where comments are handy is when you are trying to make changes to programs someone else wrote. I’ve modified programs written by other people. Some were well commented, others not. If I had not already been convinced of the value of comments, working with those poorly commented programs would have changed my mind.

In C, comments can be added to a program in 2 ways. The first way is with a //. When the compiler sees // it ignores the rest of the line. So, you can add a comment that is a full line in the program by starting the line with //. But you can also use the // after the line of code like this:

printf(“Hello World!\n”); // Say Hello

The compiler ignores the Say Hello. The // does not need to have a space before or after it.

The second type of comment is a multiline comment. Multiline comments begin with /* and end with */. The compiler ignores everything between the comments. The /* and the */ do not need to be on separate lines, but can be any number of lines apart. There are sensible restrictions to the use of /* and */. For example

printf(“Hello /* Say Hello */World\n”);
will print
Hello /* Say Hello */World

printf/* Say Hello */(“Hello World\n”);
will give an error

printf(“Hello World\n”)/* Say Hello */;
will print
Hello World

printf(“Hello World\n”)
/*
Say Hello
*/;
will print
Hello World

Author: Ron

Homeschooling dad of 4 (ages 27 - 14), grampy to 3, WordPress core contributor, former farmboy & software developer by profession.