获取va_list格式化长度

调用_vscprintf(ANSI) 、 _vscwprintf(WCHAR)获取格式化输出字符串长度(注意此长度不包含结束符号),MSDN示例:

// crt_vsprintf.c
// compile with: /W1
// This program uses vsprintf to write to a buffer.
// The size of the buffer is determined by _vscprintf.

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

void test( char * format, ... )
{
    va_list args;
    int     len;
    char    *buffer;

    // retrieve the variable arguments
    va_start( args, format );
    
    len = _vscprintf( format, args ) // _vscprintf doesn't count
                                + 1; // terminating '\0'
    
    buffer = (char*)malloc( len * sizeof(char) );

    vsprintf( buffer, format, args ); // C4996
    // Note: vsprintf is deprecated; consider using vsprintf_s instead
    puts( buffer );

    free( buffer );
}

int main( void )
{
   test( "%d %c %d", 123, '<', 456 );
   test( "%s", "This is a string" );
}
 


你可能感兴趣的:(获取va_list格式化长度)