CodeSutraHub

Comments in C Language

Writing Comments in C Language Programs

In C programming, comments are explanatory statements used to understand the code. They are not visible to the compiler, meaning they are not part of the program execution. They are only used to help us and other developers understand the code easily.
In C, there are two types of comments:
  • Single-line comment
  • Multi-line comment

Single Line Comments in C Language

  • Starts with //.
  • Used to write a short explanation in a single line.

Example:
// This is a single line comment
printf("Hello, Welcome to C World");
        

Multi Line Comments in C Language

  • Starts with /* and ends with */.
  • Used to write explanations across multiple lines.

Example:
/* This is a multi-line comment
   You can write multiple
   lines between /* and */
   and continue explaining
*/
printf("Hello, Welcome to C World");
        

Important Points about Comments in C Language

  • Comments are not part of the program.
  • The compiler ignores comments.
  • They help in understanding the code.
  • They are useful to explain code to others.

Example Program: Using Comments in C Language


#include <stdio.h>

/* This is a C program
   This program creates two integer variables
   The sum is stored in another variable
*/

int main() {
    // variable declaration
    int a = 10, b = 20, c;

    c = a + b;

    // printing result on screen
    printf("Sum = %d", c);

    return 0;
}
        
In the above program, both multi-line and single-line comments are used. The multi-line comment is used to describe the program. The single-line comment is used twice in the program. When the program is compiled, the compiler completely ignores these comments. Therefore, comments are not part of the program execution.

So far, we have learned:

  • Single-line comments
  • Multi-line comments
In the next topic, we will learn about C Arithmetic Operators.

Below Video Explains Programming Languages