Created
September 17, 2013 02:44
-
-
Save itsyarkee/6589484 to your computer and use it in GitHub Desktop.
convert long long to string in C
This file contains 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.
Learn more about bidirectional Unicode characters
#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