1、itoa()函数(整型转字符)
以下是用itoa()函数将整数转换为字符串的一个例子:
# include <stdio.h>
# include <stdlib.h>
void main (void)
{
int num = 100;
char str[25];
itoa(num, str, 10);
printf("The number 'num' is %d and the string 'str' is %s. \n" ,
num, str);
}

itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转移数字时所用 的基数。在上例中,转换基数为10。10:十进制;2:二进制...

2、atoi()函数(字符转整型)
头文件:#include <stdlib.h>

atoi() 函数用来将字符串转换成整数(int),其原型为:
int atoi (const char * str);

【函数说明】atoi() 函数会扫描参数 str 字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过  isspace()  函数来检测),直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。

【返回值】返回转换后的整型数;如果 str 不能转换成 int 或者 str 为空字符串,那么将返回 0。


范例:将字符串a 与字符串b 转换成数字后相加。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main ()
  4. {
  5.     int i;
  6.     char buffer[256];
  7.     printf ("Enter a number: ");
  8.     fgets (buffer, 256, stdin);
  9.     i = atoi (buffer);
  10.     printf ("The value entered is %d.", i);
  11.     system("pause");
  12.     return 0;
  13. }
执行结果:
Enter a number: 233cyuyan
The value entered is 233.