Comments in C Language

Comments are written in a c program that is not compiled and executed in c language. Comments are used to add information about code. Comments are used to block the code from compiling without permanently removing from the c file.

The commented lines are ignored when compiling a c file. Comments are used to add information such as program details, code logic, function details etc. The comments are added anywhere in the c file.



Types of C comments

There are two kinds of comments

  • Single line comments (//…)
  • Multi line comments (/* … */)


Single line comments (//…)

Single line comments are used to comment on a single line. It starts with two forward slashes (//) followed by comments until the end of the line.

#include<stdio.h>

int main()
{
	// Printing welcome message in the console
	printf("Welcome to Hello World!");
	return 0;
}

Output

Welcome to Hello World!

You can add single line comments after the code line as shown in the example below.

#include<stdio.h>

int main()
{
	printf("Welcome to Hello World!"); // Printing welcome message in the console
	return 0;
}

Output

Welcome to Hello World!


Multi line comments (/* … */)

Multi line comments are used to make a comment on one or more lines. If the commented information contains more than one line, a multi-line comment will be used. Multi-line comment starts with one forwarded slash followed by a star ( /* ) and ends with a star followed by one forwarded slash ( */ ).

#include<stdio.h>

int main()
{
	/*
	  Printing welcome message
	  in the console
	 */
	printf("Welcome to Hello World!");
	return 0;
}

Output

Welcome to Hello World!

Each multi line comments is optionally started with the star as shown in the example below.

#include<stdio.h>

int main()
{
	/*
	 * Printing welcome message 
	 * in the console
	 */
	printf("Welcome to Hello World!");
	return 0;
}

Output

Welcome to Hello World!



Related Articles

Leave a Reply

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