Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

really? then how do you #define things like types like `int` and `char`?


For example, it would be illegal to do the following:

> #define int long

Because you're replacing the int keyword with something else.

The standard says:

> 17.6.4.3.1 [macro.names] paragraph 2: A translation unit shall not #define or #undef names lexically identical to keywords, to the identifiers listed in Table 3, or to the attribute-tokens described in 7.6.


That's C++. But I couldn't find the same restriction in C. In fact it seems that C allows it as long as you don't include any of the standard C header.

> 7.1.2 "Standard headers" §5 [...] The program shall not have any macros with names lexically identical to keywords currently defined prior to the inclusion of the header or when any macro defined in the header is expanded.


Searching around gave me this from stackoverflow:

In C standard, it is not allowed.

> C11, § 6.4.1 Keywords

> [...]

> The above tokens (case sensitive) are reserved (in translation phases 7 and 8) for use as keywords, and shall not be used otherwise.

https://stackoverflow.com/questions/12286691/keywords-redefi...


And so you still can re-define keywords, but only after you've included all the standard headers you want. Which makes sense: the meaning of including a standard header is entirely standard-mandated (they are not even required to be actual files) so making anything that could potentially mess with the implementation's implementation of standard headers UB is reasonable.


You don't.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: