Search code examples
ceof

How to end a c program with Ctrl+D in windows?


SO in linux i could probably end a while-loop with this kind of code if I compile with gcc:

#include <stdio.h>

int main()
{   
    int s;
    while(scanf("d%",&s)!=EOF);
    {   
        scanf("%d",&s);
          }
    return 0;
}

However this does not work with a windows computer and the compiler Im using is Microsoft Visual Studio 12. Any suggestions?


Solution

  • #include <stdio.h>
    
    int main() {
        int s;
        while((s = getchar()) != EOF) {
            printf("%d\n", s);
        }
        printf("%d - at EOF\n", s);
    }
    

    you can try this