main.c:13:13: warning: implicit declaration of function 'itoa' is invalid in C99 [-Wimplicit-function-declaration] itoa(n, pr, 10); ^ 1 warning generated. Undefined symbols for architecture x86_64: "_itoa", referenced from: _main in main-2e9155.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
正道
后面找到用sprintf
1 2 3 4 5 6 7 8 9 10
#include<stdio.h> #include<stdlib.h>
voidmain(void) { int num = 100; char str[25]; sprintf(str, " %d" , num); printf ("The number 'num' is %d and the string 'str' is %s. \n" ,num, str); }
补充
不过遇到比较长的整型时候, 发现疑似溢出的情况(像这个负号)
1 2
3362695274// 输入 - 932272022// 输出(每个数字中间加了空格))
其实是整型应该从int换成long, 然后就正常了
1 2 3 4 5 6 7 8
intmain() { char str[10]; long x = 1234567890L; sprintf(str,"%ld",x); printf("%s",str); return0; }