📜  sprintf()在C中

📅  最后修改于: 2021-05-26 01:31:21             🧑  作者: Mango

句法:

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

返回:

If successful,
it returns the total number of 
characters written excluding 
null-character appended in the string, 
in case of failure a negative number 
is returned .

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

C
// Example program to demonstrate sprintf()
#include 
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
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。