53

I have a C program.

int main ()
{
    if (getchar()!=EOF)
        puts("Got a character");
    else
        puts("EOF");
}

What should I type into the stdin on the terminal to produce an EOF?

4
  • 1
    try putchar('%') or printf("%%")
    – pmg
    Commented Aug 15, 2012 at 11:36
  • rather what should I give from keyboard to get control in else part ? Commented Aug 15, 2012 at 11:38
  • 1
    You never assign the character you read to c. Commented Aug 15, 2012 at 11:43
  • 4
    @user1593308, Have you tried pressing CTRL-D? Pl. check to see if this takes the code to the else part. Commented Aug 15, 2012 at 11:44

4 Answers 4

88

In Windows, Control+Z is the typical keyboard shortcut to mean "end of file", in Linux and Unix it's typically Control+D.

4
  • 1
    Sir I want to print the integer value of EOF in C, how can I do this ? Commented Aug 15, 2012 at 11:49
  • the integer value of EOF in C is a value<0, so set your variable equal to getchar() and then print it out upon reading EOF
    – user2494770
    Commented Oct 16, 2013 at 1:08
  • Unhelpfully, this is the first Google hit for "end of file keyboard shortcut". Commented Mar 31, 2020 at 13:02
  • 1
    @user1593308 Just do printf("EOF=%d\n", EOF);, no need to get the value from getchar(), just use the macro directly.
    – unwind
    Commented Mar 31, 2020 at 13:31
17
  1. EOF is wrapped in a macro for a reason - you never need to know the value.
  2. From the command-line, when you are running your program you can send EOF to the program with Ctrl-D (Unix) or CTRL-Z (Microsoft).
  3. To determine what the value of EOF is on your platform you can always just print it:

    printf ("%i\n", EOF);
    
4
  • 4
    printf("%i", EOF) prints -1 instead of Control+D
    – KMC
    Commented Apr 5, 2013 at 9:54
  • 5
    @KMC - allow me to clarify. Doing this: printf ("%i\n", 'K'); would print '75' and not 'K' after all. I also doubt if printing EOF as a character with "%c" will result in "CTRL+D" being printed. Depending on the terminal you might even get "?". Bear in mind that EOF is what the library returns when you read input, and is not representable as a printable character anyway. When you are writing output in your terminal, pressing CTRL+D (or whatever) causes the terminal to end input; it does not cause the terminal to generate a specific representation of EOF. Hope this clears things up.
    – Lelanthran
    Commented Apr 6, 2013 at 10:51
  • ubuntu 18. doesnt work. gives an error "bash: syntax error near unexpected token `"%i\n",'"
    – Alex
    Commented Oct 5, 2023 at 5:08
  • @Alex This isn't a bash question, so the answer is not a snippet of bash. It's a C question, hence the answer is a snippet of C code. IOW, type it into your C program, not your bash program.
    – Lelanthran
    Commented Jan 27 at 19:38
7

You can simulate an EOF with:

  • Windows: ctrl+Z
  • Unix: ctrl+D
0
4

It's not mentioned in any of the other answers so far, but you may need to press the right key combo (^D or ^Z) 2 or 3 times in order to actually signal EOF; see here for explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.