且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C中将int转换为字符串?

更新时间:2022-04-02 01:58:49

编辑:正如注释中指出的那样,itoa()不是标准,因此***使用sprintf()方法.竞争的答案!

As pointed out in the comment, itoa() is not a standard, so better use sprintf() approach suggested in the rivaling answer!

您可以使用itoa()函数将您的整数值转换为字符串.

You can use itoa() function to convert your integer value to a string.

这里是一个例子:

int num = 321;
char snum[5];

// convert 123 to string [buf]
itoa(num, snum, 10);

// print our string
printf("%s\n", snum);


如果要将结构输出到文件中,则无需事先转换任何值.您可以只使用 printf格式规范来指示如何输出值和使用 printf系列中的任何运算符来输出数据.


If you want to output your structure into a file there is no need to convert any value beforehand. You can just use the printf format specification to indicate how to output your values and use any of the operators from printf family to output your data.