The snprintf() function formats and stores a series of characters and values in the array buffer. The snprintf() function with the addition of the n argument, which indicates the maximum number of characters (including at the end of null character) to be written to buffer. It is defined in <stdio.h> header file.
Syntax :
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 <stdio.h> 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; } |
Output:
string: geeks character count = 14
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to print range of basic data types without any library function and constant in C?
- Unordered Sets in C++ Standard Template Library
- Advanced C++ with boost library
- Inbuilt library functions for user Input | scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s
- How to add "graphics.h" C/C++ library to gcc compiler in Linux
- The C++ Standard Template Library (STL)
- List in C++ Standard Template Library (STL)
- Deque in C++ Standard Template Library (STL)
- Map in C++ Standard Template Library (STL)
- Wide char and library functions in C++
- C Library math.h functions
- isgraph() C library function
- difftime() C library function
- wcstof function in C library
- wprintf() and wscanf in C Library
- boost::split in C++ library
- <iterator> library in C++ STL
- Factorial of Large Number Using boost multiprecision Library
- Difference between Header file and Library
- <strings> library in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

