📅  最后修改于: 2020-09-25 08:36:17             🧑  作者: Mango
int sprintf( char* buffer, const char* format, ... );
sprintf()
函数将格式指向的字符串写入缓冲区。 字符串格式可以包含以%开头的格式说明符,这些格式说明符由作为附加参数传递给sprintf()
函数的变量的值替换。
它在
#include
#include
using namespace std;
int main()
{
char buffer[100];
int retVal;
char name[] = "Max";
int age = 23;
retVal = sprintf(buffer, "Hi, I am %s and I am %d years old", name, age);
cout << buffer << endl;
cout << "Number of characters written = " << retVal << endl;
return 0;
}
运行该程序时,输出为:
Hi, I am Max and I am 23 years old
Number of characters written = 34