Im learning C right now and I've got this problem I want to convert the integer into an array my code works but the problem is that I declare the size of an array in the beginning and I want to make it so that it works for every integer basically.
#include<stdio.h>
int main()
{
int x,i,temp;
int arr1[6];
scanf("%d",&x);
for (i=5;i>=0;i--){
temp=x%10;
arr1[i]=temp;
x/=10;
}
for (i=0;i<=5;i++){
printf("%d",arr1[i]);
}
return 0;
}
Can you please help me?
I'm trying to find solution for the problem.
For starters your code does not take into account that the user can enter a negative number.
If you are not going to deal with negative values then declare the variable
xas having an unsigned integer type as for exampleunsigned int.As for your problem then you can use a variable length array.
For example
If your compiler does not support variable length arrays then you can allocate an array dynamically using standard function
mallocdeclared in header<stdlib.h>as for example