atoi is a standard function. But itoa is not. Why?

删除回忆录丶 提交于 2019-12-10 01:50:11

问题


Why this distinction? I've landed up with terrible problems, assuming itoa to be in stdlib.h and finally ending up with linking a custom version of itoa with a different prototype and thus producing some crazy errors.

So, why isn't itoa not a standard function? What's wrong with it? And why is the standard partial towards its twin brother atoi?


回答1:


No itoa has ever been standardised so to add it to the standard you would need a compelling reason and a good interface to add it.

Most itoa interfaces that I have seen either use a static buffer which has re-entrancy and lifetime issues, allocate a dynamic buffer that the caller needs to free or require the user to supply a buffer which makes the interface no better than sprintf.




回答2:


An "itoa" function would have to return a string. Since strings aren't first-class objects, the caller would have to pass a buffer + length and the function would have to have some way to indicate whether it ran out of room or not. By the time you get that far, you've created something similar enough to sprintf that it's not worth duplicating the code/functionality. The "atoi" function exists because it's less complicated (and arguably safer) than a full "scanf" call. An "itoa" function wouldn't be different enough to be worth it.




回答3:


The itoa function isn't standard probably for the reason is that there is no consistent definition of it. Different compiler and library vendors have introduced subtly different versions of it, possibly as an invention to serve as a complement to atoi.

If some non-standard function is widely provided by vendors, the standard's job is to codify it: basically add a description of the existing function to the standard. This is possible if the function has more or less consistent argument conventions and behavior.

Because multiple flavors of itoa are already out there, such a function cannot be added into ISO C. Whatever behavior is described would be at odds with some implementations.

itoa has existed in forms such as:

void itoa(int n, char *s); /* Given in _The C Programming Language_, 1st ed. (K&R1) */

void itoa(int input, void (*subr)(char)); /* Ancient Unix library */

void itoa(int n, char *buf, int radix);
char *itoa(int in, char *buf, int radix);

Microsoft provides it in their Visual C Run Time Library under the altered name: _itoa.

Not only have C implementations historically provided it under differing definitions, C programs also provide a function named itoa function for themselves, which is another source for possible clashes.

Basically, the itoa identifier is "radioactive" with regard to standardization as an external name or macro. If such a function is standardized, it will have to be under a different name.



来源:https://stackoverflow.com/questions/10162733/atoi-is-a-standard-function-but-itoa-is-not-why

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