微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何输入 eof(Ctrl+z) 同时让 scanf 在说明符前有一个空格以避免出现以前的 \n 错误?

如何解决如何输入 eof(Ctrl+z) 同时让 scanf 在说明符前有一个空格以避免出现以前的 \n 错误?

enter image description here

(我按回车键)

#include<stdio.h>
#include<stdlib.h>
int main(){
    int target,result;
    while(1){
        printf("Please input your target: ");
        scanf(" %d",&target);
        if(feof(stdin)){
            break;
        }
        printf("test\n");       
    }
}

如果我将代码更改为:(删除空格)

#include<stdio.h>
#include<stdlib.h>
int main(){
    int target,result;
    while(1){
        printf("Please input your target: ");
        scanf("%d",&target);//no space
        if(feof(stdin)){
            break;
        }
        printf("test\n");       
    }
}

成功了

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。