3

Simple c program to accept and print the character.

int c;
while((c=getchar())!=EOF)
{
  putchar(c);
}

I am not getting why it accept input when I press Ctrl+Z at the end of line ex Hello(press Ctrl+Z) hello (some symbol) but it work properly after leaving a line then pressing Ctrl+Z. And I am using Window 7

Ouput

Flicks Gorger
  • 195
  • 1
  • 1
  • 9
  • 1
    Are you running this on Windows or some flavour of Unix/Linux? On my Mac and most Unixes, Ctrl-Z suspends the process. – JeremyP Jul 14 '15 at 13:07
  • 1
    What generates EOF is platform dependent. If you are on *nix systems, use `CTRL+D`. `CTRL+Z` works on Windows. – P.P Jul 14 '15 at 13:07
  • 1
    AFAIK, on Windows you also have to hit enter along with EOF (CTRL + Z). – edmz Jul 14 '15 at 13:07
  • 1
    This part "*... at the end of line ex Hello(press Ctrl+Z) hello (some symbol) but ...*" of your question is not completely clear to me. Please be more clear/precise with you want to express. – alk Jul 14 '15 at 13:12
  • It is unclear what you are asking, please clearify you question. – Andreas DM Jul 14 '15 at 13:25
  • Could you please format carefully your input? Do it similary to how you formatted the source code (that is, separate with an empty line and indent with 4 spaces), so we could understand precisely what characters you typed as your program input. Don't forget to mark newlines. – CiaPan Jul 14 '15 at 13:27

5 Answers5

3

When you call getchar() it in turn ends up making a read() system call. The read() will block until it has some characters available. The terminal driver only makes characters available when you press the return key or the key signifying end of input. At least that is what it looks like. In reality, it's more involved.

On the assumption that by ctrl-Z you mean whatever keystroke combination means "end of input", the reason is the way that the read() system call works. The ctrl-D (it's ctrl-D on most Unixes) character doesn't mean "end of input", it means "send the current pending input to the application".

When you've typed something in before pressing ctrl-D, that input gets sent to the application which is probably blocked on the read() system call. read() fills a buffer with the input and returns the number of bytes it put in the buffer.

When you press ctrl-D without any input pending (i.e. the last thing you didwas hit return or ctrl-D, the same thing happens but there are no characters, so read() returns 0. read() returning 0 is the convention for end of input. When getchar() sees this, it returns EOF to the calling program.

This answer in Stack Exchange puts it a bit more clearly

https://unix.stackexchange.com/a/177662/6555

Community
  • 1
  • 1
JeremyP
  • 84,577
  • 15
  • 123
  • 161
1

You have not said what system you are working on, [U|Li]nix or Windows. This answer is Windows specific. For [Li|U]nix, replace references to ctrl-z with ctrl-d.

While using a terminal, Ctrl-z will not produce an EOF (-1) (see good answers from Haccks & JeremyP for detailed whys), so the loop will not exit the way you have it written. However, you can put a test for ctrl-z in your while loop condition to exit...

int main ()
{
    int c=0;

    puts ("Enter text. ctrl-z to exit:");

    while(c != 26) //(26 is the ASCII value for ctrl-z)
    {
        putchar(c);
        c = getchar();
    }

    return 0;
}

By the way, here is a table showing the values for ASCII control characters.

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • 2
    This doesn't answer the question. – haccks Jul 14 '15 at 13:04
  • 4
    "*Ctrl-z is not == EOF*": On windows it is. – alk Jul 14 '15 at 13:07
  • @haccks - What else would you suggest I say? – ryyker Jul 14 '15 at 13:07
  • @ryyker; I am surprised that what OP is saying is happening to my test program too. Looking for the valid reason. – haccks Jul 14 '15 at 13:08
  • @alk - Not always. On Windows, in a command prompt it is not. (Nor is it in Linux). OP did not specify which system. I addressed question from perspective of OPs given information - which includes using a command prompt, and using Windows command prompt. – ryyker Jul 14 '15 at 17:08
0

I found the answer on wiki:

In Microsoft's DOS and Windows (and in CP/M and many DEC operating systems), reading from the terminal will never produce an EOF. Instead, programs recognize that the source is a terminal (or other "character device") and interpret a given reserved character or sequence as an end-of-file indicator; most commonly this is an ASCII Control-Z, code 26.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    Hmmm: Disagree. Compiled `int main(void) { int c; do { c = getchar(); printf("%d\n", c); fflush(stdout); } while (c != EOF); return 0; }` with GCC version 4.9.2, ran in a Windows 'cmd' shell, typed Crtrl-Z and the output was `-1` and program completion. – chux - Reinstate Monica Jul 14 '15 at 16:55
  • Now give input as: `abcdCtrl+Z` and tell me what happened. – haccks Jul 14 '15 at 17:30
  • 2
    `abcdCtrl+Z` (with no enter) --> `-1` and program completion, no 97,98,99,100. Just the `EOF`. – chux - Reinstate Monica Jul 14 '15 at 17:37
  • On my system It prints `97\n98\n99\n100\n26` and program waiting for next input. – haccks Jul 14 '15 at 17:45
  • Perhaps it depends on the compiler and not the OS? – chux - Reinstate Monica Jul 14 '15 at 17:52
  • I know this is old, but I ran into a similar (possibly related) phenomena that may explain. In the environment I use, I can explicitly choose whether the build targets a `console` application. The _console_ behavior appears to change based on this setting. In particular, the behavior of `stdin` and `stdout` are modified based on that setting. For example, as a console application, I can capture any `printf()` output by piping it through `stdout` to the parent process. If I build without specifying _console_, `printf()` calls are not captured. This may explain different ctrl-Z behavior. – ryyker Aug 23 '17 at 14:43
-1
 #include <stdio.h>
 int main()
  {  
     int  c;
     while((c=getchar())!=26)
      {
           putchar(c);
      }
  }

You can use ASCII value of CTRL-Z.Now it won't take input after pressing CTRL-Z.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
-1

getchar() fution read single character by Pressing Ctrl+Z sends the TSTP signal to your process, means terminate the process (unix/linux)

raj yadav
  • 115
  • 1
  • 14
  • Stopping a process is different from ending/terminating a process. A stopped process may be continued again, a terminated process may not. For your reference: http://man7.org/linux/man-pages/man7/signal.7.html – alk Jul 14 '15 at 14:01