Snprintf Function In C

The snprintf function also provides the count of characters that have been added to the buffer. Nonetheless, these characters are then rendered or shown by the printf function in the output statement or characters in the stdout header file.

Note: The snprintf function inserts a null character to the end of the resulting output that is also counted as the size of the buffer. Furthermore, the buffer is an array that stores only character type elements, not in the string type.

Syntax of the snprintf function in C

Below is the format of the snprintf function in the C programming language.

Example

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

Parameters:

str : It is a character type array buffer.

The size parameter specifies the maximum capacity for storing characters within the buffer.

In the C programming language, a string represents a structure that includes similar specifications to those defined by the printf function in the stdio.h header file.

…: It is an optional (…) parameter or argument.

Return Values:

The snprintf function provides the count of characters or data written to a buffer of adequate size, excluding the null terminator. In cases where the written content exceeds the buffer capacity, a negative value is returned. If the buffer size is insufficient, the string provided will be either cut short or adjusted to fit the buffer size.

Example 1: Illustration showing the utilization of the snprintf function in the C programming language

Let's develop a program to verify the buffer size and provide the count of characters entered into the buffer utilizing the snprintf function in the C programming language.

Example

/* create an example to use the snprintf function in c. */
#include <stdio.h>
#include <conio.h>

int main ()
{
	// declare and initialize the char variable
	char *r = "Logic Practice.com";
	char buf[100]; // define the size of character type buffer 
	
	/* use the snprintf() function to return the no. of character founded in the buffer area */
     int n = snprintf (buf, 34, "%s \n", r); // 34 represents the size of buffer to store max characters
	
	// display the string stored in the buffer and count each character of the buffer area.
	printf (" The given string is: %s \n Count the stored character: %d \n", buf, n);
	
	return 0; 	 
}

When running the aforementioned code, it generates the specified result on the console display.

Example

The given string is: Logic Practice.com

Count the stored character: 16

2 nd execution

Example

The given string is: Logic Practice.com
Count the stored character: -1

Now, the maximum input character limit has been decreased from 34 to 14 characters. Consequently, when this limit is exceeded, the function now returns a negative value, indicating that the buffer size is insufficient for the provided string.

Example 2: Utilizing the snprintf Function in C

  1. Begin by including the necessary header file for the standard input-output library.
  2. Declare a character array to store the formatted string.
  3. Use the snprintf function to format the string with specified length constraints.
  4. Specify the destination buffer, maximum characters to write, and the format string with placeholders.
  5. Handle the return value of snprintf to check for errors or truncation.
  6. Finally, utilize the formatted string as needed in your program.

Let's generate a sample scenario where we insert a character into the buffer and then retrieve it using the snprintf function in the C programming language.

Example

#include <stdio.h>
#include <conio.h>

int main ()
{
	char buf[200]; // define the size of character type buffer 

	int ret_val, buf_size = 55;
	char name[] = "David"; // define string
	int age = 19;
		
	// use the snprintf() function to return the no. of character found in buffer area
	ret_val = snprintf (buf, buf_size, "Hello friend, My name is %s, and I am %d years old.", name, age);
	
	
	/* check ret_value should be greater than 0 and less than the size of the buffer (buf_size). */
	if ( ret_val > 0 && ret_val < buf_size)
	{
		printf (" Buffer is written successfully! \n ");
		printf (" %s\n", buf);
		printf (" No. of characters read: %d", ret_val);
	}
	else
	{
		printf (" Buffer is not completely filled or written. \n ");
		printf (" %s \n", buf);
		printf (" The return value: %d", ret_val);
	}
	
	return 0; 
	 
}

When the program mentioned above is run, it displays the provided result on the console screen.

Example

Buffer is written successfully!
Hello friend, My name is David, and I am 19 years old.
No. of characters read: 53

In the program mentioned earlier, we defined a character array named buf[200] of type char, with buf_size variable set to allow a maximum of 55 characters to be inserted. Should the statement fall within the specified range, the snprintf function will provide the count of characters successfully read from the buffer.

2 nd execution

Example

Buffer is not completely filled or written.
Hello friend, My name is David and
The return value: -1

When setting the buf_size to 35, the statement provided will be truncated by the snprintf function, resulting in a negative return value (-1) and showing the message "Buffer is not entirely filled or written".

Input Required

This code uses input(). Please provide values below: