Advertisement

转为数字

阅读量:

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()函数仅接受一个参数,即需要被转换为数值形式的字符串。该函数执行完毕后,返回值即为所转换得到的整数结果。

以下列举了可用于将字符串转换为数字的多种函数:
------------------------------------------------------------------------
函数名 作 用
------------------------------------------------------------------------
atof() 将字符串转化为双精度浮点型数值

全部评论 (0)

还没有任何评论哟~