C语言提供了几个标准库函数,可以将字符串转换为任意类型(整型、长整型、浮点型等)的数字。以下是用atoi()函数将字符串转换为整数的一个例子:
# include <stdio. h>
# include <stdlib. h>
void main (void) ; void main (void) {
int num; char * str = "100"; num = atoi(str); printf("The string 'str' is %s and the number 'num' is %d. \n", str, num); } atoi()函数只有一个参数,即要转换为数字的字符串。atoi()函数的返回值就是转换所得的整型值。