Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • Note that snprintf is not guaranteed to null-terminate the string. Here's one way to make sure it works: <pre> char buffer[128]; buffer[sizeof(buffer)-1] = '\0'; snprintf(buffer, sizeof(buffer)-1, "%s%d", name.c_str(), age); std::cout << buffer << std::endl; </pre> Commented Oct 10, 2008 at 16:06
  • My tendency would be to never use sprintf, since this can result in buffer-overflows. The example above is a good example where using sprintf would be unsafe if the name was very long. Commented Oct 11, 2008 at 18:06
  • note that snprintf is equally non-standard c++ (like itoa which you mention). it's taken from c99 Commented Feb 9, 2009 at 3:36
  • @terson: I see no occurence of sprintf in the answer, only snprintf. Commented Jul 3, 2013 at 18:07