-3
#include <stdio.h>
int main() {
int c;
while ((c = getchar()) != EOF) {
    putchar(c);
    }
}

This works fine, when EOF ( ctrl + z) is in a new line, but when the input is :
blabla^z
it does not work. When i debug the program it tells me that the input "^z"(^z = EOF) is saved as 26,
but when the input ^z is in a new line it is saved as -1. Why?
in case something is unclear:
it is saved in the variable c, and does not work means it doesnt terminate the while loop, only when the input ^z is put in a new line it tearminates the loop
im using windows

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
sebastian
  • 13
  • 5

1 Answers1

0

Maybe you're using it wrong, and the CTRL+z symbol is not send to the getchar() function (which would give an EOF) but to the system that runs you program (e.g. it will "Stopped" the program).

Try

#include <stdio.h>

int main() {
int c;
while ((c = getchar()) != EOF) {
// while ((c = getchar()) != '.') {
    putchar(c);
    }
    putchar('W');
}

Do you see the ouput 'W' in any case ? (especially when using a new line ?) in my case, I get "Stopped" (on UNix system, CTRL+Z is used to suspend a process).

On Windows system, the answer has already been given on SO:

At the end of a line, hitting ^z (or ^d on linux) does not cause the terminal driver to send EOF. It only makes it flush the buffer to your process (with no \n).

Hitting ^z (or ^d on linux) at the start of a line is interpreted by the terminal as "I want to signal EOF".

Community
  • 1
  • 1
cyrobin
  • 146
  • 1
  • 11