16

I'd like to convert a long long to a string in C.

long long x = 999;

I'd like to convert x to a string. How could I go about doing that?

Thanks.

Tommy
  • 965
  • 6
  • 13
  • 21
  • 8
    This answer gives a hint: [How to convert unsigned long to string](http://stackoverflow.com/questions/2709713/how-to-convert-unsigned-long-to-string) and the [snprintf](http://linux.die.net/man/3/snprintf) man page the `long long` modifier: `ll`, *ell-ell*. – miku Apr 19 '13 at 00:25
  • I'd like to store the string in a char* though? – Tommy Apr 19 '13 at 00:29
  • That's exactly what @miku's comment explains how to do. – Carl Norum Apr 19 '13 at 00:30
  • not really seeing it? – Tommy Apr 19 '13 at 00:32
  • 7
    `sprintf(string, "%lld", x);` – Carl Norum Apr 19 '13 at 00:33
  • 1
    @Tommy: You do not store the ascii representation of a `long long` **in** a `char *`, but you store it in the memory the `char *` is pointing to, as a `char *` is a pointer and contains nothing more than an address. – alk Apr 19 '13 at 06:23

1 Answers1

26
long long x = 999;
char str[256];
sprintf(str, "%lld", x);
printf("%s\n", str);
Ziffusion
  • 8,779
  • 4
  • 29
  • 57
  • 9
    It would be better, in general, to use `snprintf()`, though with the buffer size you specify and the data in use it is not crucial. – Jonathan Leffler Apr 19 '13 at 00:40