Home » snprintf() function in C

snprintf() function in C

by Online Tutorials Library

snprintf() function in C

In this section, we will discuss the snprintf() function in the C programming language. The snprintf is a predefined library function of the stdio.h header file, which redirects the output of the standard printf() function to other buffers.

The snprint() function is used to format the given strings into a series of characters or values in the buffer area. The snprintf() function contains an ‘n’ argument representing the maximum number of characters, including the null character, stored in the buffer area.

The snprintf function also returns the number of characters that are inserted or written to the buffer. However, these characters are returned or displayed by the printf() function in the print statement or characters in the stdout header file.

snprintf() function in C

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

Following is the syntax of the snprintf() function in the c programming language.

Parameters:

str: It is a character type array buffer.

size: it defines the maximum number of characters that can store in the buffer.

format: In C language, the string defines a format that contains the same type of specifications as the printf() function defines in the stdio.h header file.

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

Return Values:

The snprintf() function returns the number of characters or values that have been written or stored for a sufficiently large buffer without including the null terminating character. And if the written characters are larger than the buffer size, it returns a negative value. And if the buffer size is too small, the given string will be truncated or reduced to the buffer size.

Example 1: Program to demonstrate the snprintf() function in C

Let’s create a program to check the buffer size and return the number of character enter to the buffer using the snprintf() function in C.

When we execute the above program, it produces the given output on the console screen.

The given string is:  tutoraspire.com    Count the stored character: 16  

2nd execution

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

Now we reduce the max input character from 34 to 14, and this time, it returns a negative number, indicating the buffer size is less than the given string.

Example 2: Program to use the snprintf() function in C

Let’s create an example to insert the character to the buffer and return from the same using the snprintf() function in C programming language.

When we execute the above program, it produces the given output on the console screen.

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

In the above program, we declared the character type buffer buf[200], and the buf_size variable can insert the maximum characters is 55. If the given statement is in the defined range, the snprintf() function returns the total no. of characters read from the buffer.

2nd execution

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

When we define the buf_size as 35, the given statement is automatically truncated by the snprintf() function that returns a negative number (-1) and displays “Buffer is not completely filled or written”.


You may also like