C Programming Comments

Last modified by Microchip on 2023/11/09 09:06

Comments
Comments are lines or blocks of text used to document a program's functionality and explain how a program works for the benefit of a programmer. Comments are ignored by the compiler, so you can type anything you want into them.

C supports two different styles of comments: Block Comments and Single Line Comments.

Block Comments

/* This is a comment */

Block Comments begin with a /* and end with a */ and may span multiple lines. For example:

1
2
3
4
5
6
7
8
9
10
11
/************************************************************************
* Program:    Hello.c
* Author:     R. Ostapiuk
************************************************************************/

#include <stdio.h>

/* Application begins here */
int main(void)
{
    printf("Hello, world!\n");    /* Display "Hello, world!" in terminal */
}

Lines 1-4 are comprised of one long comment that spans four lines. The comment begins with the /* at the beginning of line 1 and ends with the */ at the end of line 4. Every other character between those two delimiters is ignored by the compiler.

Line 7 shows another example where a block comment is used for just a single line. While line 10 shows a similar construct to add a comment at the end of a line of code.

Single Line Comments

// This is also a comment

Single line comments begin with a // and run to the end of the line. These are sometimes called "C++ style comments" because they were developed for C++ and were not originally part of the ANSI C standard. They may not span multiple lines. For example:

1
2
3
4
5
6
7
8
9
10
11
//====================================================
// Program:    Hello.c
// Author:     R. Ostapiuk
//====================================================
#include <stdio.h>

// Application begins here
int main(void)
{
    printf("Hello, world!\n");    // Display "Hello, world!" in terminal
}

Here we have replaced each block comment from the first example with an equivalent form using a line comment.

Lines 1-4 are now four separate comments with each line starting with //.

Lines 7 and 10 were also converted into single-line comments that are more appropriate for their situations.

Nesting Comments

Block comments may not be nested within other block comments. However, single-line comments may be nested within a block comment.

1
2
3
4
5
6
7
8
/*
    code_here;        // This is a valid way to nest comments
*/


/*
    code_here;        /* This is not a valid way to nest comments */

    code_here;        /* What happened here? */
*/

Lines 1-3 show that it is perfectly valid to nest a single-line comment within a block comment. In fact, this is a widespread practice when testing programs. From time to time, you may want to temporarily remove a block of code from your program. Using block comment delimiters as shown here will have that effect as long as the only other comments between the delimiters are single-line comments.

Lines 5-8 show what can go wrong when block comments are nested within one another. A comment begins with the /* at the beginning of line 5. Because of line 5, the opening delimiter /* on line 6 is completely ignored and the closing delimiter */ at the end of line 6 is matched with the opening delimiter on line 5. This leaves the code on line 7 uncommented, followed by a complete comment at the end of line 7.

Therefore, at the beginning of line 8, we have a closing delimiter */ that is unmatched by an opening delimiter which will cause a syntax error.

Best Practices

Ideally, you should use single-line comments throughout most of your code, reserving block comments for times when you want to temporarily remove a block of code or for when you really need multi-line comments. Below is an example of how many programmers use the two comment styles.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/************************************************************************
* Program:    Hello.c
* Author:     R. Ostapiuk
************************************************************************/

#include <stdio.h>

/************************************************************************
* Function: main()
* (some details about what main does here...)
************************************************************************/

int main(void)
{
   int i;                    // Loop Count Variable
   char *p;                  // Pointer to output string
/*
    printf("Hello, world!\n");    // Display "Hello, world!" in terminal
*/

    ...
}