Question

I am currently reading K&R's book and typing in the examples from the first section, and there are a couple of examples such as this:

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

I am testing these examples on a Windows box and thus running the compiled exe files from the cmd prompt.

To test the example above, how do I simulate an EOF? That is, basically how can I make the loop stop when testing the example from the command prompt?

Was it helpful?

Solution

To enter an EOF, use:

  1. ^Z (CtrlZ) in Windows
  2. ^D on Unix-like systems

OTHER TIPS

Refer EOF

Windows: Ctrl+Z
Unix :Ctrl+D

First, press: Ctrl^X, next: Ctrl^D

You can also simulate EOF by explicitly giving int variable a value of -1.

Check out this code for more clarity:

#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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top