xxxxxxxxxx
#include <stdio.h>
#include <stdlib.h>
int x = -42;
int length = snprintf( NULL, 0, "%d", x );
char* str = malloc( length + 1 );
snprintf( str, length + 1, "%d", x );
free(str);
xxxxxxxxxx
#include <stdio.h>
#include <stdlib.h>
int main() {
int number = 123456;
char str[20];
sprintf(str, "%d", number);
printf("Number as a string: %s\n", str);
return 0;
}
xxxxxxxxxx
int num = 321;
char snum[5];
// convert num to string and save in string snum
itoa(num, snum, 10);
// print our string
printf("%s\n", snum);
xxxxxxxxxx
int numToConvert = *your number*;
// calculate the length of the resulting string
int ENOUGH = malloc(sizeof(char)*(int)log10(numToConvert));
char str[ENOUGH];
sprintf(str, "%d", 42);
// str contains the number in string form
xxxxxxxxxx
// I know of three methods to do this.
//method 1: '#' is the preprocessor operator that converts to string.
//It is called stringification or stringizing operator.
#define TO_STR(x) #x
char *s=TO_STR(123;
//method 2: sprintf()
char*s;
sprintf(s,"%d",123);
//method 3: itoa(), the third parameter is base, so to convert number to binary, put base as 2.
char*s;
itoa(123,s,10)
xxxxxxxxxx
#include <stdio.h>
#include <stdlib.h>
int main()
{
char stringg[35];
int intt = 907;
//in C sprintf() is Function to Convert an Integer to a String.
sprintf(stringg,"%d",intt);
printf("\nYour string contains the number: %s\n",stringg);
return 0;
}