Showing results for long int to string in c
Search instead for long int to string in c c
long long x = 999;
char str[256];
sprintf(str, "%lld", x);
printf("%s\n", str);
Answer from Ziffusion on Stack Overflow
Discussions

How do I convert a long to a string in C++? - Stack Overflow
Would it not be faster as compared to other solutions using std::stringstream ? 2016-07-22T20:09:32.563Z+00:00 ... There are several ways. Read The String Formatters of Manor Farm for an in-depth comparison. ... Yes. This is at least the third time I've linked it on SO. 8v) 2009-06-03T22:47:56.883Z+00:00 ... #include .... std::stringstream ss; ss << a_long_int... More on stackoverflow.com
🌐 stackoverflow.com
c - How to convert unsigned long to string - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... In the C language, how do I convert unsigned long value to a string (char *) and keep my source code portable or just recompile it to work on other platform (without rewriting code? More on stackoverflow.com
🌐 stackoverflow.com
Convert Int / Long Long to a String in C
What exactly are you trying to do? Are you trying to convert a long number into a string? or do you have something else in mind? #include int main() { char b[1000]; long long int i = 1234567891234; sprintf( b, "Formatted data: %lld", i ); puts( b ); } might make some sense but notice we are assigning to a character array More on reddit.com
🌐 r/cs50
4
3
December 24, 2016
How can I convert an int to a string in C? - Stack Overflow
How do you convert an int (integer) to a string? I'm trying to make a function that converts the data of a struct into a string to save it in a file. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Quora
quora.com › How-do-you-convert-a-long-long-int-to-a-string-in-C-I-get-it-correctly-when-I-was-programming-it-in-C-but-I-cant-seem-to-find-an-equivalent-of-to_string-in-C-I-also-tried-the-sprintf-though-Im-not-sure-if-I-did-it
How do you convert a long long int to a string in C? I get it correctly when I was programming it in C++, but I can't seem to find an equ...
Answer (1 of 5): Answered as: How do you convert a long long int to a string in C? I get it correctly when I was programming it in C++, but I can't seem to find an equivalent of to_string() in C. I also tried the sprintf(), though I'm not sure if I did it correctly, but its error I’ll try ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-convert-an-integer-to-a-string-in-c
How to Convert an Integer to a String in C? - GeeksforGeeks
July 23, 2025 - We can convert an integer to a string manually by extracting each digit one by one and converting it to its corresponding character by using their ASCII code and storing it in the character array to form a string.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › convert-long-to-string-in-cpp
Convert Long to String in C++ - GeeksforGeeks
August 26, 2026 - The long integer is inserted into the stream using <<. The str() function retrieves the stream contents as a string. The resulting string is printed. C++11 introduced std::to_string(), which provides a simple way to convert numeric values into ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-program-for-long-to-string-conversion
C Program For Long to String Conversion - GeeksforGeeks
August 3, 2022 - // C Program for Long to // String Conversion #include <stdio.h> #include <string.h> #define Max_Digits 10 int main() { long N = 1243; char str[Max_Digits + sizeof(char)]; sprintf(str, "%ld", N); printf("string is: %s \n", str); } Output · ...
🌐
Linux Hint
linuxhint.com › c-int-string
Convert Int to String in C – Linux Hint
The Sprintf function is one of the functions you can use to convert an integer value into a string.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › what-is-the-best-way-in-c-to-convert-a-number-to-a-string
What is the best way in C to convert a number to a string?
June 29, 2009 - This can be done using functions such as sprintf() or snprintf(), which format the number and store its character representation in a string. Example: Use sprintf() function. ... #include<stdio.h> int main() { char result[50]; float num = 23.34; ...
🌐
GitHub
gist.github.com › itsyarkee › 6589484
convert long long to string in C · GitHub
convert long long to string in C · Raw · longlong2str.c · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
WsCube Tech
wscubetech.com › resources › c-programming › programs › int-to-string
How to Convert Int to String in C? 5 Ways With Code
September 17, 2026 - Learn five simple ways to convert an int to a string in C using sprintf(), itoa(), and other methods. Includes code examples!
Top answer
1 of 16
2884

C++11 introduces std::stoi (and variants for each numeric type) and std::to_string, the counterparts of the C atoi and itoa but expressed in term of std::string.

Use with AI#include <string> 

std::string s = std::to_string(42);

is therefore the shortest way I can think of. You can even omit naming the type, using the auto keyword:

Use with AIauto s = std::to_string(42);

Note: see [string.conversions] (21.5 in n3242)

Note: for faster/non-allocating conversions, consider the {fmt} library's fmt::format_int as per @vitaut's answer.

2 of 16
241

C++20: std::format would be the idiomatic way now.


C++17:

Picking up a discussion with @v.oddou a couple of years later, C++17 has delivered a way to do the originally macro-based type-agnostic solution (preserved below) without going through macro ugliness.

Use with AI// variadic template
template < typename... Args >
std::string sstr( Args &&... args )
{
    std::ostringstream sstr;
    // fold expression
    ( sstr << std::dec << ... << args );
    return sstr.str();
}

Usage:

Use with AIint i = 42;
std::string s = sstr( "i is: ", i );
puts( sstr( i ).c_str() );

Foo x( 42 );
throw std::runtime_error( sstr( "Foo is '", x, "', i is ", i ) );

C++98:

Since "converting ... to string" is a recurring problem, I always define the SSTR() macro in a central header of my C++ sources:

Use with AI#include <sstream>

#define SSTR( x ) static_cast< std::ostringstream & >( \
        ( std::ostringstream() << std::dec << x ) ).str()

Usage is as easy as could be:

Use with AIint i = 42;
std::string s = SSTR( "i is: " << i );
puts( SSTR( i ).c_str() );

Foo x( 42 );
throw std::runtime_error( SSTR( "Foo is '" << x << "', i is " << i ) );

The above is C++98 compatible (if you cannot use C++11 std::to_string), and does not need any third-party includes (if you cannot use Boost lexical_cast<>); both these other solutions have a better performance though.

🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "c howtos" › "how to convert an integer to a string in c"
How to Convert an Integer to a String in C | Delft Stack
February 12, 2024 - This tutorial introduces different methods in C to convert an integer into a string. It describes the use of the sprintf(), snprintf(), and itoa() functions in C.
🌐
IBM
ibm.com › docs › en › i › 7.4.0
_ltoa() — Convert Long Integer to String
The page you requested cannot be displayed (HTTP response code 403)