C language format output function and Restricted Area
Compiling environment: Debian: 7.6gcc: 4.7.2
I. formatting the output functionThe standard formatting output function designed in C language is as follows:
#include
int printf(const char *format, ...);int fprintf(FILE *stream, const char *format, ...);int sprintf(char *str, const char *format, ...);int snprintf(char *str, size_t size, const char *format, ...);#include
int vprintf(const char *format, va_list ap);int vfprintf(FILE *stream, const char *format, va_list ap);int vsprintf(char *str, const char *format, va_list ap);int vsnprintf(char *str, size_t size, const char *format, va_list ap);
2. Relationships1. Commonalities: output data to the output device under the control of formatted strings. Format the string to specify how to convert subsequent parameters. For example:
Printf ("% 02x", 100);/* the output is hexadecimal */printf ("% dx", 100);/* the output is hexadecimal */
2. Differences: the output device score is as follows:
Printf () vprintf () |
OutputTo stdout |
Fprintf () vfprintf () |
OutputTo specified output stream |
Sprintf () snprintf () vsprintf () vsnprintf () |
OutputTo String str |
Split by specified operation byte:
Snprintf () vsnprintf () |
Write up to size bytes (including '\ 0') to dtr |
Vprintf () vfprintf () vsprintf () vsnprintf () |
They are equivalent to: printf () fprintf () sprintf () snprintf (). The difference is that they all have a va_list list. Once the call is complete, the ap becomes "undefined ". |
3. Return Value: under normal circumstances, the function returns the number of successfully output characters (not including '\ 0 '). If an error occurs, a negative value is returned. Snprintf () and vsnprintf () Outputs no more than size bytes (including '\ 0'). If the size is smaller than the string length (not including' \ 0 '), the string is truncated. The returned value is the length of the string (not including '\ 0'), as follows: int len; char str_buf [100]; len = snprintf (buf, 10, "% s $", "1234567890123"); // len: 13, buf: "1234567890"
If the size is greater than the length of the data buffer, strings in the buffer length will be copied. The returned value is the length of the string (not including '\ 0'), as follows: char str_buf [5]; // len: 13. buf: "12345"
Iii. Usage notes1. When using sprintf () and vsprintf (), ensure that the data size does not exceed the size of the str buffer; otherwise, overflow may occur, resulting in a disaster. If not, use snprintf () and vsnprintf ().
2. Add the string to the back of the original string, as shown in the following figure: sprintf (buf, "% s append text", buf); In My gcc, this operation runs on schedule; however, in some gcc versions, the results are incorrect. Therefore, do not do this. Moreover, the standard clearly states that the result of calling the sprintf (), snprintf (), vsprintf (), and vsnprintf () functions is undefined if the source and target addresses overlap ). 3. Such code as printf (foo); often introduces bugs. This is because if foo contains "% n", printf () will write data to the memory and cause security vulnerabilities. Example: printf ("% n"); // Segmentation fault