To constrain the size of the content assigned to the name member of the user1 structure, we will make use of the snprintf function. You can see that through the snprintf function, only the first 10 characters from the text bintuharwani are assigned to the name member. Because the length of the name member is 10, it is capable of storing 10 characters and hence no buffer overflow occurs and the value assigned to the orderid member will remain intact and undisturbed. On displaying the values of the orderid and name members, both of their values will be displayed correctly.
Let's use GCC to compile the sprintfsolved.c program. If you get no errors or warnings, it means the sprintfsolved.c program has compiled into an executable file: sprintfsolved.exe. Let's run this file:
In the preceding output, we can see that the extra formatted text that is assigned to the name member is truncated, hence the correct output of the name and orderid members is displayed on the screen.