Typically, when using sscanf() to interpret a string buffer into values, the string buffer is already known or has been allocated elsewhere. sscanf() converts the string into the desired values, assigning them to variables. The sizes of these variables are known by their data type.
On the other hand, when using sprintf() to convert values into characters, the final output buffer size required is rarely known. We can either exercise great care to allocate a specific array size or, more commonly, we can simply allocate an array that is reasonably larger than expected, ignoring any unused or even unneeded buffer space.
The following program demonstrates the use of sscanf() and sprintf():
#include <stdio.h>
#include <string.h> // for memset
const int bufferSize = 80;
int main( void ) {
int anInteger = -1;
doubleaDouble = -1.0;
int numScanned= 0 , numPrinted = 0;
char sIn[] = ...