我目前正在阅读 K&R 的书并输入第一部分中的示例,有几个示例如下:

while((c = getchar()) != EOF) {
    //do something
}

我正在 Windows 机器上测试这些示例,从而运行编译后的 exe 文件 cmd 迅速的。

为了测试上面的例子,我如何模拟 EOF?也就是说,基本上在从命令提示符测试示例时如何使循环停止?

有帮助吗?

解决方案

要输入 EOF,请使用:

  1. ^Z (控制键Z) 在 Windows 中
  2. 类 Unix 系统上的 ^D

其他提示

参见 EOF

Windows: Ctrl+Z
Unix :Ctrl+D

首先,按键:Ctrl ^ X,下一个键:Ctrl ^ d

您也可以通过显式给出-1 int变量的值模拟EOF。

查核此代码为更清晰:

#include<stdio.h>

int main() {    
    // char ch=getchar()
    // int ch=-1;

    if(ch==EOF) { printf("\nEOF: %d",EOF); }
    if((ch!=EOF)==0) { printf("\nit is equal to 0"); }
    if((ch!=EOF)==1) { printf("\nit is equal to 1"); }
    else { printf("\n it is equal to other value"); }
    system("pause");
    return 0;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top