Comments in C

In this article, we will be delving into the realm of Comments in C, an essential aspect of any programming language. Understanding the purpose of adding comments in C programming is crucial in maintaining clean and efficient code. We'll discuss the benefits of using comments in your C code, as well as the best practices for writing them. Furthermore, we will explore the different types of comments in C, including single line comments and block comments, while comparing their syntax and usage. We will then examine the various applications of comments in C programming, such as documenting functions and algorithms, commenting out sections of code during debugging, and adding credits and licensing information within your code. By the end of this article, you'll be familiar with the significance of comments in C and how to effectively utilise them in your own programming projects.

Comments in C Comments in C

Create learning materials about Comments in C with our free learning app!

  • Instand access to millions of learning materials
  • Flashcards, notes, mock-exams and more
  • Everything you need to ace your exams
Create a free account
Table of contents

    Introduction to Comments in C

    While learning about Computer Science and programming, it is important to understand the concept of Comments in C. Comments are used in programming languages as a means of providing explanations and descriptions within the code. They improve the readability and maintainability of the code for the developers working on it.

    Purpose of adding comments in C programming

    Comments play a vital role in making the code understandable for fellow programmers. They are also helpful for documentation purposes. In C programming, comments are ignored by the compiler and have no impact on the execution of the code. There are two types of comments in C:

    • Single-line comments, denoted by //
    • Multi-line comments, denoted by /* and */

    A single-line comment extends from the given // till the end of the line, and a multi-line comment begins with /* and ends with */, encompassing multiple lines of text.

    For example, in a C program:

    // This is a single-line comment
    /* This is a
       comment */

    Benefits of using comments in C code

    There are several benefits of using comments in C code, which include:

    • Improving code readability and understanding for other developers
    • Providing a clear explanation of the code's functionality and purpose
    • Assisting in debugging and maintaining the code efficiently
    • Easing the process of updating and modifying the code in the future
    • Helping to create proper documentation for the code, thus improving overall project organization

    Best practices for writing comments in C

    To maximize the benefits of using comments in C programming, it is essential to follow good commenting practices. Some of the best practices for writing comments in C include:

    • Adding comments to explain complex or confusing code snippets
    • Keeping comments concise and to the point
    • Updating comments whenever the code is changed or modified
    • Avoiding excessive or unnecessary commenting
    • Using proper grammar and punctuation in comments
    • Writing comments while coding, rather than adding them as an afterthought
    • Commenting on the intent of the code, rather than explaining what the code does line by line

    When working on a collaborative project or contributing to a shared codebase, following established commenting conventions and guidelines within the team or organisation is crucial for maintaining consistency and facilitating better communication among developers.

    Types of Comments in C

    In C programming, there are two primary types of comments used to provide explanations and descriptions within the code. These two types are single line comments and block (or multiline) comments.

    Single-line comments in C

    Single-line comments, as the name suggests, are comments that span just one line in the code. They are used to add brief explanations or descriptions about specific lines of code to improve readability and maintainability. Single-line comments are denoted by two forward slashes (//).

    Syntax and usage of single-line comments

    The syntax for single-line comments in C programming is quite simple. You place the two forward slashes (//) before the text you want to add as a comment. The compiler will then ignore the text following the two forward slashes until the end of the line.

    Here's an example of a single-line comment in C:

    // Declare and initialise variable x
    int x = 5;

    Some recommendations when using single-line comments are:

    • Write concise and meaningful comments for better readability
    • Place comments above the code they reference to keep them visible and easily accessible
    • Do not overuse single-line comments to avoid cluttering the code

    Block comments in C

    Block comments, also known as multiline comments, consist of multiple lines of text and are often used to provide more elaborate explanations or descriptions of code snippets or functions. These comments are denoted by a pair of symbols - an opening (/*) and a closing (*/).

    Syntax and usage of block and multiline comments

    The syntax for block comments in C begins with an opening symbol (/*) followed by the comment text, and it ends with a closing symbol (*/). Unlike single-line comments, block comments can span across multiple lines.

    An example of a block comment in C:

    /* This function takes an integer input
       and returns the square of the input value */
    int square(int x) {
        return x * x;

    Some recommendations when using block comments are:

    • Use block comments to describe complex or important code sections rather than adding multiple single-line comments
    • Place block comments before the code they describe, such as prior to a function or a crucial code block
    • Keep the block comments updated if the related code is modified

    Comparing single-line and multiline comments in C

    There are some key differences and similarities between single-line and multiline comments, which are important to consider when writing C code.

    Some key differences include:

    • Single-line comments span only one line and use two forward slashes (//), whereas block comments can span multiple lines and are denoted by an opening (/*) and a closing (*/) symbol
    • Single-line comments are more suitable for brief inline explanations and descriptions, whereas block comments are ideal for longer and more detailed explanations

    The main similarity between single-line and block comments is that both are non-executable by the compiler and serve the purpose of providing explanations and descriptions within the code to improve readability and maintainability.

    Understanding and using both types of comments appropriately can greatly enhance the clarity and quality of your C code, making it easier to understand, maintain, and debug.

    Application of Comments in C Programming

    Comments in C programming have various applications that serve essential purposes, such as documenting functions and algorithms, debugging code by commenting out sections, and adding credits and licensing information within the code. These applications significantly enhance the quality, maintainability, and collaborative aspect of the code.

    Documenting functions and algorithms with comments

    One of the key applications of comments in C programming is documenting functions and algorithms to provide essential explanations, usage instructions, and important details about the code.

    Providing explanations and usage instructions

    Comments can be used to explain the following aspects of functions and algorithms:

    • Purpose of the function or algorithm, and the problem it solves
    • Parameters that the function takes, their types, and their roles in the algorithm
    • Return type and the meaning of the returned value
    • Any assumptions made by the function or algorithm
    • Required inputs, constraints, or preconditions
    • Output format, postconditions, and side effects
    • Performance characteristics and complexity
    • Examples of usage, including sample input and output values
    • References to external resources or related algorithms, if applicable

    Documenting functions and algorithms in a consistent and thorough manner can make the code significantly easier to understand, maintain, and extend by other developers. It can also help in creating comprehensive technical documentation for the software.

    Commenting out sections of code during debugging

    Another important application of comments in C programming is during the debugging process. Developers can comment out sections of code temporarily to isolate certain code snippets and identify issues or errors in the code.

    Temporarily disabling code segments for testing

    Commenting out sections of code is helpful for:

    • Isolating problematic code segments to locate the root cause of an issue
    • Testing specific code sections independently to verify their correctness
    • Disabling features or functionality that are not relevant during testing or debugging
    • Preventing compilation errors due to incomplete or experimental code segments

    To comment out a section of code, you can use either single-line comments (//) or block comments (/* */) depending on the size of the code segment. However, it is essential to remember to remove or revise the commented-out sections once the debugging process is complete, as unused or outdated code can lead to future confusion or maintenance difficulties.

    Adding credits and licensing information within code

    Comments in C programming can also be used to include important information such as credits, licensing details, and copyright notices within the code, assisting collaborators, users, and contributors in understanding the authorship and usage rights of the software.

    Providing necessary information for collaborators and users

    Some important details that can be added within comments:

    • Author name(s) and contact information
    • Affiliation(s) or project team details
    • File or software version and date of creation or modification
    • Licensing details, including the name and version of the license, and a link to the full text of the license
    • Copyright notices and disclaimers
    • Acknowledgements for contributions from other developers or sources
    • References to related projects or resources

    By including credits and licensing information within the code, developers can help ensure that the contributions, rights, and responsibilities of all contributors and users are acknowledged and respected, fostering a more collaborative and open development ecosystem.

    Comments in C - Key takeaways

    • Comments in C: Non-executable explanations within code to improve readability and maintainability.

    • Adding comments in C: Single-line comments denoted by //, and multi-line comments denoted by /* and */.

    • Benefits of comments: Enhancing code readability, documentation, and facilitating debugging and maintenance.

    • Best practices: Good commenting practices include concise explanations, timely updates, and non-redundant commenting.

    • Applications: Comments in C can be used for documentation, debugging, and providing credits and licensing information.

    Frequently Asked Questions about Comments in C
    What are comments in the C language?
    Comments in C language are lines of text written within the code to provide explanations, notes, or guidance for the programmer or anyone reading the code. They are ignored by the compiler during the execution of the program, meaning they do not affect the output. In C, single-line comments start with '//' and multi-line comments are enclosed between '/*' and '*/'. Comments are useful for understanding and maintaining complex code.
    How do you write a comment in C?
    To write a comment in C, use the double forward slash (//) for single-line comments or enclose multi-line comments within /* and */. Single-line comments are used to explain a single line of code, while multi-line comments can explain a block of code or an entire section.
    Why do we write comments in C?
    We write comments in C to provide explanations and important information about the code, making it easier for others to understand its functionality and purpose. Comments help with the maintenance and debugging of the code over time, as they offer clarity and context for developers working on the project. Additionally, comments can be used to temporarily disable sections of code during testing. Overall, comments improve code readability and collaboration among developers.
    What is the difference between comments and /* comments */?
    The difference between `// comments` and `/* comments */` lies in the way they are used in C programming. `// comments` are single-line comments that comment out only the line following the double slashes. On the other hand, `/* comments */` are multi-line comments that can span across multiple lines, starting with `/*` and ending with `*/`. These are used to provide more extensive explanations or to temporarily disable a block of code.
    What is an example of a comment?
    An example of a comment in C can be a single-line comment, using double forward slashes, such as: `// This is a comment.`. Another example is a multi-line comment, enclosed between `/*` and `*/`, like this: ``` /* This is a multi-line comment */ ```

    Test your knowledge with multiple choice flashcards

    What is the purpose of adding comments in C programming?

    What are the two types of comments in C programming?

    What is the meaning of a single-line comment in C?


    Discover learning materials with the free StudySmarter app

    Sign up for free
    About StudySmarter

    StudySmarter is a globally recognized educational technology company, offering a holistic learning platform designed for students of all ages and educational levels. Our platform provides learning support for a wide range of subjects, including STEM, Social Sciences, and Languages and also helps students to successfully master various tests and exams worldwide, such as GCSE, A Level, SAT, ACT, Abitur, and more. We offer an extensive library of learning materials, including interactive flashcards, comprehensive textbook solutions, and detailed explanations. The cutting-edge technology and tools we provide help students create their own learning materials. StudySmarter’s content is not only expert-verified but also regularly updated to ensure accuracy and relevance.

    Learn more
    StudySmarter Editorial Team

    Team Comments in C Teachers

    • 9 minutes reading time
    • Checked by StudySmarter Editorial Team
    Save Explanation

    Study anywhere. Anytime.Across all devices.

    Sign-up for free

    Sign up to highlight and take notes. It’s 100% free.

    Join over 22 million students in learning with our StudySmarter App

    The first learning app that truly has everything you need to ace your exams in one place

    • Flashcards & Quizzes
    • AI Study Assistant
    • Study Planner
    • Mock-Exams
    • Smart Note-Taking
    Join over 22 million students in learning with our StudySmarter App