如何检查字符是否是整数并输出其整数值

思路分析:int isdigit(char c)函数用来判断字符c是否为数字,当c为数字0~9时,返回非零值,否则返回零。使用时需要包含头文件<ctype.h>。如果一个字符为数字,那输出它减去48之后的整数形式,即为这个数的值。

代码如下:

#include "stdafx.h"
#include <stdio.h>
#include <ctype.h>
int main()
{
    int c;
    while ((c = getchar()) != EOF)
    {
        getchar();
        if (isdigit(c))
            printf("是:%d\n", c-48);
        else
            printf("不是\n");
    }
    getchar();
    return 0;
}

    效果如图:

posted @ 2014-03-25 11:41  源子陌  Views(430)  Comments(0Edit  收藏  举报