In C language i want to store the value of variable integer to another variable as string. How can i achieve this?
Example :
int num1 = 123456789;
I want to store the value of num1 as string in another variable.
In C language i want to store the value of variable integer to another variable as string. How can i achieve this?
Example :
int num1 = 123456789;
I want to store the value of num1 as string in another variable.
The typical approach to determining the size of the string that you will need to store the value is to use snprintf
twice. The first time, you pass a NULL pointer (the frist argument is actually irrelevant, but passing NULL makes it clear to the reader what is happening) with a zero limit so that no data is written, but you use the return value to learn the needed size. Then you allocate space and write the data. eg:
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
long long digit = 1234567890;
size_t len = snprintf(NULL, 0, "%lld", digit);
char *ptr = malloc(len + 1);
if( ptr == NULL ){
perror("malloc");
return 1;
}
sprintf(ptr, "%lld", digit);
printf("%s\n", ptr);
return 0;
}
Another completely reasonable approach, indeed perhaps more reasonable, is to simply allocate a big buffer. eg, just do char s[1024]
and skip the malloc completely. If you want, you can check system limits at compile time and use a buffer that is tuned to the needed size, but there's really no advantage to doing that. By allocating 1024 bytes, you run the risk of your program failing when computers are large enough that the maximum value of a long long is more than 1023 characters when written as a string, but that shouldn't be a concern for quite some time. If that day occurs and a computer is built with a long long that has over 3000 bits, some trivial defensive programming makes it not a big deal:
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
long long digit = 1234567890;
size_t len = snprintf(NULL, 0, "%lld", digit);
char a[1024];
char *ptr = a;
if( snprintf(ptr, sizeof a, "%lld", digit) >= sizeof a ){
fprintf(stderr, "Value truncated\n");
return 1;
}
printf("%s\n", ptr);
return 0;
}
If you are required avoid to a 2 pass conversion of first finding the string size needed and then allocating the size, consider a 1 step approach that starts with a worst case size.
To form a string, first determine the maximum size of character array needed.
Number of decimal digits can be approximated with knowing the number of binary value bits in an int
or sizeof(int)*CHAR_BIT - 1
scaled by log102 or 0.301...., which is just less than 28/93*1.
Thus the digits needed is not more than (sizeof(int)*CHAR_BIT - 1)*28/93 + 1
. Adding 1 for the potential sign bit and 1 for the null character, we arrive at:
#define LOG10_2_N 28
#define LOG10_2_D 93
#define INT_STRING_SIZE ((sizeof(int)*CHAR_BIT - 1)*LOG10_2_N/LOG10_2_D + 3)
char buf[INT_STRING_SIZE];
Now print into that.
int len = snprintf(buf, sizeof buf, "%d", num1);
assert (len > 0 && (unsigned) len < sizeof buf);
// Allocate a copy if desired.
return strdup(buf);
*1 Rare encoding will also have padding bits, yet that only makes our buffer size a tad too high. Rarely is this a concern.
The hint from https://cplusplus.com/reference/cstdlib/itoa/ might help. malloc(sizeof(int)*8+1)
. Use sprintf
after malloc then.
sprintf converts data type to string
log10(abs(digit)) works for negative but we have to add 1 extra bit for negative bit so,
len = (log10(abs(digit))) + 2
And for positive integers len = (log10(abs(digit))) + 1
if the integer is 0 so, for integer = 0 the len = 1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main() {
long long digit = 1234567890;
//size required to store the string
size_t len;
if(digit < 0) {
len = (log10(abs(digit))) + 2;
} else if(digit > 0){
len = (log10(abs(digit))) + 1;
} if (digit == 0) len = 1;
char *ptr = malloc(len);
//sprintf converts to string
sprintf(ptr,"%lld",digit);
printf("%s",ptr);
}