溫馨提示×

c語言字符轉(zhuǎn)數(shù)字怎么實(shí)現(xiàn)

小億
94
2024-02-23 17:59:39
欄目: 編程語言

在C語言中,可以使用atoi()函數(shù)將字符串轉(zhuǎn)換為整數(shù)。示例如下:

#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;
}

運(yùn)行結(jié)果為:

The converted number is: 12345

此外,還可以使用sscanf()函數(shù)來實(shí)現(xiàn)字符轉(zhuǎn)數(shù)字,示例如下:

#include <stdio.h>

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

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

    return 0;
}

運(yùn)行結(jié)果為:

The converted number is: 67890

0