Converting integer to string in C without sprintf

后端 未结 5 2162
一向
一向 2020-12-17 03:56

It is possible to convert integer to string in C without sprintf?

5条回答
  •  旧时难觅i
    2020-12-17 04:29

    You can use itoa where available. If it is not available on your platform, the following implementation may be of interest:

    https://web.archive.org/web/20130722203238/https://www.student.cs.uwaterloo.ca/~cs350/common/os161-src-html/atoi_8c-source.html

    Usage:

    char *numberAsString = itoa(integerValue); 
    

    UPDATE

    Based on the R..'s comments, it may be worth modifying an existing itoa implementation to accept a result buffer from the caller, rather than having itoa allocate and return a buffer.

    Such an implementation should accept both a buffer and the length of the buffer, taking care not to write past the end of the caller-provided buffer.

提交回复
热议问题