C語言常用內置函數及其注意事項

C語言常用內置函數及其注意事項

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main() {
    //常用內置函數,-0表示假 非0表示真
    printf("%d\n",isupper('a'));//是否爲大寫字母
    printf("%d\n",islower('a'));//是否爲小寫字母
    printf("%d\n",isalpha(97));//是否爲字母
    printf("%d\n",isupper('9'));//是否爲數字,注意傳入的參數是一個字符型,如果爲整形則當做ascii碼處理
    //轉換大小寫字母
    printf("%c\n",toupper('a'));
    /*
        //打印所有ascii碼對應的數字
        for (int i = 0; i < 127; i++) {
            printf("%c,\t",i);
        }
    */
    system("pause");
    return 0;
}

1.判斷 是否爲數字,注意傳入的參數是一個字符型,如果爲整形則當做ascii碼處理

我們寫一個小程序:數字轉中文數字

#define _CRT_SECURE_NO_WARNINGS

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

int main() {
    int money;
    int count = 0;
    char unit[10][4] = {"零","壹","貳","叄","肆","伍","陸","柒","捌","玖"};//定義中文名稱
    int moneys[6];
    printf("請輸入金額:");
    scanf("%d",&money);
    while (money != 0) {
        moneys[count]=money % 10;
        money /= 10;
        count++;
    }
    printf("輸入的數字個數是:%d",count);
    printf("轉中文大寫:\n");
    for (int i = 0; i < count; i++) {
        printf("%d - %s\n",moneys[i],unit[moneys[i]]);
    }
    system("pause");
    return 0;
}

小插曲:scanf函數報錯

在其間遇到一個小插曲,就是使用scanf的時候運行報以下錯誤:

This function or variable may be unsafe.

通過百度發現在scanf的聲明中,在函數的標準形式說明之前,還用到了幾個宏定義,正是因爲這幾個宏定義才實現了scanf函數的禁用。

問題解決

在編譯器給出的錯誤提示中,實際上已經爲我們給出了一個明確的解決方案。我們只需要在程序的開頭添加一個宏定義便能夠解決問題。

#define _CRT_SECURE_NO_WARNINGS

2018年3月26日22:49:05
QQ:201309512

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章