Home >Backend Development >C++ >Can C Preprocessors Redefine Keywords and Still Be Standards Compliant?
Redefining Keywords in C Preprocessing: Standards Compliance
The use of #define to redefine language keywords has sparked debate in the developer community. This article delves into the standards regarding this practice.
Standards Conformance with Redefined Keywords
According to the C Standard (§17.4.3.1.1/2), a standards-conforming C preprocessor cannot redefine keywords if a translation unit includes a standard library header. This restriction is lifted for translation units without standard headers.
In C 0x, the second sentence of the aforementioned paragraph seemed to prohibit the redefinition of keywords altogether, leading to confusion. However, Ken Bloom clarified that the rules have not changed in C 0x. The text has merely been rearranged in a way that may cause misunderstanding.
Consequences for Non-Conformity
If a C program redefines a language keyword, it does not inherently become non-conforming. However, it is not guaranteed to be portable across different compilers or platforms. Additionally, such a program may encounter unexpected behavior or errors if it attempts to interact with standard library headers that expect the original keyword definition.
The above is the detailed content of Can C Preprocessors Redefine Keywords and Still Be Standards Compliant?. For more information, please follow other related articles on the PHP Chinese website!