溫馨提示×

c語言字符轉數字怎么實現

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

在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

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女