"Saif" <sa****@gmail.com> writes:
he******@gmail.com wrote: How can I convert integer, for example 12113, to char array but without
specify an array size in C programming?
You can use dynamically allocated memory. To get the size of the string
needed, you can find the log (base 10) of your number, truncate it, and
add 1.
For example for 12113...
size = floor(log10(12113)) + 1 = 4 + 1 = 5
If I'm going to convert it to an char array I'd use snprintf to
calculate the size:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char* buf = NULL;
int x = 12113;
size_t sz = snprintf(buf, 0, "%d", x) + 1;
buf = malloc(sz);
snprintf(buf, sz, "%d", x);
/* do something with buf */
free(buf);
return 0;
}