c语言字符转数字怎么实现

464
2024/2/4 17:59:39
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C语言中,可以使用atoi()函数将字符串转换为整数。示例如下:

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

int main() {
    char str[] = "12345";
    int num = atoi(str);

    printf("The converted number is: %d\n", num);

    return 0;
}

运行结果为:

The converted number is: 12345

此外,还可以使用sscanf()函数来实现字符转数字,示例如下:

#include <stdio.h>

int main() {
    char str[] = "67890";
    int num;
    
    sscanf(str, "%d", &num);

    printf("The converted number is: %d\n", num);

    return 0;
}

运行结果为:

The converted number is: 67890

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

推荐阅读: c语言函数pow怎么用