Skip to content

Instantly share code, notes, and snippets.

@itsyarkee
Created September 17, 2013 02:44
Show Gist options
  • Save itsyarkee/6589484 to your computer and use it in GitHub Desktop.
Save itsyarkee/6589484 to your computer and use it in GitHub Desktop.
convert long long to string in C
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char* strfromlonglong(long long value){
char buf[32], *p;
unsigned long long v;
v = (value < 0) ? -value: value;
p = buf + 31;
do{
*p -- = '0' + (v%10);
v /= 10;
} while(v);
if(value < 0) *p -- = '-';
p ++;
int len = 32 - (p - buf);
char *s = (char*)malloc(sizeof(char) * (len + 1));
memcpy(s, p, len);
s[len] = '\0';
return s;
}
int main()
{
long long value;
char *r;
value = 5201214;
r = strfromlonglong(value);
printf("%s\n", r);
value = -5201214;
r = strfromlonglong(value);
printf("%s\n", r);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment