This article illustrates the difference between the methods used by sprintf in C + + and printf. Share to everyone for your reference. The specific analysis is as follows:
First, we'll look at the MSDN prototype and the sprintf prototype
Copy Code code as follows:
int printf (const char *format [, argument] ...);
And
Copy Code code as follows:
int sprintf (char *buffer, const char *format [, argument] ...);
By definition, the functions of the two are very similar.
If you are in touch with more console programs, see printf More, printf functions print results to the screen, and the sprintf function can complete other data types converted to strings.
Let me explain from the following points
(1) The first two parameters of the function are fixed, the optional parameter is any, buffer is to hold the string pointer or array name, Fromat is a formatted string, as long as the formatted string used by printf, can be used in sprintf, formatted string is the essence of the function.
(2) You can first format an integer data into a string. Like what:
Copy Code code as follows:
Char str[20];
int i_arg = 12345;
sprintf (buf, "%-6d", I_arg);
(3) Look at an example of a floating-point type. Like what:
Copy Code code as follows:
Char str[20];
Double d_arg = 3.1415926;
sprintf (str, "%6.2f", D_arg);
can control the accuracy
(4) to connect two strings, or to concatenate multiple strings,%M.N in the output of the string, m represents the width, the number of columns in the string, and n represents the actual number of characters. %M.N in floating-point numbers, M also represents width, and n represents the number of decimal digits. Like what:
Copy Code code as follows:
Char dest[256];
Char src1[] = {' A ', ' B ', ' C ', ' d ', ' e '};
Char src2[] ={' 1 ', ' 2 ', ' 3 ', ' 4 '};
sprintf (dest, "%.5s%.4s", SRC1,SRC2);
You can also dynamically intercept some characters of a string:
Copy Code code as follows:
Char dest[256];
Char src1[] = {' A ', ' B ', ' C ', ' d ', ' e '};
Char src2[] ={' 1 ', ' 2 ', ' 3 ', ' 4 '};
sprintf (dest, "%.*s%.*s", 2,SRC1,3,SRC2);
You can also steal a valid bit of a floating-point type
Copy Code code as follows:
sprintf (str, "%*.*", 10,4,d_arg);
To add, the sprintf return value is the number of characters in the string, which is the result of strlen (str),
You can also print the address of a parameter
Copy Code code as follows:
int i=2;
sprintf (str, "%0*x", sizeof (void *), &i);
or use:
Copy Code code as follows:
In addition, these are all multibyte-type (ANSI) functions, and similar functions should be used for Unicode types:
Copy Code code as follows:
int wprintf (const wchar_t *format [, argument] ...);
int swprintf (wchar_t *buffer, const wchar_t *format [, argument] ...);
The usage is very similar to the above, that is, the type is different.
For printf and sprintf This function is included in the <stdio.h> header file
The sprintf and wprintf functions are included in the <stdio.h> or <wchar.h> header file.
I hope this article will help you with the C + + program design.