1
#include <stdio.h>

main()
{
    long nc;
    nc = 0;
    
    while(getchar() != EOF)
        ++nc;
    printf("%ld\n", nc);
}

When I run this program and write a string for ex 'helloworld' and press Ctrl + D. It prints my string again - 'helloworldhelloworld'.

Then I print Ctrl + D again and it shows:

helloworldhelloworld^D
Count: 10

enter image description here

Why does Ctrl + D not print count straightaway? Using Visual Studio Code on Mac OS.

EDIT: I posted the wrong code. Really sorry about that.

#include <stdio.h>

main()
{
    int c;
    int counter_a = 0;
    
    c = getchar();
    while(c != EOF)
    {
        putchar(c);
        c = getchar();
        counter_a = counter_a + 1;
    }

    printf("\nCount: \t%d\n", counter_a);
}

This is the code I was running. So my query is solved.

ailhahc
  • 57
  • 1
  • 8
  • This program should not print any string. Delete the executable and then recompile and make sure that you are running the correct program. – alani Jul 02 '20 at 04:45
  • 2
    Please be sure to tag the programming language you're using. People like myself ignore unrelated tags so we can help the people with the things we *do know*. ;-) – John Jul 02 '20 at 04:46
  • It's working fine and Ctrl-d will stop taking further input stream. Please refer to [FAQ](http://www.c-faq.com/stdio/index.html) on C. – Ashish Karn Jul 02 '20 at 04:55

2 Answers2

0

Control D isn't an EOF character. Instead it forces the console input buffer logic to make whatever characters have been typed thus far available to the stdio library. If an read request issued by the stdio library yields any data, the library will expect that there will be more afterward. If a read request yields no data, however, the stdio library will assume that was because it reached the end of a file.

I'm not sure exactly why the string is getting echoed, but the console input buffering logic sometimes echos characters as they were typed, and in some conditions will echo back a partially-completed line.

Rather a hokey scheme in my opinion, but that's how Unix works.

supercat
  • 77,689
  • 9
  • 166
  • 211
  • 2
    nothing is an eof character, because EOF is not a character, it's a condition on a filehandle. https://stackoverflow.com/questions/12389518/representing-eof-in-c-code/12389581 – erik258 Jul 02 '20 at 05:33
  • @DanielFarrell: Some operating systems such as CP/M or MS-DOS do have end-of-file characters. – supercat Jul 02 '20 at 14:47
0

When you're entering input into a program at the Posix command line, and want to end the input, enter Ctrl-D on an otherwise empty line.

Your terminal is in cooked mode, essentially a line editor for each command. Your program won't receive input until the line is "entered" with the enter/return key. If the line you're editing is not empty, Ctrl-D will cause it to be sent to the terminal input's destination (your program) without the newline that acompanies the enter/return key.

With an empty line buffer, Ctrl-D is how the operator informs the shell to apply the end-of-file condition to the input stream attached to the keyboard.

Consider this example with cat, a program which in this invocation writes its input stream to its output stream, not unlike the question's program.

$ cat
Usually, I can edit a line - using backspace to edit the input before I enter it.  Now I will press enter
Usually, I can edit a line - using backspace to edit the input before I enter it.  Now I will press enter
Mid-line, I can press Ctrl-D to send the line.  I'll press it now: Mid-line, I can press Ctrl-D to send the line.  I'll press it now:

I pressed enter after that was output, rendering both the newline I typed, echoed to the terminal, and the newline cat received.
I pressed enter after that was output, rendering both the newline I typed, echoed to the terminal, and the newline cat received.
But, at an empty line buffer, Ctrl-D ends input altoghether.  I'll press return, and then Ctrl-D.
But, at an empty line buffer, Ctrl-D ends input altoghether.  I'll press return, and then Ctrl-D.

When I press enter/return, the line I've been editing is sent to the terminal's destination (cat) - including the newline. When I press Ctrl-D mid-line, the line I've been editing is sent to the destination without the newline. Ctrl-D on an empty line (you may have typed, then deleted, but now the line is empty) tells the input reader that there's no more data, just like when you read from a file when your position is at the end already.

erik258
  • 14,701
  • 2
  • 25
  • 31