C Comments

C Comments

In C, comments are notes in the code that humans can read. They don’t affect how the program runs, but they make the code easier to understand. It’s a good practice to use comments to explain your code.

When and Why to use Comments in C programming?

  1. Improving Readability: Without comments, large and complex code can be confusing and challenging to understand. Comments provide essential details and context, making it easier for anyone reading the code to follow along.
  2. Descriptive Documentation: Comments serve as a form of documentation, explaining the purpose of functions, algorithms, and code segments. This documentation helps programmers and maintainers understand how the code works.
  3. Algorithm Explanations: Comments can be used to describe algorithms, making the code’s logic and methodology clear. This is particularly useful for complex or non-intuitive algorithms.
  4. Code Control: Comments can also be used strategically to comment out (prevent execution) of certain code sections during debugging or testing without actually removing the code.

Types of comments in C

In C there are two types of comments in C language:

  • Single-line comment
  • Multi-line comment

1. Single-line Comment in C

In the C programming language, a single-line comment is denoted by two forward slashes (//). When you include // in your code, everything from that point until the end of the line is treated as a comment by the compiler and is not executed as code. This allows you to add explanatory notes or comments to your code without affecting the program’s functionality.

Syntax of Single -line Comment

// This is a single line comment

Example 1: C Program to illustrate single-line comment

// C program to illustrate
// use of single-line comment
#include <stdio.h>
 
int main(void)
{
    // This is a single-line comment
    printf("Welcome to Skill vertex");
    return 0;
}

Output

Welcome to Skill vertex

Comment at End of Code Line

We can also make a comment that shows at the end line of code using a single-line comment. But , it’s most preferable in putting the comment before the line of code.

// C program to demonstrate commenting after line of code
#include <stdio.h>
 
int main() {
    // single line comment here
   
      printf("Welcome to Skill vertex"); // comment here
    return 0;
}

Output

Welcome to Skill vertex

2. Multi-line Comment in C

In C, multi-line comments are enclosed between /* and */, and anything between these delimiters is treated as a comment and ignored by the compiler. This allows you to add comments that span multiple lines in your code, making it easier to provide detailed explanations or temporarily exclude blocks of code from execution.

Syntax of a multi-line comment in C:

/*Comment starts
    continues
    continues
    .
    .
    .
Comment ends*/

Example 2:  C Program to illustrate the multi-line comment

/* C program to illustrate
use of
multi-line comment */
#include <stdio.h>
int main(void)
{
    /*
    This is a
    multi-line comment
    */
   
      /*
    This comment contains some code which
    will not be executed.
    printf("Code enclosed in Comment");
    */
    printf("Welcome to Skill Vertex");
    return 0;
}

Output

Welcome to Skill Vertex

FAQ- C Comments

Q1.What are types of comments?

Ans. There are basically two types of comments
Line comment
Block comment

Q2.What are the 3 types of comment?

Ans. Single-line comments. Multi-line comments. Documentation comments

Q3. What is comment method?

Ans. A method comment, often referred to as a function or method documentation comment, is a detailed comment placed at the beginning of a function or method in the source code. Its purpose is to provide a clear and comprehensive description of the method’s functionality, its parameters (including their types), return type, and possibly other important information

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment