我想找到一种方法将字符串中的数字转换为整数。我可以使用isnum函数在字符串中找到数字,但问题是如何使它成为整数。
我认为你需要的是atoi
功能。
这是来自cplusplus.com的代码示例:
/* atoi example */
#include <stdio.h> /* printf, fgets */
#include <stdlib.h> /* atoi */
int main ()
{
int i;
char buffer[256];
printf ("Enter a number: ");
fgets (buffer, 256, stdin);
i = atoi (buffer);
printf ("The value entered is %d. Its double is %d.\n",i,i*2);
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句