|
Having at his disposal only function getchar and putchar you can, not knowing anything more about the operations of the I / O write a surprising number of useful programs. The simplest example is the handwriting program in the file copying introductory vyvodnoy. The general pattern is displayed: Enter a symbol while (symbol is not a sign of the end of the file) withdraw just read character introduce a new symbol The program, written in the language "C", is as follows: main () / * copy input to output; 1st version * / (int c; c = getchar () while (c! = EOF) (putchar (c), c = getchar ();)) operator relationships! = means "not equal". The main problem is to fix the end of the file input. Usually, when the function getchar faces at the end of the input file, it returns a value, not a valid symbol, so that the program can establish that the input file is exhausted. The only complication, which is a significant inconvenience, is the existence of two common agreements about what actually is a sign of the importance the end of the file. We otsrochim resolution of this issue by using the symbolic name EOF this value, whatever it might be. In practice, EOF will either -1 or 0, so in order to work in the program must have a proper
# define EOF -1 or # define EOF 0 EOF using symbolic constants for submission to the value returned function getchar when leaving at the end of the file, we ensured that only one value in the program depends on a specific numerical value. We also described the variable 'c' as int, not char, in order to keep the value returned getchar. as we shall see in Chapter 2, this is really int, as it should be in a position, in addition to all possible symbols to represent and EOF. Programmer with experience working on the "C" program would be copy written more concisely. In the language "C" any assignment, such as c = getchar () can be used in the expression, its value - just the importance attributed to the left part. When assigning a symbol variable 'c' place inside part of the operator while verification, the program file copy written in the form: main () / * copy input to output; 2nd version * / (int c; while ((c = getchar ())! = EOF) putchar (c);) The program extracts the character that it assigns the variable 'c' and then checks whether this character is a sign of the end of the file. If not - is performed while the body of the operator, vyvodyaschee character. Then, while the cycle is repeated. when, finally, end-of-file is reached input, while the operator is completed, and with it ends the execution and main features. This version mainstreamed input - the only one recourse to function getchar - and uzhimaetsya program. Embedding assignment in a verifiable condition - it is one of those places language "C", which leads to a significant reduction programmes. However, that route can be fascinated and begin writing inaccessible to the understanding of the program. This trend, we will try to discourage. It is important to understand that the parentheses around the assignment in the conditional expression really necessary. Precedence operation! = Greater than = assignment operation, which means that in the absence of brackets testing conditions! = Assignment will be done before =. Thus, the operator c = getchar ()! = EOF equivalent to the operator
|