Why are Nested Comments forbidden?

C and C++ do it for ease of parsing. This way, when they hit a comment start of /*, the parser can trivially scan to the end. Otherwise, it would have to set up and maintain a stack, and then report errors if the comment tokens are unmatched.

As to why Java does it, the answer is simple - Java's syntax was designed to emulate C and C++. If nested comments were allowed, it might trip up some C programmers, and many angry blog posts would be written!


It is different for each language(-family) but in general they are not 'forbidden' but simply not supported. Supporting them is a design choice.

One reason for the choice (for older languages) may have been ease of parsing.

Note: I remember a C++ compiler where it was an option to let them nest. It was marked as 'non standard'.


At least for C++ that's only partly true, there's no problem with:

/*
//
//
*/

However if you already got an /* comment in a section you want to comment out you might be able to do it by surrounding it with an #if 0 which I think many compilers optimize away. As in:

#if 0
/*

*/
#endif