'Signal EOF in mac osx terminal

I am stumped by the 1.5.2 question in K&R. I googled for sometime and found out that I have to supply the EOF input after entering the characters.

long nc = 0;

while (getchar() != EOF)
    ++nc;
printf("%ld\n", nc);

return 0;

I tried both command-D and control-D as EOF inputs but nothing worked. Any idea how to supply the EOF for Mac OS X?



Solution 1:[1]

If you want to see what EOF is set as in your terminal, you can type

stty all

on my mac, this gives the output -

speed 9600 baud; 24 rows; 80 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
    -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
-extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
-ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
-dtrflow -mdmbuf
discard dsusp   eof     eol     eol2    erase   intr    kill    lnext   
^O      ^Y      ^D      <undef> <undef> ^?      ^C      ^U      ^V      
min     quit    reprint start   status  stop    susp    time    werase  
 1       ^\      ^R      ^Q      ^T      ^S      ^Z      0       ^W     

You can see four lines up from the bottom, three cells in eof is ^D.

There's a fuller description here which is where I found the information.

Solution 2:[2]

I just figured it out. You have to press Ctrl+D+D. Hold the control down and press D twice. I have to say what a weird key sequence.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dan
Solution 2 vahid abdi