Concept of Comments in Computer Programming

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.

filter_none

edit
close

play_arrow

link
brightness_4
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

chevron_right


filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 –

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // GCD BY EUCLID'S ALGORITHM
    /* Euclid(m, n)
    {
          while m does not divide n
          r = n mod m
          n = m
          m = r
          end
          return m
    }
    */

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.