The use of sprintf () function in C + + _c language

Source: Internet
Author: User
Tags 04x sprintf
When you construct various types of data into strings, the powerful features of sprintf rarely disappoint you. Because sprintf is almost the same as printf in usage, it's just a different destination for printing, which is printed to a string, and the latter is printed directly on the command line. This also causes sprintf to be 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 are fixed, any number of parameters can be followed. And it's the essence, apparently on the second parameter:
(1) format the string.
printfAnd sprintfUse a formatted string to specify the format of the string. Using a format specifier (formatted specifications) that starts with "%" inside a format string to occupy a position and provide the corresponding variable in the next argument list, the final function replaces the descriptor with the corresponding position variable. Produces a string that the caller wants.
To format a string of numbers
one of the most common applications of sprintf is to print integers to a string, so SPRITNF can replace itoa in most situations.
such as:
Print the integer 123 into a string and save it in S.
sprintf (S, "%d", 123); Produce "123"
You can specify the width, insufficient left fill space:
sprintf (S, "%8d%8d", 123, 4567); Generated: "123 4567"
Of course, you can also align Left:
sprintf (S, "%-8d%8d", 123, 4567); Generated: "123 4567"
You can also print in 16:
sprintf (S, "%8x", 4567); lowercase 16, width to 8 positions, right aligned
sprintf (S, "%-8x", 4568); Uppercase 16, width to 8 positions, left-aligned
That way, an integer's 16 string is easy to get, but when we print the 16 feed, we usually want a equal-width format with a left complement of 0. Simply put a 0 in front of the number that represents the width.
sprintf (S, "%08x", 4567); Produce: "000011d7"
The 10-in-print printing above with "%d" can also use this left complement of 0.
Notice the problem of a symbolic extension: for example, if we want to print the memory 16 binary representation of a short integer, 1, on the Win32 platform, a shorter type occupies 2 bytes, so we naturally want to print it in 4 16 digits:
Short si =-1;
sprintf (S, "%04x", si);
Produce "ffffffff", what's going on? Because SPRITNF is a variable parameter function, in addition to the preceding two parameters, after the parameters are not type-safe, the function is not only through a "%x" to know the original function call before the pressure stack is a 4-byte integer or a 2-byte short integer, so take a unified 4 byte processing, resulting in the parameter stack when the symbolic extension, expanded into a 32-bit integer-1, printing when 4 position is not enough, the 32-bit integer-1 8-bit 16 into the system are printed.
If you want to see the true nature of SI, then you should let the compiler do 0 extensions instead of symbolic extensions (when extending the binary left complement 0 instead of the complement symbol bit):
sprintf (S, "%04x", (unsigned short) SI);
It's OK. Or:
unsigned short si =-1;
sprintf (S, "%04x", si);
sprintf and printf can also print an integer string in 8, using "%o". Note that 8 and 16 are not going to hit.
A negative number is an unsigned, in fact, a direct 16-or 8-binary representation of the internal encoding of the variable.
Control floating-point printing format
The printing and formatting control of floating point numbers is another common function of sprintf, floating point numbers use the format character "%f" control, default security
Leave 6 digits after the decimal point, such as:
sprintf (S, "%f", 3.1415926); Produce "3.141593"
But sometimes we want to control the width and scale of the print, and then we should use: "%M.NF" format, where M table
Shows the width of the print, and n indicates the number of digits after the decimal point. Like what:
sprintf (S, "%10.3f", 3.1415626); Produced: "3.142"
sprintf (S, "%-10.3f", 3.1415626); Produced: "3.142"
sprintf (S, "%.3f", 3.1415626); Do not specify the total width, resulting in: "3.142"
Pay attention to a question, you guess.
int i = 100;
sprintf (S, "%.2f", I);
What's going to happen? "100.00"? Is that right? Try it yourself, and try the following:
sprintf (S, "%.2f", (double) i);
The first one is definitely not the right result, as mentioned earlier, the caller does not know that the format controller corresponding to I is a "%f" when the parameter is pressed. The function is not aware of the execution of the stack in the year was a whole number, so the poor save integer I of the 4 bytes was use robust reporting forced as a floating-point format to explain the whole mess. However, if someone is interested in using a floating-point number to encode manually, you can use this method to verify that the results you have manually choreographed are correct.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.