snprintf() in C library

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

Practical Example to demonstrate snprintf()

A practical way to demonstrate snprintf is to implement a conservative buffer. We will allocate small amount of memory for a buffer and attempt to place a string into it. We create a larger buffer into it, if necessary. If the string still doesn’t fit, we exit the program.

// C program to demonstrate snprintf to
// implements conservative buffer
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int bufsize = 7;
    char* str = "GeeksforGeeks";
    char* buff = malloc(bufsize);

    if (snprintf(buff, bufsize, "%s", str) >= bufsize) {
        bufsize *= 2;
        printf("Not enough space. Trying %d bytes\n", bufsize);
        free(buff);
        buff = malloc(bufsize);

        if (snprintf(buff, bufsize, "%s", str) >= bufsize) {
            printf("Still not enough space. Aborting\n");
            exit(1);
        }
    }

    printf("There was enough space!\n");
    printf("buf: %s\n", buff);
    return 0;
}

Output:

Not enough space. Trying 14 bytes
There was enough space! 
buf: GeeksforGeeks

However, if bufsize was initially set to 5, the output would be the following:
Output:

Not enough space. Trying 10 bytes
Still not enough space. Aborting




Check out this Author's contributed articles.

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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1 Average Difficulty : 1/5.0
Based on 1 vote(s)