使用ungetc()【#include<stdio.h>】过滤输入

int ungetc(int c, FILE *stream);//把一个字符退回到输入流中

输入参数

c 要写入的字符,stream 文件流指针

输出参数

字符c - 操作成功,EOF - 操作失败



例子程序:将输入的数据中,数字部分打印出来。

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

int main()
{

    char ch;
    int num;
    while ((ch = getchar()) != ' ')
    {
        if (!isdigit(ch))
            continue;
        ungetc(ch, stdin);
        scanf_s("%d", &num);
        printf("%d ", num);
    }

    return 0;
}







LOFTER:hgfalgorithm   http://hgfal.lofter.com/post/28eef2_ec31d4
原文地址:https://www.cnblogs.com/hgfgood/p/4248325.html