温馨提示×

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

小亿
83
2024-04-12 11:43:04
栏目: 编程语言

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

int atoi(const char *str);

示例如下:

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

int main() {
    char str[] = "12345";
    int num = atoi(str);
    
    printf("String: %s\n", str);
    printf("Number: %d\n", num);
    
    return 0;
}

运行结果将输出:

String: 12345
Number: 12345

注意:atoi()函数只能将合法的整数字符串转化为整数,如果字符串不符合整数格式,转化结果会不可预料。

0