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 fi
If you are using GCC, you can use the GNU extension asprintf function.
char* str;
asprintf (&str, "%i", 12313);
free(str);
If you want to output your structure into a file there is no need to convert any value beforehand. You can just use the printf format specification to indicate how to output your values and use any of the operators from printf family to output your data.
This is old but here's another way.
#include <stdio.h>
#define atoa(x) #x
int main(int argc, char *argv[])
{
char *string = atoa(1234567890);
printf("%s\n", string);
return 0;
}
Use function itoa()
to convert an integer to a string
For example:
char msg[30];
int num = 10;
itoa(num,msg,10);