Comments in C/C++ - GeeksforGeeks

23 Mar, 2021
A well-documented program is a good practice as a programmer. It makes a program more readable and error finding become easier. One important part of good documentation is Comments.  
In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program
Comments are statements that are not executed by the compiler and interpreter.
In C/C++ there are two types of comments :  
Single line commentMulti-line comment
Single line comment
Multi-line comment
 
 
Single line Comment
Represented as // double forward slash It is used to denote a single line comment. It applies comment to a single line only. It is referred to as C++-style comments as it is originally part of C++ programming. for example:  
// single line comment
Example: This example goes same for C and C++ as the style of commenting remains same for both the language.  
C
// C program to illustrate// use of multi-line comment#include <stdio.h>int main(void){     // Single line Welcome user comment    printf("Welcome to GeeksforGeeks");    return 0;}
Welcome to GeeksforGeeks
 
Multi-line comment
Represented as /* any_text */ start with forward slash and asterisk (/*) and end with asterisk and forward slash (*/). It is used to denote multi-line comment. It can apply comment to more than a single line. It is referred to as C-Style comment as it was introduced in C programming. 
/*Comment starts
continues
continues
.
.
.
Comment ends*/
Example: This example goes same for C and C++ as the style of commenting remains same for both the language.  
C
/* C program to illustrateuse ofmulti-line comment */#include <stdio.h>int main(void){     /* Multi-line Welcome user comment    written to demonstrate comments    in C/C++ */    printf("Welcome to GeeksforGeeks");    return 0;}
Welcome to GeeksforGeeks
 
Comment at End of Code Line
You can also create a comment that displays at the end of a line of code. But generally its a better practice to put the comment before the line of code. Example: This example goes same for C and C++ as the style of commenting remains same for both the language.  
 int age; // age of the person
 
When and Why to use Comments in programming?
 
A person reading a large code will be bemused if comments are not provided about details of the program.Comments are a way to make a code more readable by providing more description.Comments can include a description of an algorithm to make code understandable.Comments can be helpful for one’s own self too if code is to be reused after a long gap.
A person reading a large code will be bemused if comments are not provided about details of the program.
Comments are a way to make a code more readable by providing more description.
Comments can include a description of an algorithm to make code understandable.
Comments can be helpful for one’s own self too if code is to be reused after a long gap.
 
sangai260
CBSE - Class 11
Picked
school-programming
C Language
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
rand() and srand() in C/C++
fork() in C
Command line arguments in C/C++
Substring in C++
Function Pointer in C
Python Dictionary
Reverse a string in Java
Inheritance in C++
C++ Classes and Objects
Constructors in C++