格式化输入输出函数(Formatted input output function)1.1.1 formatted input output functionThe Turbo C2.0 standard library provides two console formats, input and output functions, printf (), andScanf () these two functions can read and write data in a variety of formats on a standard input / output device.The printf () function is used to write data to a standard output device (screen); the scanf () function is used to enter data from a standard inputRead data on device (keyboard). The use of these two functions is described in detail below.1. Printf () functionThe printf () function is a formatted output function that is typically output to standard output devices in a specified formatInformation. This is often used when writing programs. The call format of the printf () function is:Printf (< formatted string >, < parameter table >);The formatted string consists of two parts: one is the normal character, the other is the original characterAnother part is formatting the required characters, beginningwith '%', followed by one or more specified characters,Used to determine the format of the output content.A parameter table is a series of parameters that need to be output, and the number must be the output specified by the formatted stringA number of parameters, among the parameters "," separate and sequential correspondence, otherwise there will be unexpectedErrors that cannot be reached.1. formatting specificationsThe formatting specifications provided by Turbo C2.0 are as follows:Thought of thought - thought of thoughtSymbolic action- - - - - - - - - - - - - - - -%d decimal signed integer%u decimal unsigned integer%f floating point numbers%s string%c single characterThe value of the%p pointerFloating point numbers in the%e exponent%x,%X unsigned integer represented by sixteen decimal%0 unsigned integer represented by octal notationThe%g automatically selects the appropriate representationThought of thought - thought of thoughtExplain:(1) you can insert numbers between "%" and "letters" to represent the maximum field width.For example:%3d means output 3 bit integers, not 3 bit, right justified.%9.2f represents the floating-point number of the output field width of 9, in which the decimal digit is 2 and the integer bit is 6,The decimal point is one, not 9, right justified.%8s stands for a string of 8 characters, not 8 characters, right justified.If the length or integer number of the string exceeds the indicated field width, it will be output at its actual length.But for floating point numbers, if the integer bits exceed the indicated integer bit width, they are output by actual integer;If the decimal portion exceeds the indicated decimal width, the output is output from four to five at the width of the description.In addition, if you want to add some 0 before the output value, you should add a 0 before the width of the event.For example:%04d means that when you output a value less than 4 bits, you will fill 0 in front to make the total widthFor 4.If floating-point numbers are used to represent the character or integer output format, the digits after the decimal point represent the maximum width,The number before the decimal point represents the minimum width.For example,%6.9s means a string that is no longer than 6 and not greater than 9. If greater than 9, thenAfter ninth characters, the contents will be deleted.(2) you can add a lowercase letter L between `% 'and' letter ', which means that the output is long.For example:%ld means output long integers%lf means output double floating point numbers(3) you can control the output left aligned or right aligned,i.e., add a "-" number between "%" and "letter"Indicates that the output is left justified, otherwise right justified.For example:%-7d means output 7 bit integers left justified%-10s stands for output, 10 characters left justified2. some special charactersThought of thought - thought of thoughtCharacter action- - - - - - - - - - - - - - - -\n line feed\f screen and paging\r carriage return\t Tab symbol\xhh means that a ASCII code is represented by 16 inputs,Among them, HH is 1 to 2 16 hexadecimal numbersThought of thought - thought of thoughtThe printf () function learned in this section, and the data types learned in the previous section, prepare the following procedureOrder to deepen understanding of the Turbo C2.0 data type.In 1 cases#include#includeInt, main (){Char, C, s[20], *p;Int, a=1234, *i;Float f=3.141592653589;Double x=0.12345678987654321;P= "How do you do"";Strcpy (s, Hello, Comrade);*i=12;C='\x41';Printf (a=%d\n, a); / * output decimal integer a=1234*/ Printf (a=%6d\n, a); / * the output 6 decimal number a= 1234*/ Printf (a=%06d\n, a); / * the output 6 decimal number a=001234*/Printf ("a=%2d\n", "a"); /*a more than 2 bits, output a=1234*/ at actual valuePrintf (*i=%4d\n, *i); / * output 4 decimal integer *i= 12*/Printf (*i=%-4d\n, *i); / * output left aligned 4 decimal integer *i=12*/Printf (i=%p\n, I); / * i=06E4*/ output.Printf (f=%f\n, f); / * output floating-point numberf=3.141593*/Printf (f=6.4f\n, f); / * output 6 the decimal floating-point number 4F=3.1416*/Printf (x=%lf\n, x); / * output long floating pointx=0.123457*/Printf (x=%18.16lf\n, x); / * output 18 the 16 decimal floating-point longNumber x=0.1234567898765432*/Printf (c=%c\n, c); / * output character c=A*/Printf (c=%x\n, c); / * c=41*/ the output character of the ASCII code valuePrintf (s[]=%s\n, s); / * output string array s[]=Hello, Comrade*/Printf (s[]=%6.9s\n, s); / * s[]=Hello string output of up to 9 characters,Co*/Printf (s=%p\n, s); / * the first character string array output address s=FFBE*/Printf (*p=%s\n, P); / * p=How do you do*/ output string pointerPrintf (p=%p\n, P); / * pointer output value p=0194*/Getch ();Retunr 0;}The address values in the above results may differ on different computers.In example 1., the first statement "#include" means to call another file, stdio.h, thisIs a header file that includes data type definitions and function descriptions for all standard input and output library functions.Turbo C2.0 defines and explains the variables and function types that each library function usesIn the header file "*.h", users must use #include<*.h> or #include "* H" when using these functions"The statement calls the appropriate header file for the connection. If this statement is not used, the connection will go wrongError.Two, scanf () functionThe scanf () function is a formatted input function that reads input information from a standard input device (keyboard).Its call format is:Scanf (< formatted string >, < address table >);A formatted string consists of the following three different characters;1.: format specifier format specifier and printf () function in the same format specifier.2. blank characters: blank character will make scanf (omitted) function in the read operation input in one or moreBlank character.3. non white space characters: a non blank character will make the scanf () function tick off when readThe same character as a blank character.The address table is the address of all variables that need to be read, not the variables themselves. This is associated with the printf () functionIt's quite different. Pay special attention to it. The addresses of each variable are separated from "" "" "" "" "" "".Cases of 2:{Int, I, j;Printf ("I, j=, \n");Scanf ("%d", "%d", "&i", "&j");}In this example, the scanf () function to read an integer, then the next input comma out of theThen read into another integer. If this particular character is not found, the scanf () function terminates. ifThe spaces between arguments are spaces, and one or more spaces must be entered between the arguments.Explain:(1) for strings, arrays, or string pointer variables, because the array name and pointer variable name itselfIs the address, so when you use the scanf () function, you don't need to add the '&' operator in front of them.In 3 cases{Char, *p, str[20];Scanf (%s, P); / * * / from the keyboard input stringScanf ("%s", STR);Printf (%s\n, P); / * * / string output to the screenPrintf ("%s\n", STR);}(2) you can add an integer between the "%" formatting specifications in the formatted stringThe maximum number of bits in any read operation.For example 3, if you can only enter 10 characters for string pointer P, then the first scanf () function statementForScanf ("%10s", P);When the program is running, once the number of characters is greater than 10, the P is no longer read in, while the latter one readsThe entry function, that is, scanf ("%s", "STR"), is read from the Eleventh character.There is a problem with the actual use of the scanf () function, as illustrated below:When multiple scanf () functions are used to continuously enter multiple character variables, for example:(main){Char, C1, c2;Scanf ("%c", &c1);Scanf ("%c", &c2);Printf (C1, is,%c, C2, is,%c, c2\1, C2);}Run the program, enter a character A, enter (to complete the input must return), in the implementation of scanfWhen ("%c", "&c1"), assign "A" to the variable C1, but the carriage return remains in the buffer and executes the input statementWhen scanf ("%c", "&c2"), the variable C2 output is a blank line. If you enter AB and then enter, then the output nodeThe results are: C1, is, A, C2, is, B.To solve the above problem, you can add the clearing function fflush () to the input functionThe method will be described at the end of this section. Modify the above program to become:#include(main){Char, C1, c2;Scanf ("%c", &c1);Fflush (stdin);Scanf ("%c", &c2);Printf (C1, is,%c, C2, is,%c, C1, C2);}1.1.2 non formatted input output functionThe non formatted input output function can be replaced by the Standard formatted input output function described above, butThese functions are less compiled and relatively small in memory, thus increasing the speed and using them at the same timeConvenient. The following are described separately.1. Puts () and gets () functions1. puts () functionThe puts () function is used to write strings to the standard output device (screen) and to wrap them:Puts (s);Where s is a string variable (string, array name, or string pointer).The function of the puts () function is the same as the language printf ("%s\n", "s").Cases of 4:(main){Char s[20], *f; / * string array and pointer variables.Strcpy (s, "Hello Turbo C2.0!"); / * * / string array variable assignmentF= "Thank you"; / * * / string pointer variable assignmentPuts (s);Puts (f);}Explain:(1) the puts () function can only output strings, cannot output values, or format transformations.(2) you can write strings directly to the puts () function. Such as:Puts ("Hello, Turbo, C2.0");2. gets () functionThe gets () function is used to read strings from the standard input device (keyboard) until the carriage returns, but carriage returnsDoes not belong to this string. Its call format is:Gets (s);Where s is a string variable (string, array name, or string pointer).The gets (s) function is similar to scanf ("%s", "&s"), but not exactly the same, using scanf ("%s", "&s")When a function enters a string, there is a problem that if the space is entered, the input string is considered to be over,The character after the space will be processed as the next entry, but the gets () function will accept the entire input characterString until carriage returns.In 5 cases(main){Char, s[20], *f;Printf ("What's, your, name, \n");Gets (s); / * * / wait until enter the end of the input stringPuts (s); / * * / the input string outputPuts ("How, old, are, you,...");Gets (f); Puts (f); }。