头文件:#include <stdio.h>
功能:用于将格式化的数据写入字符串
#include <stdio.h>
main()
{
    char buf[10];
    sprintf(buf, "The length of the string is more than 10");
    printf("%s", buf);
}
#include <stdio.h>
main()
{
    char a = ‘a‘;
    char buf[80];
    sprintf(buf, "The ASCII code of a is %d.", a);
    printf("%s", buf);
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
    char str[100];
    int offset =0;
    int i=0;
    srand(time(0));  // *随机种子
    for(i = 0;i<10;i++)
    {
        offset+=sprintf(str+offset,"%d,",rand()%100);  // 格式化的数据写入字符串
    }
    str[offset-1]=‘\n‘;
    printf(str);
    return 0;
}
[转][修]sprintf()函数:将格式化的数据写入字符串
原文:https://www.cnblogs.com/stxs/p/8809065.html