Home >Backend Development >C++ >How to Convert an Integer to a String in C ?
Converting Integer to String in C : Alternatives to itoa()
In C , converting an integer to a string has several alternatives to the itoa() function, which may not be supported in all environments. Here are some alternative approaches:
std::to_string (C 11 and later):
#include <string> std::string s = std::to_string(5);
std::stringstream (pre-C 11):
#include <sstream> int i = 5; std::string s; std::stringstream out; out << i; s = out.str();
va_arg() with sprintf:
char s[256]; va_list valist; va_start(valist, format); vsnprintf(s, 256, format, valist); va_end(valist);
snprintf:
char s[256]; snprintf(s, 256, "%d", i);
Depending on your development environment and C standard support, you can choose the most appropriate method to convert integers to strings in your code.
The above is the detailed content of How to Convert an Integer to a String in C ?. For more information, please follow other related articles on the PHP Chinese website!