The use of sprintf of function in C++

  • 2020-04-01 23:34:37
  • OfStack

Sprintf's power will rarely disappoint when it comes to constructing various types of data into strings. Since sprintf is almost identical in usage to printf, except that it is printed to a different destination, the former to a string and the latter to the command line. This also makes sprintf much more useful than printf.
Sprintf is a variable parameter function, defined as follows:
Int sprintf(char *buffer, const char *format [, argument]... );
In addition to the first two parameter types fixed, can be followed by any number of parameters. And the essence of it is obviously in the second parameter:
(1) on the format string.
Printf and sprintf USES formatted string to a specified string format, the format string used within some starting with "%" format specifier (the format specifications) to occupy a position in the back and provide the corresponding variables in the list, the final function with the corresponding position of the variable to replace the specifiers, produce a caller to a string.
Format numeric string
One of the most common USES of sprintf is to print integers into strings, so spritnf can replace itoa in most cases.
Such as:
// print the integer 123 as a string and save it in s.
Sprintf (s, "% d", 123); / / the "123"
You can specify the width, not enough space on the left:
8 8 sprintf (s, "% d % d", 123, 4567); // produced: "123 4567"
It can also be left aligned:
Sprintf (s, "% - 8 d % d", 123, 4567); // produced: "123 4567"
It can also be printed in hexadecimal:
Sprintf (s, "% x 8", 4567); // lower case hexadecimal, 8 widths, right aligned
Sprintf (s, "% - 8 x", 4568); // capital hexadecimal, width takes 8 positions, left aligned
This makes it easy to get a hexadecimal string for an integer, but when we print hexadecimal content, we usually want an equal width format with zeros on the left, so what do we do? Simply put a 0 in front of the width number.
Sprintf (s, "% x" 08, 4567); // generate: "000011D7"
The hexadecimal printing above in "%d" can also be done in this way.
Here's a sign extension: for example, if we want to print the memory hexadecimal representation of the short integer (short) -1, on the Win32 platform, a short takes 2 bytes, so we naturally want to print it with 4 hexadecimal digits:
Short si = 1;
Sprintf (s, "% 4 x," si);
"FFFFFFFF". What's going on? Because spritnf is a variable and function, in addition to the front two parameters, the parameters are not type safe behind, function more there is no way through a "% X" can only learn that when the parameters of pressure before the function call stack is pressed in a 4-byte integer or a short integer 2 bytes, so take the unified handling of 4 bytes, lead to arguments stack when did sign extension, expand a 32-bit integer 1, printing was not enough in the 4 positions, the 32-bit integer 1 8 hexadecimal are printed out.
If you want to see si for what it is, you should ask the compiler to do a 0 extension instead of a sign extension:
Sprintf (s, "% 4 x," (unsigned short) si);
That's it. Or:
Unsigned short si = -1;
Sprintf (s, "% 4 x," si);
Sprintf and printf can also print integer strings in base 8, using "%o". Note that neither hexadecimal nor hexadecimal can be played
When you print negative Numbers, they're all unsigned, which is essentially a direct hexadecimal or hexadecimal representation of the internal code of the variable.
Controls the floating-point print format
Floating-point printing and format control is another common function of sprintf, floating-point use format character "%f" control, default protection
Leave six decimal places behind, for example:
Sprintf (s, "% f", 3.1415926); / / the "3.141593"
But sometimes we want to control the width of the print and the number of decimal places, so we should use the format "%m.nf", where m table
Shows the width of the print, and n represents the number of digits after the decimal point. Such as:
Sprintf (s, "f" % 10.3, 3.1415626); // produced: "3.142"
Sprintf (s, "f" % 10.3, 3.1415626); // produced: "3.142"
Sprintf (s, "% 3" f, 3.1415626); // does not specify the total width, resulting in: "3.142"
Notice a problem, you guess
Int I = 100;
Sprintf (s, "% 2 f", I);
What's going to come out? "100.00"? Isn't it? Just try it yourself, but also try this:
Sprintf (s, "% 2 f", (double) I);
The first one is definitely not the correct result because, as mentioned earlier, the caller does not know that the format control corresponding to I is "%f". The function itself did not know that an integer was being pushed on the stack, so the poor 4 bytes that held the integer I were automatically forced to be interpreted as a floating point. However, if someone is interested in coding a floating point number by hand, you can use this method to verify that your hand-choreographed results are correct.

Related articles: