How it works...
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access