vsprintf and vsnprintf

Source: Internet
Author: User
Tags truncated
vsprintf

  /* Function Name: vsprintf function: Send formatted output to string return value: Normally returns the length of the generated string (minus/0), and error conditions return negative usage: int vsprintf (char *string, Char *format,   Va_list param);   Note: This function will appear memory overflow condition, recommend to use VSNPRINTF program Example: * * #include <stdarg.h> char buffer[80];   int VSPF (char *fmt, ...)   {va_list argptr;   int cnt;   Va_start (Argptr, FMT);   CNT = vsprintf (buffer, FMT, argptr);   Va_end (ARGPTR);   return (CNT);   int main (void) {int inumber = 30;   float fnumber = 90.0;   Char string[4] = "ABC";   VSPF ("%d%f%s", Inumber, Fnumber, String);   printf ("%s/n", buffer);   return 0; This program results in the usage of 90.000000 ABC Va_list (Va_list is a set of macros that solve the argument problem in C): (1) First, define a va_list-type variable in the function, which is the pointer to the parameter (2) and then use the Va_ The start macro initializes the va_list variable that is just defined, and the second argument to the macro is the first parameter of the variable parameter, which is a fixed parameter. (for example, after running Va_start (AP,V), the AP points to the address of the first variable parameter on the stack.)   ) (3) then returns the variable parameter with Va_arg, and the second parameter of the va_arg is the type of the parameter you want to return. (4) Finally using the Va_end macro to end the acquisition of variable parameters.   Then you can use the second argument in the function. If the function has more than one variable parameter, call Va_arg in turn to get each parameter. _vsnprintf header file: #include <stdarg.h> function declaration: int _vsnprintf (char *buffEr, size_t max_count, const char *format, va_list varglist);   Parameter description: 1. Char *buffer [out], storing the generated formatted string here.   2. size_t Max_count [in], the maximum number of bytes that can be accepted by the buffer, preventing the array from crossing. 3. const char *format [in], format string 4. Va_list varglist [in], va_list variable.   Va:variable-argument: The variable parameter usage is similar to vsprintf, except that the max_count limit is added. Return value Description: If this function is successfully called, returns the number of characters written to the buffer (excluding the ending '/0 '). The snprintf and vsnprintf functions cannot write more than the size (including the end of ' 0 ') bytes. If the output is truncated for the above reasons, returns the number of characters (excluding the ending '/0 ') that have been successfully written to the buffer if there is sufficient memory space. Therefore, if the return value is equal to size or greater than size, the character that is output to buffer is truncated and a negative number is returned if an error is encountered in the output process.edit this paragraph usage example int Mon_log (char* format, ...)   {va_list Varglist//define a va_list-type variable, which is a pointer to a parameter. Va_start (varglist, format); With Va_startMacrosInitializes the variable, the second parameter of the macro is the first parameter of the variable parameter and is a fixed parameter._vsnprintf(Str_tmp, 3, format, varglist); Note, do not omit the front _ Va_end (varglist); With Va_endMacrosEnd of the variable parameter fetch return 0;   }//Call the above function Mon_log ("%d,%d,%d,%d", 1,2,3,4); Return value usage: #include <stdio.h> #include <stdlib.h> #include <stdarg.h> char * make_message (const CH Ar *fmt, ...)   {/* Initially assume that we need only 100 bytes of space/int n, size = 100;   Char *p;   Va_list ap;   if (p = (char *) malloc (size)) = = null) return null;   while (1) {/* attempts to print operations in the requested space * * Va_start (AP, FMT);   n = vsnprintf (p, size, FMT, AP);   Va_end (AP);   /* If the vsnprintf call succeeds, return the string */if (n > 1 && N < size) returns p; /* vsnprintf call failed (n<0) or P's space is not enough to hold the size of the string (n>=size), try to request a larger space * * size *= 2;   /* Twice times the original size of space */if (p = (char *) realloc (p, size)) = = null) return null;   {/} int main () {/* * Call the function above * * char* str = make_message ("%d,%d,%d,%d", 5,6,7,8);   printf ("%s/n", str);   return 0; }

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.