Sprintf function, sprintf
Sprintf function usage example
1 # include <stdio. h> 2 int main () 3 {4 // 1. connection string 5 char a1 [] = {'A', 'B', 'C', 'D', 'E', 'F', 'G '}; 6 char a2 [] = {'h', 'I', 'J', 'k', 'l', 'M', 'n '}; 7 char buffer [200]; 8 sprintf (buffer, "%. * s %. * s \ n ", sizeof (a1), a1, sizeof (a2), a2); printf (" % s \ n ", buffer); 9 10 // 2. data and place them in the string named buffer. (use sprintf to format various data and place it in the character array buffer) 11 char str [] = "computer", c = 'l'; 12 int I = 35, j; 13 float fp = 1.7320534f; 14 // format and print various data to buffer15 j = sprintf (buffer, "String: % s", str ); 16 j + = sprintf (buffer + j, "Character: % c", c); 17 j + = sprintf (buffer + j, "Integer: % d", I ); 18 j + = sprintf (buffer + j, "Real: % f", fp); // sprintf returns the number of bytes written to the buffer j19 // Note: sprintf is deprecated; consider using sprintf_s instead20 printf ("Output: \ n % s \ ncharacter count = % d \ n", buffer, j ); // The ending character '\ 0' is not counted as 21 22 // 3. format a numeric string (which can be used to replace the itoa function in most cases) 23 short m =-1; int n = 100; 24 sprintf (buffer, "%-4d % 4d", 123,456 7 ); printf ("% s \ n", buffer); // generate "123 4567" where 123 is left aligned with 25 sprintf (buffer, "% 08x", 4567 ); printf ("% s \ n", buffer); // generate: "201711d7", in hexadecimal notation, where the width occupies 8 positions, equal width format 26 sprintf (buffer, "% # 04X", (unsigned short) m); printf ("% s \ n", buffer ); // sprintf is a variable parameter function. Apart from the first two parameters, the following parameters must ensure the type security 27 sprintf (buffer, "%. 2f ", (double) n); printf (" % s \ n ", buffer); // same as 28 return 0; 29}View Code
Analysis:
1. (1) print the "% m. nf ", m indicates the occupied width (when the length of the string is insufficient, fill in spaces. If the length is exceeded, print according to the actual width), n indicates the maximum number of characters used from the corresponding string, of course, you can also take only part of the characters before and after.
(2) If you want the information numbers with the specified length in these format controllers to be dynamic, instead of Statically specified (many times the program needs to get several characters in the character array at runtime ), sprintf uses "*" to occupy a position that originally requires a constant number with a specified width or accuracy. The actual width or accuracy can be provided like other printed variables.
3. if m is not forcibly converted, then, the function cannot only use a "% X" to know whether the four-byte integer or two-byte short integer that was pressed during the parameter stack before the function call., the 4-byte processing method is adopted. As a result, the symbol is extended to 32-bit integer-1 when the parameter is pressed to the stack. Because the four locations are insufficient during printing, the 32-bit integer-1 8-bit 16 is printed out.
Reference link:
Http://baike.baidu.com/link? Url = Response