C语言 sprintf

C语言 sprintf

#include <stdio.h>
int sprintf(char *str, const char *format, ...);

功能:根据参数format字符串来转换并格式化数据,然后将结果输出到str指定的空间中,直到出现字符串结束符 '' 为止。
参数:

  • str:字符串首地址
  • format:字符串格式,用法和printf()一样

返回值:

  • 成功:实际格式化的字符个数
  • 失败: - 1

案例

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    char ch[100];

    // 将内容放入ch内
    sprintf(ch,"hello world");
    sprintf(ch,"%02d+%02d=%02d",1,2,3);
    sprintf(ch, "%x+%o=%d", 0xabc, 2, 3);

    printf("%s
", ch);

    return 0;
}
sprintf 使用案例
原文地址:https://www.cnblogs.com/xiangsikai/p/12378546.html