I am trying to create a program to transform a integer
into string
:
#include <stdio.h>
char int_to_char(int num){
int i=0;
for(int a=num; a!=0; i++) a/=10;
char s[i+1];
for(int a=num; i!=0; i--){
s[i-1] = (a % 10) + 48;
a/=10;
}
return *s;
}
int main(void){
char a = int_to_char(123);
printf("%c\n", a);
return 0;
}
What it does is:
- Calculate the length of the number.
- create an array
s
with that length. - fill
s
with the ascii of each number.
The problem is that it returns only the first digit of the array s
, how do I make it return the entire array?