Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are tokens after #endif legal?

I currently do the following and the compiler (MSVC2008 / as well as 2010) doesn't complain about it but I'm not sure if it's a bad idea or not:

#ifndef FOO_H_
#define FOO_H_

// note, FOO_H_ is not a comment:
#endif FOO_H_

I used to always write it as #endif // FOO_H_ but I caught myself not doing that today and thought it was strange because apparently I've not done the comment method for a while.

Is this bad practice that I should go back through all of my headers and fix (it's a cross-platform application) or is it okay to leave it the way it is?

like image 473
Joe.F Avatar asked Aug 11 '10 18:08

Joe.F


2 Answers

It is not ok, it is not valid, AFAIK. Many compilers ignore the extra text after the #endif though and often they warn about it. You should add the // to make it a comment.

like image 23
wilx Avatar answered Sep 22 '22 02:09

wilx


Strictly speaking (according to the grammar in the standard) no tokens are allowed following the #endif directive on the same line (comments are OK since they get removed at an earlier phase of translation than the preprocessing directives - phase 3 vs. 4).

However, MSVC seems to allow it - I wouldn't go on a quest to fix these (since they aren't causing a problem), but would probably make a mental note to fix them as you modify the headers that have them.

Of course, if your other supported compilers issue diagnostics about them it's probably more urgent to fix them.

like image 74
Michael Burr Avatar answered Sep 22 '22 02:09

Michael Burr