CTRL-D on Mac terminal prints D

I run this example (from Kernighan and Ritchie C, section 1.5.2) on a Mac OS X terminal:

#include <stdio.h>

int main()
{
    int c, nl;

    nl = 0;
    while((c = getchar()) != EOF)
        if(c == '\n')
            ++nl;

    printf("%d\n", nl);
}

I launched the application and immediately entered the EOF symbol CTRL-D. The program displays 0Dand exits. 0 is the expected result, but where does the extra "D" come from?

I saw this thread and this faq , but could not find the answer.

+4
source share
2 answers

I am sure that the terminal shows ^D(standard carriage notation) and leaving the cursor on the carriage, then your program prints 0\nbecause nl- 0, rewriting the carriage.

sleep(5) printf .

+7

, , . Mac stty -echoctl

C...

0

All Articles