當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。