C programming language comments; Through this tutorial, you will learn how to use comments in the C language with syntax and examples.
In every programming language; comments provide clarity to the C source code to assist others to know what the code was designed to accomplish and help a lot in debugging the code. Comments are especially important in large projects that contain hundreds or thousands of lines of source code, or in projects that have multiple developers or programmers working on the source code.
C Programming Language Comments
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 For Comments in C Programming
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.