Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are C++ comments considered bad style in C? [closed]

I was discussing C programming styles with some students and when we were talking about comments, one of them noted that he doesn't use C++ comments in C code because they are a bad idea. Turns out that it was based on personal experience with multi-line C++ comments, but it's not the first time I've heard that claim. So, is // considered harmful and if so, then why?

like image 559
Igor Avatar asked Aug 29 '10 12:08

Igor


People also ask

Are comments in C interpreted by the compiler?

What Is Comment In C Language? A comment is an explanation or description of the source code of the program. It helps a developer explain logic of the code and improves program readability. At run-time, a comment is ignored by the compiler.

What is the correct way of making comments in C?

There are two ways to add comments in C: // - Single Line Comment. /*... */ - Multi-line Comment.

How are comments represented in C?

Represented as /* any_text */ start with forward slash and asterisk (/*) and end with asterisk and forward slash (*/). It is used to denote multi-line comment. It can apply comment to more than a single line.

How do you comment out a block of code in C?

In the C/C++ editor, select multiple line(s) of code to comment out. To comment out multiple code lines right-click and select Source > Add Block Comment. ( CTRL+SHIFT+/ )


2 Answers

It depends what version of C you are using. C 99 allows // as a comment, whereas C 89 doesn't.

If you want to be as backward compatible as possible, don't use them. But, I think this is an extreme fringe case. I'm willing to bet almost everyone uses C 99.

Edit: Any recent version of GCC uses most of C99. You can find more info in Wikipedia.

like image 129
Codeacula Avatar answered Oct 31 '22 08:10

Codeacula


C++ comments are not allowed as per the MISRA-C 2004 standard. Certain industries (automotive, specifically) prize MISRA compliant code and therefore, C++ comments are not allowed. I believe the same goes for other static code checking tools such as LDRA, etc...

This doesn't make them inherently bad, but it does mean that if you get into certain industries and want to work professionally, you will be actively discouraged from using C++ style comments.

like image 26
dls Avatar answered Oct 31 '22 08:10

dls