For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
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. That’s why, you’ll find comments in C as a must-have topic in highly-recommended software development courses.
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:
Let your career take a flight with the following full-stack development courses:
In C, two types of comments are available. These are single-line, and multi-line comments. However, there’s also a third-type, known as nesting comments, which are not used in C. But, we’ll explore them too to strengthen our foundational knowledge. Let’s understand with the help of examples:
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 in C:-
#include <stdio.h>
int main() {
// Single Line Comment
printf("Hello World!");
return 0;
}
Output:
Hello World!
In the above code, the compiler has ignored the commented line in the execution. The code provided here is only a basic example, you can explore our detailed article on developing a hello world C program.
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 in :-
#include <stdio.h>
int main() {
/*
Let's see the example of,
Multi-Line Comment
*/
printf("Hello World!");
return 0;
}
Output:
Hello World!
In this example, the compiler has ignored the commented line in the execution.
Nesting comments refers to placing one comment block inside another. While this is a useful feature in some languages, C does not support nested comments. In C, the /* and */ delimiters define a comment block, and once the compiler encounters the first */, it treats that as the end of the comment—regardless of whether a second /* was opened inside it.
Trying to nest multi-line comments can lead to compilation errors or unexpected behavior, as the compiler will misinterpret the structure of the comments.
Example of Improperly Nested Comments in C:
#include <stdio.h>
int main() {
/*
This is a comment block
/* This inner comment is invalid */
This will cause an error
*/
printf("Hello World!");
return 0;
}
Output:
Compilation Error
In this example, the compiler terminates the comment at the first */, treating the remaining text as code, which leads to a syntax error. To avoid such errors, avoid placing one comment block inside another. Instead, use single-line comments (//) if you need to temporarily disable lines within a block comment or break the comment into separate parts.
The nesting comments in C are of no use to you. But, the nested loop in C concept is a must to explore. In addition, you should also learn about the do while loop and For loop 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 synchronized 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 organization 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.
The main reasons for the need for comments in C are:
Improved Readability and Maintainability: By utilizing 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 summarize 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 utilization, especially after a long period or when the code is reused in other projects.
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.
Additionally, you should also learn about the different C compilers as per the operating systems. Explore our articles on:
Apart from this, you should also learn about the difference between compiler and an interpreter.
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 utilizing 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.
Furthermore, developers should follow consistent commenting practices and ensure that comments stay up to date with code changes. Outdated or misleading comments can be more harmful than none at all, as they may create confusion or lead to incorrect assumptions about how the code works. Effective comments are clear, concise, and purposeful—serving as a guide without overshadowing the readability of the actual code.
1. Can I use comments to disable code temporarily in C?
Yes, comments are often used to temporarily disable lines of code during debugging or testing. By commenting out specific lines, you prevent them from being compiled or executed. This is useful for isolating issues or testing different sections without permanently removing the code. Just remember to remove or restore them when you're done.
2. Are comments visible in the compiled binary?
No, comments are not included in the compiled binary. The C compiler strips out all comments during the preprocessing stage, so they are never part of the executable file. This ensures that comments have no effect on the size or performance of the compiled program, and they remain only in the source code.
3. Should comments be written in full sentences?
While it's not mandatory, writing comments in full, clear sentences improves readability and professionalism, especially in collaborative projects. Proper grammar and punctuation help convey the intended message more clearly. However, short fragments or keywords are acceptable for brief explanations, as long as the meaning remains obvious to anyone reading the code.
4. What are some best practices for writing comments in C?
Keep comments concise and relevant. Avoid restating obvious code logic. Focus on explaining why something is done, not what is being done. Update comments when the code changes, and use consistent formatting. Place comments above or beside the code they refer to, and avoid excessive commenting that can clutter or confuse the source.
5. Can I use comments inside macros or preprocessor directives?
You can use comments near preprocessor directives or macros, but be careful not to interfere with syntax. Avoid placing multi-line comments inside macros as they can cause issues with line continuation characters (`\`). Use single-line comments for safer inline notes within or near macro definitions to maintain clarity and prevent errors.
6. Are comments necessary in every function?
Not necessarily. Simple and self-explanatory functions may not need comments. However, it's good practice to comment on the purpose, parameters, and return values of more complex or less obvious functions. Aim to clarify logic and intent where needed without overexplaining straightforward code, especially when naming conventions are already descriptive.
7. How can I comment out multiple lines quickly in an IDE?
Most modern IDEs and code editors offer shortcuts to comment or uncomment multiple lines at once. For example, in many editors, you can select the lines and press `Ctrl + /` or use block comment shortcuts like `Ctrl + Shift + /`. These tools help you edit comments efficiently during development or debugging.
8. Do professional C programmers use comments heavily?
Professional programmers use comments thoughtfully—not heavily. Instead of commenting every line, they focus on meaningful explanations where the logic is complex or non-obvious. Clean, well-named variables and functions often reduce the need for comments. The goal is to write self-documenting code and supplement it with comments only where necessary.
9. Can I use comments for documentation in C?
Yes, comments are often used for internal documentation. Tools like Doxygen allow you to generate external documentation from specially formatted comments. By using structured tags in your comments, you can create comprehensive documentation for functions, parameters, and structures, which is particularly useful in large or open-source C projects.
10. What are the risks of using outdated comments?
Outdated comments can be misleading and harmful. They may describe behavior that no longer exists, causing confusion or errors during maintenance. Always update or remove comments when code changes. Relying on incorrect comments can lead to poor debugging decisions or misunderstandings in collaborative environments, where clarity and accuracy are essential.
11. Are there tools to check comment quality or consistency in C?
While C compilers don’t check comment quality, static analysis tools and linters like `clang-tidy`, `cppcheck`, or integrated tools in IDEs can help enforce commenting standards. Some tools also flag TODOs, check for missing documentation, or highlight inconsistencies. Using these tools helps maintain clear, professional, and maintainable codebases.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.