函数名:sprintf()
功能:格式化传递给字符串
#include <stdio.h>
// 函数概要
int sprintf( char* buf, const char* format, ... );
参数:
buf 用于存储格式化字符串的指针buffer
format 传递给buf的格式化字符串
实例:
#include <stdio.h>
#include <stdlib.h>
void Test::test()
{
char buff[99];
int age = 18;
sprintf(buff, "I am %d years old.", age);
}
sprintf()用法和printf()用法类似,只是printf()用于打印log,sprintf()传递字符串到buf
函数名:vsprintf()
功能:格式化传递给字符串
#include <stdio.h>
#include <stdarg.h>
// 函数概要
int vsprintf( char* buf, const char* format,va_list arg );
参数:
buf 用于存储格式化字符串的指针buffer
format 传递给buf的格式化字符串
arg 可变参数列表
实例:
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
char msgbuf[80];
char *fmtmsg( char *format, ... )
{
va_list arglist;
va_start( arglist, format );
strcpy( msgbuf, "Error: " );
vsprintf( &msgbuf[7], format, arglist );
va_end( arglist );
return( msgbuf );
}
int main( void )
{
char *msg;
msg = fmtmsg( "%s %d %s", "Failed", 100, "times" );
printf( "%s\n", msg );
return EXIT_SUCCESS;
}