C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
/* This is a single-line comment */
for (i = 0; i < 25; i++) /* Counts from 0 to 25 */

Note

Notice that comments can go on lines by themselves or before or after programming
statements. The choice of placement depends on the length of the comment and the
amount of code the comment describes.

The Draw Poker program in Appendix B, “The Draw Poker Program,” contains all kinds of
comments. By reading through the comments in that program, you can get an idea of what the program
does without ever looking at the C code itself.


Don’t comment every line. Usually only every few lines need comments. Many programmers like to
place a multiline comment before a section of code and then insert a few smaller comments on lines
that need them. Here is a complete program with different kinds of comments:


Click here to view code image


/* The first code listing from Chapter 3 of The Absolute Beginner's
Guide to C
Teaching new programmer to create kick-butt code since 1994! */
/* A Dean Miller joint */
/* Filename Chapter3ex1.c */
/* Totals how much money will be spent on holiday gifts. */
#include <stdio.h>
main()
{
float gift1, gift2, gift3, gift4, gift5; /* Variables to hold
costs. */
float total; /* Variable to hold total amount */
/*Asks for each gift amount */
printf("How much do you want to spend on your mom? ");
scanf(" %f", &gift1);
printf("How much do you want to spend on your dad? ");
scanf(" %f", &gift2);
printf("How much do you want to spend on your sister? ");
scanf(" %f", &gift3);
printf("How much do you want to spend on your brother? ");
scanf(" %f", &gift4);
printf("How much do you want to spend on your favorite ");
printf("C Programming author? ");
scanf(" %f", &gift5);
total = gift1+gift2+gift3+gift4+gift5; /* Computes total amount
spent on gifts */
printf("\nThe total you will be spending on gifts is $%.2f", total);
return 0; /*Ends the program */
}

Many companies require that their programmers embed their own names in comments at the top of

Free download pdf