Grizlyk wrote:
JeffCameron ΠΙΣΑΜ(Α):
int number;
char s1[256];
number = 42;
sprintf(s1, "%d", number);
snprintf can be used (if implemented) as safe version of sprintf
In a conforming C++ implementation, std::snprintf() will always be
implemented. The routine is declared in <cstdio>.
std::snprintf() is a much safer choice than std::sprintf() when
converting a number to a string. In fact sprintf() which should never
be called at all in a modern application - due to the risk of
overruning the buffer by calling the function.
Of course snprintf() is not perfectly safe either, since a buffer
overrun - while less likely - is still possible. With a string class
object, however, there is no risk at all of a buffer overrun. Therefore
using std::string for the number-to-string conversion is the best
option; and std::string should be the default choice of for managing
string values in a C++ program.
Greg