When constructing various types of data into strings, the powerful features of sprintf seldom disappoint you. Since sprintf and printf have almost the same usage, but the printing destination is different, the former is printed into the string, and the latter is directly output on the command line. This also makes sprintf much more useful than printf.
Sprintf is a variable parameter function, which is defined as follows:
Int sprintf (char * buffer, const char * Format [, argument]...); in addition to the first two parameter types, you can take over multiple parameters later. But its essence is obviously in the second parameter:
(1) format the string.
Both printf and sprintf use formatted strings to specify the string format. Some format specifiers (format specifications) starting with "%" are used inside the format string to occupy a position, the variable is provided in the variable parameter list, and the function will replace the specifier with the variable at the corresponding position to generate the string that the caller wants.
Format a numeric string
One of the most common applications of sprintf is to print Integers to strings. Therefore, spritnf can replace ITOA in most cases.
For example: // print the integer 123 into a string and save it in S. Sprintf (S, "% d", 123); // generate "123"
You can specify the width, with spaces filled on the left: sprintf (S, "% 8d % 8d", 123,456 7); // generate: "123 4567"
Of course, it can also be left aligned: sprintf (S, "%-8d % 8d", 123,456 7); // generated: "123 4567"
It can also be printed in hexadecimal notation: printf (S, "% 8x", 4567); // in hexadecimal notation, where the width occupies 8 positions and the right alignment sprintf (S, "%-8x", 4568); // in hexadecimal notation, the width occupies 8 positions and is left aligned.
In this way, the hexadecimal string of an integer is easy to obtain, but when printing the hexadecimal content, we usually want an equal-width format with 0 on the left, what should we do? Simply add 0 to the number that represents the width. Sprintf (S, "% 08x", 4567); // generate: "201711d7"
You can also use this left-side 0 Complement Method to print the 10-in-hexadecimal format with "% d" above.
Pay attention to a symbol extension problem: for example, if we want to print a short INTEGER (short)-1 memory hexadecimal representation, on the Win32 platform, A short type occupies 2 bytes, So we naturally want to print it with 4 hexadecimal numbers: Short Si =-1; sprintf (S, "% 04x ", si); generate "ffffffff". What is going on? Because spritnf is a Variable Parameter Function, except the first two parameters, the following parameters are not of type security, there is no way for a function to know whether a 4-byte integer or a 2-byte short integer is pressed in the parameter stack before the function call through a "% x "., therefore, a 4-byte processing method is adopted, which leads to the symbol extension during parameter pressure stack, which is a 32-bit integer-1. When printing, the four locations are insufficient, print out the 8-bit 16 hexadecimal values of 32-bit integer-1. If you want to see the original face of Si, you should let the compiler do 0 extension instead of symbol extension (when the extension is performed, the left side of the binary complement 0 instead of the sign bit): sprintf (S, "% 04x", (unsigned short) Si. Or: Unsigned short Si =-1; sprintf (S, "% 04x", Si); sprintf and printf can also print integer strings in octal format, using "% O ". Note that both the octal and hexadecimal formats do not print negative numbers. They are all unsigned. In fact, they are the direct hexadecimal or octal representation of the variable's internal code. Controlling the printing and format of floating-point numbers is another common function of sprintf. the floating-point numbers are controlled by the format character "% F". By default, the six digits after the decimal point are kept. For example: sprintf (S, "% F", 3.1415926); // generate "3.141593", but sometimes we want to control the print width and decimal places, then we should use: "% m. NF "format, where M indicates the print width, N indicates the number of digits after the decimal point. For example: sprintf (S, "% 10.3f", 3.1415626); // generate: "3.142" sprintf (S, "%-10.3f", 3.1415626); // generate: & quot; 3.142 & quot; sprintf (S, & quot; %. 3f ", 3.1415626); // The total width is not specified, resulting in:" 3.142 "Pay attention to a problem, you guess int I = 100; sprintf (S," %. 2f ", I); what will it do? 100.00 "? Right? Try it on your own and try the following: sprintf (S, "%. 2f ", (double) I); the first one is definitely not the correct result. The reason is the same as previously mentioned, when the parameter is pushed to the stack, the caller does not know that the format controller corresponding to I is "% F ". When a function is executed, the function itself does not know that the number pushed to the stack in the current year is an integer, so the four bytes that saved the integer I were forcibly interpreted as a floating-point number. However, if someone is interested in manually encoding a floating point number, you can use this method to check whether the result of your manual arrangement is correct.