Comments in C Programming

C programming language comments; In this tutorial, i am going to show you how to use comments in the C language with syntax and examples.

How to write Comments in C Programming

A comment starts with a slash asterisk /* and ends with a asterisk slash */ and can be anywhere in your program. Comments can span several lines within your C program. Comments are typically added directly above the related C source code.

Attaching comments to your C source code is an extremely recommended practice. In common, it is forever better to over comment in C source code than to not add enough.

Syntax of C Programming Comments

The syntax for comment is:

/* comment goes here */

OR

/*
 * comment goes here
 */

Note that:- It is important that you prefer the way of commenting and use it consistently during your source code. Doing so creates the code more understandable.

Example 1 – Single Line Comment in C

You can create a comment on a single line. See the following example for that:

/* Author: Tutsmake.com */

C++ introduced a double slash comment prefix // as a way to comment single lines. The following is an example of this:

// Author: Tutsmake.com

This form of commenting may be used with most modern C compilers if they also understand the C++ language.

Example 2 – Multiple Lines Comment Spans in C

You can create a comment that spans multiple lines. See the following example for that:

/*
 * Author: tutsmake.com
 * Purpose: To show a comment that spans multiple lines.
 * Language:  C
 */

The compiler will assume that everything after the /* symbol is a comment until it reaches the */ symbol, even if it spans multiple lines within the C program.

Example 3 – End of Code Line Comment in C

You can create a comment that displays at the end of a line of code.

For example:

#define Amount 500    /* This constant is called Amount */

OR

#define Amount 500    // This constant is called Amount

In these examples, the compiler will define a constant called Amount that contains the value of 500. The comment is found at the end of the line of code after the constant has been defined.

Be First to Comment

Leave a Reply

Your email address will not be published.