windows - C - inputting correct code but receiving no output -


When I run the following code and input a sentence, I have not given any output. The cursor just goes into a new line

I copied it directly from the book and double it for the mistakes (first edition of C programming and language) and

 < Code> #include & lt; Stdio.h & gt; Int main () {int c, i, nwhite, nother; Int ndigit [10]; Nwhite = nother = 0; For (i = 0; i <-10; ++ i) ndigit [i] = 0; While ((c = getchar ()) = EOF) if (c> = '0' & amp; lift; = '9') ++ ndigit [c-'0 ']; And if (C == '' || C == '\ n' || C == '\ t') ++ White; Other ++ Nobhere; Printf ("numeral" =); For (i = 0; i <-10; ++ i) printf ("% d", ndigit [i]); Printf (", white space =% d, other =% d \ n", nwhite, nother); Return 0; }  

Since you are testing a program copied from another source, I think you do not want to change it, but understand it

getchar () gets the same 1 character from the standard input, which is <00> standard header & Lt; The stdio is a file named & gt; .

Standard input, stdin is considered to be a file.
Speaking formally, the end of the file is "Icon" and not "Characters". "End of file" icon CRRL-Z (whose ASCII code is 26) in Windows
CTRL-D (whose ASCII code is 4) in Linux.

On the other hand, standard input typically has the following behavior:

  • Wait for the user to enter until an identity / record key is pressed.

If the user does not enter, the standard input does not return control to the program. This is also when you enter the "end-of-file" character (say, CTRL-Z).
However, other behaviors are possible. For example, in the Ubuntu console I think CTRL-D is recognized without waiting for the Enter key to be pressed.

In any case, you must explicitly type the end-of-the-file symbol in your system's console .
So, CTRL-Z (maybe followed and followed) or CTRL-D should be pressed for itself

About Enter and EOF

After pressing, mark your program test for "EOF ." However, enter the keyword "end-of-file" sign does not print, but only "End-of-Line", which matches the standard character Newline \ '\ n' .

Thus, if it is desired that while () If the sum ends after compressing / registering, then the test should be against "code \" and \ EOF .

A supervision

It can be seen that the getchar () character does not retrieve CTRL-Z because the ASCII code for CTRL-Z is 26 , But receives getchar () negative value (in general -1 ). This means that getchar () character ASCII 26 Recognizes as the End and the File Icon Land, and then the macro EOF converts a value with meaning went provided by C, which is 26.

What do I mean is that EOF is not CTRL-Z, and then there is a belief that under the assumption of ASCII 26 (CTR-Z), innocent EOF < / Code> can not send. Will be sent to a text file

In short, I think it is important to understand the abstract concept of "end-of-file", the role of " EOF and" mark "and a character".
(Another example: "In Windows" "for the end-of-line" are some letters of the letter "Mark", which is CTRL-M CTRL-J, which is not just 1 character, but 2).

Quoted from the standard:

The getchar function returns the next character from the input stream indicated by stdin. If the stream is at the end of the file, the end-pointer indicator of the file is set for the stream and the receiver returns the EOF. If there is a reading error, the error indicator is set for the stream and returns the return eof.


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -