tucuxi.org

C++-style comments in C89 considered harmful.

A demonstration of why one should never mix C++-style comments with C code, especially when mixing different compilers, or releasing open source code. In the below snippet, C89 compilers will interpret the equation as a = 20 / 2 + 79, ignoring only the C-style /* comment */. C99 compilers, on the other hand, will interpret the // as a rest-of-line comment, and treat the equation as a = 20 + 79.

Granted, this is an esoteric example of how comments can be interpreted differently between C89 and C99 compilers, but it boils down to one thing: using // for comments was, and never will be, part of the C89 standard. If you want your code to be widely portable, C89 is the way to go. If you only care about platforms that have up-to-date compilers (any modern GCC target, really), then go nuts. Just remember that C99 will bite you when addressing many embedded platforms, as there are many developers who use toolchains provided by vendors that do not support all (or in some cases, any) features of C99 or beyond.

bst$ cat c.c
#include <stdio.h>
 
int main(int argc, char **argv) {
  int a = 20 //* comment */ 2
      + 79;
 
  printf("Compiled with --std=c%d\n\n", a);
  return 0;
}
 
bst$ gcc --std=c89 -o c89 c.c
bst$ ./c89
Compiled with --std=c89
 
bst$ gcc --std=c99 -o c99 c.c
bst$ ./c99
Compiled with --std=c99

Download the code.