c语言中限制用户输入整数

 

#include <stdio.h>

long get_long(void);

int main(void)
{
    
    long num;
    
    printf("please input an num: ");
    num = get_long();
    
    printf("num: %ld.\n", num);
    
    return 0;
}

long get_long(void)
{
    long input;
    char ch;
    while(scanf("%d", &input) != 1)
    {
        while((ch = getchar())!= '\n')
            putchar(ch);
        printf(" is not num.\n");
        printf("please input num: ");
    }
    
    return input;
}

 

posted @ 2021-08-26 01:26  小鲨鱼2018  阅读(626)  评论(0编辑  收藏  举报