Tutorial Playlist
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 |
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:
In C, two types of comments are available. These are single-line and multi-line comments. 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:-
#include <stdio.h> |
This is the output:
Hello World! |
In the above code, the compiler has ignored the commented line in the execution.
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> |
This is the output:
Hello World! |
In this example, the compiler has ignored the commented line in the execution.
Here are some of the basic rules of writing comments in C language:
The main reasons for the need for comments in C are:
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.
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.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...