📜  C库中的snprintf()

📅  最后修改于: 2021-05-25 23:01:32             🧑  作者: Mango

snprintf()函数格式并将一系列字符和值存储在数组缓冲区中。在加入n个参数,它表示字符(包括在空字符结束时)的最大数量的的snprintf()函数将被写入到缓冲器。它在头文件中定义。

句法 :

int snprintf(char *str, size_t size, const char *format, ...);

*str : is a buffer.
size : is the maximum number of bytes
(characters) that will be written to the buffer.
format : C string that contains a format
string that follows the same specifications as format in printf
... : the optional ( …) arguments 
are just the string formats like (“%d”, myint) as seen in printf.
// C program to demonstrate snprintf()
#include 
  
int main()
{
    char buffer[50];
    char* s = "geeksforgeeks";
  
    // Counting the character and storing 
    // in buffer using snprintf
    int j = snprintf(buffer, 6, "%s\n", s);
  
    // Print the string stored in buffer and
    // character count
    printf("string:\n%s\ncharacter count = %d\n",
                                     buffer, j);
  
    return 0;
}

输出:

string:
geeks
character count = 14
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。