ctrl-d appends characters to output

John-Mark Gurney jmg at funkthat.com
Sat Jan 17 19:43:15 UTC 2015


less xss wrote this message on Sat, Jan 17, 2015 at 12:59 -0500:
> I've searched around quite a bit with no luck on this matter. I currently
> have an issue where I send EOF (ctrl-d) to some simple K&R2 exercises and
> the terminal returns the D character appended to my data when EOF is sent.
> I wish to prevent any and all extra characters from being appended and I
> would also like to understand why it's happening. The following code
> is an example exercise from K&R2 that yield said problem.
> 
> #include <stdio.h>
> 
> int main() {
>     double nc;
> 
>     for (nc = 0; getchar() != EOF; ++nc) {
>         ; /* syntactic null statement */
>     }
> 
>     printf("%.0f\n", nc);
> }
> 
> $ ./a.out
> 0D
> $

This problem exists on pretty much any Unix based platform and is not
FreeBSD specific...

The D is not getting appended to your output...  If you changed your
printf to include a space or two before the new line, you wouldn't
see the D... the D is left after the kernel echoed ^D in response to your
ctrl-D.  If you redirect the output to a file and examined the output,
there would be no D in your output...  Or if you provided input from
another source that didn't require an EOF to trigger it, such as:
$echo | ./t
1

-- 
  John-Mark Gurney				Voice: +1 415 225 5579

     "All that I will do, has been done, All that I have, has not."


More information about the freebsd-hackers mailing list