C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C programming language

Comments in c programming language

Standard C programming language supports only multi line comment characters that can be used as single line comment or multi line comment.

It may be just an exception, if any compiler support single line comment character (//) with the file extension .c

How to add a comment in c program?

In c programming language program, you can add any number of comments, comments provide clarity to the source code. Comments are necessary to explain the logic (that you write in the source code) to the other with yourself too (if you rewrite or change the code).

A comment starts with set of two character /* (slash, asterisk) and ends with set of two characters */ (asterisk, slash). That means between these characters sets you can write a comment (whatever you want to write - related to the program)






Here is the syntax

/*Comment goes here*/

or

/*
    Comment (line1) goes here
    Comment (line2) goes here
    ...
    Comment (linen) goes here
*/
    

You can also add comments between the statements, have a look

printf("Value of x= %d\n",/*value*/x);

Let’s consider the following example

/*
Program - Demonstrate example for comments 
Author - IncludeHelp
Date - 25/09/2016
*/

#include <stdio.h>

int main()
{
	/*int value=100;*/

	int x=10;
	printf("Value of x= %d\n",/*value*/x);

	return 0;
}

In this example we added following comments

  • Before the program - to define program, author name and date.
  • /*int value=100;*/ within the main function, here we are commenting an integer variable value.
  • /*value*/ inside printf statement, we are commenting the variable value.

What does the compiler do with the comments?

There is an inbuilt program in the compiler which is called "Lexical Analyzer" which scans the characters and converts them into tokens.

Usually, it is the responsibility of "Lexical Analyzer" to take care of comments; it does not pass the commented text to the parser.

Hence we can understand that comments are ignored by the compilers, when program is going to be compiled.

Reference: https://www.quora.com/How-exactly-does-the-compiler-deal-with-comments-in-code-specifically


COMMENTS