Formatted output string - sprintf

sprintf() is very similar to printf(), but the print destination is different. The former (sprintf) is printed to a string, the latter (printf) is output directly on the command line. 
sprintf() is a variadic function that formats strings. 

The function prototype is: 
int sprintf( char* buffer, const char* format [, argument], … ); //format format is the same as the format controller of printf() 

Parameter list 
buffer: char pointer, pointing to the buffer of the string to be written. 
format: format string. 
[argument]…: Optional arguments, which can be any type of data. 
Return value: string length (strlen), the number of characters printed into the buffer 

Functions: 
1. Convert numeric variables to strings 
2. Get hexadecimal and octal strings of integer variables 
3. Concatenate multiple strings 

 

 

effect:

One: format string (commonly used)

char str[10] = {0};

//Print the integer 123 as a string and save it in str
sprintf(str, "%d", 123); //Output: "123"

//You can specify the width, fill the space on the left side:
sprintf(str, "%8d%8d", 123, 4567); //Output: "_1234567"
//Of course, it can also be left-aligned:
sprintf(str, "%- 8d%8d", 123, 4567); //output: "123_4567"

//It can also be printed in hexadecimal:
sprintf(str, "%8x", 4567); //lowercase hexadecimal, the width occupies 8 positions, and is right-aligned. Output:____11d7
sprintf(str, "%-8X", 4567); //Uppercase hexadecimal, width occupies 8 positions, left-aligned. Output: 11D7_____

//Monospace format with 0 on the left (decimal %d can also be used)
sprintf(s, "%08X", 4567); //Output: "000011D7"

 

Two: control the printing format of floating-point numbers

Floating-point numbers are controlled by the format character "%f", and 6 digits after the decimal point are reserved by default. For example, 
sprintf(s, "%f", 3.1415926); //输出:"3.141593" 
when you need to control the printing width and decimal places, use the "%m.nf" format. (m is the width of the print, and n is the number of digits after the decimal point.)

sprintf(s, "%10.3f", 3.1415626); //Output: "3.142"
sprintf(s, "%-10.3f", 3.1415626); //Output: "3.142"
sprintf(s, "%.3f" , 3.1415626); //Do not specify the total width, output: "3.142"

int i = 100; sprintf(s, "%.2f", i); //输出:"0.00"

sprintf(s, "%.2f", (double)i); //输出:"100.00 "

spritnf is a variadic function, except for the first two parameters, the latter parameters are not type safe. 
There is no way for a function to know whether the parameter pushed on the stack before the function call is an integer or a floating-point number just through a "%f". 
The 4-byte i that holds the integer is forced to be interpreted as an 8-byte floating point number.

 

Three: connection string

char s[20] = {0};
char a1[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
char a2[] = {'H', 'I', 'J', 'K', 'L', 'M', 'N'};
// sprintf(s, "%s%s", a1, a2); //Don't do that!乱码
// sprintf(s, "%7s%7s", a1, a2); //乱码
sprintf(s, "%.7s%.7s", a1, a2); //输出:"ABCDEFGHIJKLMN"
sprintf(s, "%.6s%.5s", a1, a2); //输出:"ABCDEFHIJKL"

%m.ns ≈ %m.nf: 
m represents the occupied width (fill a space when the string length is insufficient, and print it according to the actual width); 
n represents the maximum number of characters taken from the corresponding string, and it is used more . 
Try to specify the maximum number of characters in the form of "%.ns".

Dynamically specify access character length: sprintf uses "*" to place a placeholder [ position of a constant number specifying width or precision ] . 
Thus, the above example can become: 
sprintf(s, "%.*s%.*s", 7, a1, 7, a2); 
or: 
sprintf(s, "%.*s%.*s", sizeof(a1), a1, sizeof(a2), a2);

In fact, the printing characters, integers, floating-point numbers, etc. introduced earlier can dynamically specify those constant values, such as:

sprintf(s, "%-*d", 4, 'A'); //Output: "65"
sprintf(s, "%#0*X", 8, 128); //Output: "0X000080", "#" produces 0X
sprintf(s, "%*.*f", 10, 2, 3.1415926); //output: "3.14"

 

Four: print address information

When debugging a program, you want to see the addresses of some variables or members. Since the address or pointer is just a 32-bit number, it can be printed with "%u" which prints an unsigned integer: 
sprintf(s, "%u", &i); 
display the address in hexadecimal: 
sprintf(s, "%08X", &i); 
address printing, sprintf provides a special "%p":  ≈ 
sprintf(s, "%p", &i); 

sprintf(s, "%0*x", 2 * sizeof(void *), &i);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325043265&siteId=291194637