When I was solving a problem from the famous KR book to remove comments from a c program I have searched over the internet to know all the possible cases where comments can be inserted in a C program and didn't find any complete answer of this question. So here I am adding the answer to this question. feedback is welcomed.
How many ways are there to insert comments in a C program?
When a single line comment symbol is inserted anywhere in a line everything that appears before a newline is part of the comment.
Any type of comment symbol can appear inside a double-quoted string — so single-line and multi-line comment symbols appearing inside double quotes are not a comment; they are normal text.
Multi-line comments can be placed anywhere in the source code where you can put a blank.
In C, comments cannot be nested. Inserting a multi-line comment start symbol inside another multiple line comment will truncate the parts of comments that come after the first comment terminating symbol. Check the example of #4 to understand clearly.
#include <stdio.h>
#define TABSIZE /*false comment*/ 8
// this is a demo program. /* comment here */ XD example of #1
int main()
{
printf("#1 :- /* multiple */ and // single \n"); // example of #2
char str[40] = "/* multiple */ and // single"; // example of #2
printf("#2 :- %s\n", str);
char one /* example of #3 */ = 'x'; // example of #3
char two /* example of #3 */ = 'y'; // example of #3
printf("#3 :- %c and %c\n", one, two);
return /*example of #3 */ 0; // example of #3
/*(start1) this is a comment /*(start2) trying to- -- example of #4
insert an nested comment here */
// (e2) this part is not considered as comment */(e1).
// so to successfully run the program this part of the
// comment is handled by single line comment symbol '//'.
// remove all the '//' to see what happens.
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.