Is a compiler allowed to add functions to standard headers?

╄→гoц情女王★ 提交于 2019-12-28 06:58:11

问题


Is a C compiler allowed to add functions to standard headers and still conform to the C standard?

I read this somewhere, but I can't find any reference in the standard, except in annex J.5:

The inclusion of any extension that may cause a strictly conforming program to become invalid renders an implementation nonconforming. Examples of such extensions are new keywords, extra library functions declared in standard headers, or predefined macros with names that do not begin with an underscore.

However, Annex J is informative and not normative... so it isn't helping.

So I wonder if it is okay or not for a conforming compiler to add additional functions in standard headers?

For example, lets say it adds non-standard itoa to stdlib.h.


回答1:


In 4. "Conformance" §6, there is:

A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any strictly conforming program.

with the immediate conclusion in a footnote:

This implies that a conforming implementation reserves no identifiers other than those explicitly reserved in this International Standard.

The reserved identifiers are described in 7.1.3. Basically, it is everything starting with an underscore and everything explicitly listed as used for the standard libraries.

So, yes the compiler is allowed to add extensions. But they have to have a name starting with an underscore or one of the prefixes reserved for libraries.

itoa is not a reserved identifier and a compiler defining it in a standard header is not conforming.




回答2:


In "7.26 Future library directions" you have a list of the identifiers that may be added to the standard headers, this includes identifiers starting with str or mem, macros starting with E and stuff like that.

Other than that, implementations are restricted to the generic names as reserved in "7.1.3 Reserved identifiers".




回答3:


Compilers for embedded systems regularly add functions and macros to standard headers, usually to make a special processor feature available for use.

If I read the standard correctly, they can do so without sacrificing conformity if they do use names specified as reserved by the standard. Since a conforming program may use any non-reserved name as a variable or a function name, using such a non-reserved name as an addition to a standard header would break a conforming program.

In practice, however, the compiler writers usually do not care too much. They will at most provide a list of elements defined for the system you may not use if you want your program to work with their implementation.



来源:https://stackoverflow.com/questions/8270324/is-a-compiler-allowed-to-add-functions-to-standard-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!