Printf () function
1. parameter transfer
The mechanism for passing parameters varies with implementation. The following describes how parameters are transmitted in the system. The function call is as follows:
Printf ("% ld", n1, n2, n3, n4); // set n1 to float type n2 to double type n3, n4 to long type
This call tells the computer to pass the values of the variables n1, n2, n3, and n4 to the computer. The computer places them in a memory area called a battle (stack) for implementation. The computer places these values in the stack based on the variable type rather than the conversion specifier (% ld. Therefore, n1 occupies 8 bytes in the stack (float is converted to double ). Similarly, n2 occupies 8 bytes, while n3 and n4 occupy 4 bytes respectively. Transfer the subsequent control to the printf () function.
This function returns all values from the stack, but reads the value based on the Conversion instruction. The % ld specifier indicates that printf () should read four bytes, so printf () reads the first four bytes in the stack as its first value. This is the first half of n1. It is interpreted as a long integer ). The next % ld specifier reads four bytes. This is the first half of n1, which is interpreted as the second long integer ). Similarly, the third and fourth instances of % ld read the first half and the second half of n2 and are interpreted as two long integers ). Therefore, although both n3 and n4 are correct, printf () still reads the wrong byte.
2. Return Value of printf ()
Returns the number of printed characters. If an error is returned, printf () returns a negative number (some old versions of printf () have different return values ).
Example:
# Include
Main () {int a = 100; float B = 123.255; printf ("a = % d", a); printf ("a = d", ); printf ("a = %-10d", a); printf ("a = % + d", a); printf ("a = % d", ); printf ("a = % # o", a); printf ("a = % # x", a); printf ("B = % # f", B );} /* running result a = 100a = 100a = 100a = + 100a = 100a = 0144a = 0x64b = 123.254997 (????) */
Minimum output width: A decimal integer is used to represent the minimum number of output digits. (At least output so many digits !) If the actual number of digits is greater than the defined width, the output is based on the actual number of digits. If the actual number of digits is less than the defined width, the right alignment is displayed and the left side is left blank. There is a negative number, left alignment, left blank indicates that the number of width starts with 0, right alignment, left blank.
# Include <stdio. h> main () {int a = 3456; printf ("a = % 3d", a); // if the actual number of digits is greater than the defined width: printf ("a = d", a) is output according to the actual number of digits; // if the actual number of digits is less than the defined width, the right is aligned, leave printf ("a = %-10d", a) blank on the left; // if the actual number of digits is less than the defined width: there is a negative number, left alignment, printf ("a = 0d", a) is left blank on the right. // if the actual number of digits is less than the defined width, the right alignment is displayed, leave printf ("a = %-010d", a) blank on the left; // left alignment, 0 meaningless .} /* Running result: a = 3456a = 3456a = 3456a = 0000003456a = 3456 */
Scanf () function
This function can read a variety of data. It converts the input string to a string of various forms: integer, floating point, character, and C.
1. Use the scanf () function
The scanf () function uses spaces (line breaks, tabs, and spaces) to determine how to divide the input into several fields. He matches the conversion description with the field at one time and skips the spaces between them.
Example:
int age;int assets;scanf("%d %f",&age,&assets);
We can input data in two rows. Similarly, you can input data in one or five lines. You only need to enter at least one line break, space, or Tab character between each input item. The only exception is % c. It reads that character even if the next character is a blank character.
2. From the scanf () perspective
We are carefully studying how scanf reads the input. Suppose you use a % d specifier to read an integer. The scanf () function starts to read an input character each time. It skips blank characters (spaces, tabs, and line breaks) until a non-blank character is encountered. Because he tries to read an integer, scanf () expects to find a numeric character or a symbol (+ or -).
If you encounter a non-numeric character, he will conclude that the character has been read to the end of the integer. Scanf () puts this non-numeric character back in the input buffer. This means that when the program starts to read the input next time, it will start with the non-numeric character that was discarded from the front. Finally, scanf () calculates the corresponding array of numbers read and places the value in the specified variable.
What will happen if the first non-blank character is not a number? For example, is it A rather than A number? At this time, scanf () will stop there and put A (or whatever) back into the input. No value is assigned to the specified variable. When the program reads the input next time, it starts again at location. If the program only has the % d specifier, scanf () will never read the next one over that. In addition, if you use a scanf () statement with multiple specifiers, ansi c requires the function to stop reading input in the first error.
If % s specifier is used, all characters other than blank characters (space, carriage return, and Tab) are acceptable, so scanf () the white space character is skipped until the first non-white space character is encountered, and all non-white space characters before the white space character is saved again. This means that % s is a scanf () string that reads a word, that is, a string that does not contain spaces. If the field width is used, scanf () stops at the end of the field or the first blank character. Scanf () cannot read more than one word with one % s specifier by field width. Last point: when scanf () places the string in a specified array, it adds the terminated ''To make the array content a c string.
If % c is used, all input characters are equal. If the next input character is a space or line break, the space or line break is assigned to the specified age. The blank characters are not skipped.
3. Return Value of scanf ()
Returns the number of successfully read items. Scanf () returns 0 if it does not read any project (this happens when it expects a number and you type a non-numeric string. When he detects that "The end of the file" is, he returns the EOF (EOF is a special value defined in the file stdio. h. Generally, the # define command defines the value of EOF as-1 ).
Main () {int I; char * p, str [20]; scanf ("% d", & I); scanf ("% s", p ); /* input string from the drive */scanf ("% s", str); printf ("I = % d", I); printf ("% s", p ); /* output string to the screen */printf ("% s", str );}