c语言中怎么将字符数字转化为数字

706
2023/10/13 19:34:05
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C语言中,可以使用库函数atoi()将字符数字转化为数字。该函数的原型如下:

int atoi(const char *str);

其中,str是一个指向以null结尾的字符串的指针,该字符串表示要转换的数字。该函数返回转换后的整数值。

下面是一个例子:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char num_char[] = "12345";
    int num_int = atoi(num_char);
    printf("Converted number: %d\n", num_int);
    return 0;
}

输出:

Converted number: 12345

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: C语言怎么打印变量地址