当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C语言 sprintf()用法及代码示例


用法:

int sprintf(char *str, const char *string,...); 

sprintf代表“String print”。它将输出存储在sprintf中指定的char缓冲区中,而不是在控制台上进行打印

例:


// Example program to demonstrate sprintf() 
#include<stdio.h> 
int main() 
{ 
    char buffer[50]; 
    int a = 10, b = 20, c; 
    c = a + b; 
    sprintf(buffer, "Sum of %d and %d is %d", a, b, c); 
  
    // The string "sum of 10 and 20 is 30" is stored  
    // into buffer instead of printing on stdout 
    printf("%s", buffer); 
  
    return 0; 
}

输出:

Sum of 10 and 20 is 30



注:本文由纯净天空筛选整理自 sprintf() in C。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。