C: Program Structure

A programmer uses comments to document the code. Text that has been “commented” is ignored by the compiler. The largest contributor of the total cost of software is code maintenance, not program development. Adequately commented code can drastically reduce the lifecycle cost of a program. Often, even the programmer who wrote the code forgets details when editing code several months later.

C supports two different formats of comments. The first type starts with a slash asterisk and ends with an asterisk slash; everything in between is ignored by the compiler. There are many implementation of this format used by programmers. A typical program will have a header comment to provide details regarding the entire program as well as comments inserted throughout the program to clarify the programmer’s intent. This is the hello program with comments added.

/*******************************************
* Filename:   hello.c
* Programmer: Bob Adams
* Date:  June 8, 1998
*
* Description: 
* Displays a hello message on screen to user
********************************************/

/* preprocessor directive */
#include <stdio.c>
int main(void) 
{
   printf("Hello"); /* display  hello on screen */
   return 0;   /* indicates success to OS */
}

OUTPUT

Hello

The newer C99 standard implemented the C++ syntax for comments using the double slash //. Everything after the double slash until the end of the line is considered a comment and ignored by the compiler. Below is the program using the newer style of comments. Both styles can be used, sometimes one style is more clear than the other within the code.

//************************************************
// Filename:   hello.c
// Programmer: Bob Adams
// Date:  June 8, 1998
//
// Description: 
// Displays a hello message on screen to user
//************************************************

// preprocessor directive
#include <stdio.c>
int main(void) 
{
   printf("Hello"); // display  hello on screen
   return 0;   // indicates success to OS
}

OUTPUT

Hello


Source: C Primer Plus, Berkeley C programming class notes