Subscribe Us

How to write comment in C ? - comment in C language - smartduniya

Hello guys, You are  learning  C programming from scratch . Hope you will see my previous post about C programming .Today, I'll be talking about commenting in c language / C programming .

How to write comment in C

So let's begin with a set of questions like

 What is commenting in C programming ? How many ways of commenting there are in C ? .

The answer to the first question is commenting in C  is the process of documenting source code.

It helps programmer to remind the fact that why he written the code or anything else.  Documenting source code means that the statement in the program will have explanation right beside them which confronts the programmer with the role of code in each line without having those explanation affected our source code both syntactically and logically. I told it before .

 So I have already prepared our well documented c program for you.

Remember that user can’t see the comment text , only programmer see the comment text in program. 

 As you can see here these light blue colored statements are comments which explain the role of code in each line. When you run the program you just see “This is Red “ in out put.   I hope you understand comment.

These commands are non-executable part of code. What it means is these comments are ignored by compiler and it treats these comments as if they don't even exist.

Two types of Comment in C

 Now let's move to answering the second question which is How many ways there are to comment in C programming language .

There are two ways to comment and they are called single line commenting and multi-line commenting.

Single line commenting is used when a  statement wrapped up within a state sorry when a  statement wrapped up within a sentence is to be commented  and the symbol which is used for single line commenting is a pair of  forward slash and except for first comment here except for first  comment here in this code all of the all of these light-blue out  comments are single line commenting and multi-line commenting is required when statements to be commended take up more than one line.


For example the first comment here

// This is single line comment .

 for multi-line commenting a forward slash followed by an asterisk is used and it must ends with an asterisk followed up by forward slash. Otherwise it will go on commenting  each and every line until it finds an asterisk  sign followed by a forward slash.

/* This is multi line comment

       We use multi line in this comment . */


For example if I remove this asterisk sign followed by forward slash the entire code is commented.

Look, everything is light blue . So, you must not forget to end multiple commenting with an asterisk sign  followed by a forward slash. Now let's move to other use of  commenting except for documentation.

For example if there's a statement in your source code which you don't want compiler to execute and treat it as if it doesn't even exist then commenting is used. Let's execute this concept here in this  code. I don't want this compiler to display  enter the value of a and b. So I will put a pair of forward slash right before it to make it non-executable and i put a pair of forward slash because this printf() statement is wrapped up within a line.

Now let's run this code and see now it is not displaying enter the values of a and b. I will enter the value of a five and b to be six. If i press enter , now the sum of a and b is eleven. If I remove this pair of forward slash and then I clicked and run this code again then look, now it's asking for enter the values of a and b. So I will again enter the value of a to be 7 b to be 8.

Now press enter, now sum of a and b is 15. If I needed to if I needed compiler to ignore first four statements inside  the main function this first four statement inside the main function then i would use multi-line commenting. 

comment in C language

Let me show you here in this code. Forward slash followed by an asterisk sign, look, everything is light blue color   out, but just since I want the compiler to ignore only the first four statements I will end this multi-line commenting here an asterisk  sign followed by forward slash.

Now, look, except for return 0 everything inside main function section is light blue and i use multi-line commenting because the statements to be commented were taking up more than one line. So this was all about commenting and it is always considered to be good practice to have program documented.

comment in C language

So if you like this post keep your smile, if you face any problem in c programming Comment below . I will try to help you.

Thank you for reading.

>> Learn C Tokens , Identifiers , Keywords and rules for Identifiers

>> Learn Printf ( ) function in C programming

>> Learn Algorithm in C

Post a Comment