2.3. Comments

Comments are a way to add explanatory text to your program, they are ignored by the compiler so they don't affect your program in any way. As the programs you write get larger you will find it helpful to have comments in your code to remind you what you are doing. In the examples in this book we will use comments to explain what is going on. There are two ways to insert a comment into your program, the most common way is to start and end your comments with /* and */ respectively. Comments of this sort can span multiple lines. The second way is by placing // at the start of your comment. Comments of this sort are terminated at the end of a line. Here's our "hello, world" program with comments.

Example 2-2. hello2.c

/* The purpose of this program is to
 * display some text to the screen
 * and then exit.
 */

#include <stdio.h>

int
main()
{
  /* printf() displays a text string */
  printf("hello, world\n");

  return 0;  //zero indicates there were no errors
}
    

When compiled, this code will produce exactly the same executable. Lines 2 and 3 of the comment at the top start with an asterisk, this is not necessary but it makes it clear that the comment extends for four lines.