C Comments
C Comments
In this tutorial, we will learn about C Comments. Adding comments to code is good programming practice so that others can understand the code and logic. However, the C compiler ignores the comments when it translates the C program into executable machine code.
C language uses three types of comments. They are as follows:
- Line comment( // )
- Block comment( /* …. */ )
- Document Comment ( /** … */ )
Line comment
The line comment is also known as a single-line comment. The single-line comment uses two slashes to identify it as a line comment. The end of the line automatically ends the line comment.
The programmer usually uses single-line comments to write short descriptive text in the program code.
Single line comment example:
Block comment
The block comment is also known as a multi-line comment. The multi-line comment uses opening and closing comment tokens. A token is a combination of one or more symbols with a special meaning understood by the C compiler.
The multi-line comment starts with a /* and ends with a */ token. The compiler ignores all the comment text between the /* and */ comment symbols.
A block comment can span several lines. The programmer usually uses multi-line comments when the comment text contains several lines.
Multi-line comment example:
Document Comment
This comment is mostly used at the top of the C program. The document comment is used to add program attributes, program documentation, license information, etc.
The document comment starts with a /* * and ends with a */ token. The compiler ignores all the comment text between the /** and */ comment symbols.
Document comment example:
Notice that the IDE highlights the comment text with a different color.
C Program
Sample C demo program with the comments.
/** ********************************** * Program Description: * C Comments Demo * Filename : commentsDemo.c * Author : TestingDocs * Date : 07/08/2017 * C Tutorials - www.TestingDocs.com ************************************* */ #include<stdio.h> int main() { /* C Variable Declaration Initialization Demo Program C Tutorials - www.TestingDocs.com */ int number = 20; // declare number double interestRate = 0.15;// declare interest rate printf("Number = %s \n",number); // print number printf("Interest Rate = %lf \n",interestRate); return 0; } // end main
That’s it.
—
C Tutorials
C Tutorials on this website can be found at: