C Tutorial



C Tutorial

Comments in C

Comments in C are non-executable statements that provide information and explanations within a C program's source code. They are mainly used for documentation purposes and to improve code readability. The compiler ignores comments and has no impact on the program's execution. They allow programmers to add notes, clarify code logic, and make the program more understandable to others. 

C supports two types of comments: single-line comments, which start with "//" and extend to the end of the line, and multi-line comments, which are enclosed between "/" and "/". Programmers can use comments effectively to enhance code maintenance, collaboration, and comprehension. During the compilation process, comments are disregarded by the compiler and have no impact on the program's execution. 

Here is the most basic syntax of comments in C:

// Your comment Here
    Your comment Here

If the compiler disregards the comment in the program’s execution, then why do we use them in C programming? Let’s understand the Importance of comments in C programming language:

  • Comments in C are crucial for improving code readability, especially when dealing with large codebases. Without comments, it can be confusing for someone to understand the details of the program.

  • C comments provide additional descriptions and explanations within the code, making it easier for developers to comprehend its functionality.

  • Comments can be utilised to describe algorithms employed in the code, helping others understand the logic and purpose behind the implemented solution.

  • Another useful aspect of C comments is their ability to prevent the execution of specific code segments. By commenting on certain parts of the code, those sections are excluded from the program's execution, providing flexibility for debugging or temporarily disabling functionality.

Types of C Comments:

In C, two types of comments are available. These are single-line and multi-line comments. Let’s understand with the help of examples:

I. Single-line Comments

Single line comments in C provide explanations or descriptions on a single line of code. They can be placed anywhere within the line and extended until the end or a new line. These comments are initiated using the "//" character sequence, indicating that the text following it is a single-line comment. For instance, consider the following example:

Syntax of Single-line Comments:-

#include <stdio.h>

int main() {
    // Single Line Comment
    printf("Hello World!");
    return 0;

This is the output:

Hello World!

In the above code,  the compiler has ignored the commented line in the execution.

II. Multi-line Comments

Multi-line comments in C allow for including one or more narrative lines within a specific comment block. These comments are enclosed between the /* delimiter, which marks the beginning of the comment, and the */ delimiter marks the end. The comment block can span multiple lines; content between these delimiters is considered a comment. For example

Syntax of Multi-line Comments:-

#include <stdio.h>

int main() {
        Let's see the example of,
        Multi-Line Comment
    printf("Hello World!");
    return 0;

This is the output: 

Hello World!

In this example, the compiler has ignored the commented line in the execution.

Rules for writing comments in C

Here are some of the basic rules of writing comments in C language: 

  • Accurate Comments: It is essential to ensure that comments are correct and accurately reflect the code's functionality. Incorrect comments can mislead readers and potentially lead to compilation errors.

  • Updating Comments: Whenever changes are made to the code, it is necessary to update the corresponding comments accordingly. This ensures that the comments remain synchronised with the code and provide accurate information.

  • No Nesting of Comments: Comments should not be nested, meaning that one comment pair should not appear inside another. Each comment should be self-contained and independent.

  • Splitting Comments: Splitting comments into multiple lines is acceptable to improve readability or provide more detailed explanations. This helps in maintaining clarity and organisation within the comments.

  • Flexible Comment Placement: Comments can be added at any location within a program, offering the freedom to provide explanatory notes, descriptions, or documentation wherever necessary. There are no restrictions on the number of comments that can be included in a single program.

Why do you need comments in C?

The main reasons for the need for comments in C are:

  • Improved Readability and Maintainability: By utilising comments, we can enhance the readability and maintainability of our code or program, especially when it becomes lengthy. Comments provide valuable explanations, making it easier for developers to understand the logic and structure of the code.

  • Summary and Clarification: Comments help summarise algorithms, identify variable purposes, or clarify unclear code segments. They serve as concise descriptions that aid in understanding the code's functionality and purpose.

  • Effective Communication with Other Programmers: When we anticipate that other programmers will read our code, comments become particularly valuable. They help explain the workings of the code, making it easier for others to comprehend and collaborate on the project.

  • Personal Aid and Recap: Comments also benefit the code's original author. They serve as reminders and recaps of the code's information, enabling quick comprehension and utilisation, especially after a long period or when the code is reused in other projects.

What does the compiler do with the C comments?

Within a compiler, the lexical analyzer scans characters and converts them into tokens. During this process, comments are not passed to the parser. As a result, comments provide human-readable explanations and do not affect the program's functionality. Essentially, comments are ignored by the compiler and considered non-existent during compilation. Their sole purpose is to enhance code readability and understanding. Therefore, the comments in C are disregarded by the compiler, as it focuses solely on the executable code and does not consider comments part of the program's logic.

Advantages of commenting on a program

  • Code Documentation: Comments serve as self-explanatory documentation within the code, providing crucial details about the program's functionality, algorithms used, and any important considerations.

  • Debugging Assistance: Comments can act as clues during debugging processes, helping developers identify and fix issues more efficiently by providing insights into the code's behaviour and expected outcomes.

  • Collaboration and Teamwork: Comments promote collaboration among developers, allowing them to understand and work on each other's code more effectively, even if they are unfamiliar with the specific implementation details.

  • Code Review: Comments are valuable during code review processes, enabling reviewers to understand the code and provide feedback, suggestions, or improvements.

  • Reminder and Planning: Comments can serve as reminders or to-do lists for future optimizations, improvements, or necessary changes, ensuring that important tasks are not overlooked or forgotten.

  • Compliance and Standards: Comments help adhere to coding standards and best practices, ensuring consistent and well-documented code across the project.

  • Avoiding Code Execution: Comments can temporarily disable or "comment out" code sections, allowing developers to test alternative approaches or isolate problematic code without deleting it permanently.


To sum up, comments in C can greatly aid in code comprehension for yourself and other programmers utilising your code. They act as annotations, providing helpful narratives that explain the purpose and functionality of the code. However, it is important to strike a balance and avoid overusing comments, as excessive commenting can make the code harder to read and understand.

To amplify your overall understanding of C programming language, upGrad provides a specialised course in MS in Computer Science from Liverpool John Moores University to amplify your overall understanding of C programming language. The course will teach you about full-stack development, 7+ programming tools and languages, and many more. 


1. What is a comment statement?

A comment is a statement placed within the source code by the programmer which will be ignored by the compiler. It is of two types: single-line comments which are enclosed within // and multi-line comments which are enclosed within /. 

2. What is the comment method?

A method comment is a comment placed before or directly after a method in code. These comments explain what the method does, what kind of parameters are accepted, the type of data that the parameters accept, and what type of value is returned.

3. Why are comments used in programming?

Adding comments inside of code is a helpful way to explain what each line of code does. This can be useful for making bug fixes simpler and quicker to locate. Furthermore, it is essential for developers who plan to share their program with others, so that coworkers or colleagues can understand and use the code.

Leave a Reply

Your email address will not be published. Required fields are marked *