Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Concept of Comments in Computer Programming

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Comments are text notes added to the program to provide explanatory information about the source code. They are used in a programming language to document the program and remind programmers of what tricky things they just did with the code and also helps the later generation for understanding and maintenance of code. The compiler considers these as non-executable statements. Different programming language uses a different way of including the comments in the source code.

/* this is a comment in C. 
This comment syntax is guaranteed to
work on every compiler */
// This is also a comment in C
// but it might present portability challenges

<!-- This is a comment in HTML-->

Comments can be used for the various purpose like :

  1. Planning and reviewing : In comments, we can write the pseudocode which we planned before writing the source code. Pseudocode is a mixture of natural language and high-level programming language. This helps in reviewing the source code more easily because pseudocode is more understandable than the program.
    Example –

    /* Euclid(m, n)
          while m does not divide n
          r = n mod m
          n = m
          m = r
          return m

    In the above example, I have written the pseudo code of gcd in comments. This will help during the implementation of the code.

  2. Code description : Code description is used by the programmer to make others understand his/her intent. It contains the summary of the code.

        /* A Multiline Comment --
               Define a variable of
               string type and assign
               value to it*/
        string msg = "GeeksforGeeks";

  3. Algorithmic description : Comments are used for explanation of the methodology. Such explanations may include diagrams and formal mathematical proofs. This may constitute the explanation of the code, rather than a clarification of its intent. For example, a programmer may add a comment to explain why an insertion sort was chosen instead of a quicksort, as the former is, in theory, slower than the latter.

    list = [ f(b), f(b), f(c), f(d), f(a), ... ];
    // Need a stable sort. Besides,
    // the performance really does not matter.

  4. Resource inclusion : Logos, diagrams, and flowcharts consisting of ASCII art constructions can be inserted into source code formatted as a comment. Further, copyright notices can be embedded within source code as comments.
  5. Metadata : Comments also contain metadata of the program. This metadata helps in software maintenance. The metadata includes the name of the creator of the original version, current maintainer of the program, data when the first version was created, the name of the people who have edited the program files so far etc.
  6. Debugging : Brute force method is a common method of debugging. In this approach, print statements are inserted throughout the program to print the intermediate values with the hope that some of the printed values will help to identify the errors. After doing debugging we comment those print statements. Hence comment is also used for debugging.

    int fun(int m)
        int count = 0;
        while (m > 10) {
            // printf("m is less than 10, m=%d", m);
        return m;

  7. Automatic documentation generation : Programming tools sometimes store documentation and metadata in comments. These may include insert positions for automatic header file inclusion, commands to set the file’s syntax highlighting mode, or the file’s revision number. These functional control comments are also commonly referred to as annotations. Keeping documentation within source code comments is considered as one way to simplify the documentation process, as well as increase the chances that the documentation will be kept up to date with changes in the code.
  8. Stress relief : Commenting on development tools, competitors, employers, working conditions, or the quality of the code itself are the ways to relieve stress. The occurrence of this phenomenon can be easily seen from online resources that track profanity in source code.

Learn more about Comments in C/C++, Java, HTML, C#

My Personal Notes arrow_drop_up
Last Updated : 29 Aug, 2018
Like Article
Save Article
Similar Reads
Related Tutorials