Converting Integers to Strings in C Without itoa()
Question:
Seeking an alternative to itoa() for integer-to-string conversion in C , as it generates warnings in Visual Studio and compilation errors in Linux.
Answer:
Fortunately, C offers multiple solutions:
C 11 and Beyond:
std::to_string: This function directly converts an integer to a string:
#include <string> int i = 5; std::string s = std::to_string(i);
Pre-C 11:
C Streams: Leverage the following stream manipulation:
#include <sstream> int i = 5; std::stringstream out; out << i; std::string s = out.str();
The example in question has been adapted from http://notfaq.wordpress.com/2006/08/30/c-convert-int-to-string/.
The above is the detailed content of How to Convert Integers to Strings in C without itoa()?. For more information, please follow other related articles on the PHP Chinese website!