From MISRA C 2004:
Rule 2.2 (required): Source code shall only use /* … */ style comments.
This excludes the use of //
C99 style comments and C++ style comments, since these are not
permitted in C90
. Many compilers support the //
style of comments as an extension to C90
. The
use of //
in preprocessor directives (e.g. #define
) can vary. Also the mixing of /* … */
and //
is not consistent. This is more than a style issue, since different (pre C99
) compilers may behave differently.
I think this explains it all, but I'll still add my explanation with links.
In original C (pre C99), there were no //
comments. C++
introduced a double slash comment prefix //
as a way to comment single lines.
For more clarity, let's go line by line:
This excludes the use of //
C99 style comments and C++ style comments, since these are not
permitted in C90
.
Nowadays, modern compilers like gcc
have started supporting such //
in C90 mode also, but will still issue a warning like:
warning: C++ style comments are not allowed in C90
but it is recommended to use /*..*/
comments in C90 C.
Many compilers support the //
style of comments as an extension to C90
.
That is what I exactly told above, but it is still not recommended to use //
in C90 C as the code will not be portable. For example, that code will compile with gcc
, but with another compiler which does not have the extension, it will not compile.
The
use of //
in preprocessor directives (e.g. #define
) can vary.
I think the meaning of this is clear. It says that the usage of //
is not going to be consistent in preprocessor directives.
Also the mixing of /* ... */
and //
is not consistent.
How? This is possible. Like I mentioned above, it varies from compiler to compiler. In C99 C, it will be mostly consistent, but if you're talking about the pre C99 compilers, it matters if they support or not support //
comments.
This is more than a style issue, since different (pre C99
) compilers may behave differently.
Yes it is. As this is MISRA 2004, which is mainly talking about pre C90 C mainly. This is because most C90 compilers do not support //
, and thus you will get an error. If you're using C99, this is invalid.
Questions Expected
Why was there no //
in pre C99 C, and then why was it introduced in C++? Is there a specific reason?
C
originated from B
. B
originated from BCPL
. //
were created during BCPL
, and thus BCPl
used them. It's successor B
started using /*...*/
, which were inherited by C
. Although, C++
decided to resurrect the BCPL
comments, and of course used /*...*/
also. The C-standard at that time took //
as a non-standard feature.
When C99 C was introduced, it had the comments //
.
What are //
and /*...*/
comments called?
//
comments are called single-line comments as they can only extend till one line.
/*...*/
comments are called multi-line comments as they can extend upto multiple lines.
Is it compulsory to use only one kind of comment through your source code?
No, you can use any and even both. Although, some people say that using only one type makes the code readable. It is more a matter of opinion.
Is rule 2.2 of MISRA C 2004 outdated?
Kind of. Recently, MISRA 2012 was released which has these changes. Although, if you think that MISRA C 2004 is wrong as it is only concentrating on pre C99, it isn't. I would recommend using MISRA 2012 nowadays.