Learning how to avoid errors while formatting strings

In this recipe, we will use the snprintf function. The snprintf function will assign the formatted text to the name member but will limit the size of the string assigned to it. The difference between theĀ sprintf and snprintf functions is that sprintf simply assigns the complete formatted text to the destination string no matter what its capacity, whereas snprintf allows us to specify the maximum length of the text that can be assigned to the destination string. Therefore, a buffer overflow will not occur as only the specified size of the text is assigned to the destination string.