如何一个int转换为字符串用C [英] How to convert an int to string in C

查看:106
本文介绍了如何一个int转换为字符串用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何转换为字符串的 INT (整数)?我试图做一个结构的数据转换成一个字符串,它保存在一个文件中的函数。

How do you convert an int (integer) to a string? I'm trying to make a function that converts the data of a struct into a string to save it in a file.

推荐答案

编辑:正如评论指出的那样, 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.

这篇关于如何一个int转换为字符串用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆