Note that there are some explanatory texts on larger screens.

plurals
  1. POEOF behavior when accompanied by other values
    text
    copied!<p>*Note: I'm using windows, so EOF is ctrl + Z for me.</p> <p>For a while I've noticed an EOF input seems to behave differently in isolation than it does when accompanied by other input. For example, <code>^Z</code> (the EOF command for windows in command prompt) and <code>a^Z</code> seem to cause different behavior in the following code:</p> <pre><code>#include &lt;stdio.h&gt; #define MAX 1000 int getline(char s[]); main() { int line; char arr[MAX]; while( (line = getline(arr)) != EOF) printf("%s",arr); system("Pause"); return 0; } int getline(char s[]) { int c, i = 0; while ((c = getchar()) != EOF &amp;&amp; c != '\n') { s[i++] = c; } if (c == '\n') { s[i++] = c; } else return EOF; s[i] = '\0'; return 1; } </code></pre> <p>If I input <code>^Z</code> + enter in the command prompt, the program predictably jumps to <code>system("Pause");</code> However, if I input <code>abc^Z</code> + enter, nothing happens, as though EOF was ignored and a <code>'\n'</code> command was never received. If, at this point, I press enter again, it shows the following:</p> <p><img src="https://i.stack.imgur.com/oXQCw.png" alt="EOF weirdness"></p> <p>I've been tinkering with and debugging this code and small variations of it for over an hour now and can't seem to find anything wrong with it. In theory, if I input <code>abc^Z</code> + enter, I expect the input to be interpreted as <code>abcEOF\n</code>, which would give:</p> <pre><code>s[0] = 'a' s[1] = 'b' s[2] = 'c' i = 3 when loop breaks from c = EOF if (c == '\n') skipped since c = EOF leads to else -&gt; return EOF in main(), line = EOF since that is what the function getline returned while loop breaks because of above system("Pause"); follows </code></pre> <p>Is there something wrong with my code that I'm overlooking or is there some quirk to EOF or command prompt that I should be aware of? I'm almost certain this isn't the only instance where mixing <code>^Z</code> with other values caused unintended behavior.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload