// 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)
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
int num = 321;
char snum[5];
// convert num to string and save in string snum
itoa(num, snum, 10);
// print our string
printf("%s
", snum);
char s[] = "42";
int num = atoi(s);
Code Example |
---|
C :: dynamically create matrix c |
C :: search array element in c |
C :: how to mutex lock in c |
C :: c language append line to file |
C :: unity set transform position code |
C :: servo motor arduino |
C :: c Program to check if a given year is leap year |
C :: how to empty string in c |
C :: how to print value of pointer in c |
C :: c random array |
C :: connect servo to arduino |
C :: how to make a linked list in c |
C :: strong number in c |
C :: how to print sizes of various data types of C |
C :: .sh template |
C :: c sleep milliseconds |
C :: get float in c |
C :: struct main function c in unix |
C :: signal function c |
C :: fgets c |
C :: c convert string to size_t |
C :: millis() |
C :: c print 2d array |
C :: 2d array in c |
C :: c memcpy array |
C :: c add char to char array |
C :: What should main() return in C? |
C :: C program to calculate the sum of odd and even numbers |
C :: c get pid |
C :: sOY wapo ya lo c |