Thursday 14 February 2013

sprintf(), sscanf() Functions for Formatting Data in Memory



The formatting functions available as part of the standard library for formatting data in memory, are:

-> sprintf()

->sscanf()

These functions, like printf() or scanf(), write variables to a destination or read data for variables from a destination. The difference is that while printf() writes to the screen, sprintf "writes" to a variable in the memory. Similarly while scanf() reads from the keyboad into variables, sscanf() "read" from a variable in the memory and stores the data in different variables specified. These funcions are used for transferring data between variables in a specific format.

The syntax of the above functions is similar to that of the functions, fprintf() and fscanf() respectively.

The syntax of the functions sprintf() is:

sprintf( string, format-specification, data, ...);

where,

string  - is a pointer to the destination string in memory which will contain the formatted data as per the format specification.

format- specification  - is the specification of how data is to be formatted

The function sprintf() is useful for formatting data and controlling the width of the destination string.

The sytax of the function sscanf() is:

sscanf(string, format-specification, variable, ..);



Powered by Blogger.