# turning the digits of a number into an array [closed]

int main()
{
int x,numDigits,i,y,a,diff;
scanf("%d",&x);

a=x;
int result = 1;
int base=10;
numDigits=0;

do
{
++numDigits;
x = x / 10;
} while ( x>0 );

for(result=1;numDigits>0;--numDigits){
result *= base;
i=result;

y=a%i;
while(y>10){
y=y/10;
}
printf(" %d",y);
}
return 0;
}


I manage to seperate integer's digits one by one. But I was not able to collect them in an array. Even though with limited numbers without taking input from user it is easier, I also want to process these numbers but I got stuck.

• Welcome to Code Review! Since we require that the code is working as intended to the best of your knowledge, your code is unfortunately not yet ready for a review. Once everything is working, feel free to come back to get feedback on best practices and improvements. – AlexV Nov 13 '19 at 22:03
• There appear to be some magic numbers in the code. Do you mean base everywhere you use 10? Would you please comment your variables so that we know what they are supposed to represent and please comment your code so that we know what you are trying to do? The code makes no attempt "to collect them in an array", no array is declared. As it stands, this code is not ready for review. – Peter Jennings Nov 14 '19 at 0:46

You can use the standard C function sprintf(buffer,"%d",x) to get an array of digits. In this case, the buffer will contain big-endian representation of x, so the conversion to a digit from each char will be something like this

for(int i = 0; buffer[i] != '\0' && i < sizeof(buffer);++i)
digits[i] = buffer[i] - '0';


This work if x is positive.

But if you want to make your own implementation of splitting a number into digits, you should analyze the problem:

1. What is the base - is it always 10?
2. How many digits could be - is it always int, i.e. mostly 32-bit?

The answer for first question is to have the base parameter int base = 10 or 2 or 3 or 8 or any you want.

The answer for second question is ceil( log(|x| + 1) / log(base) ) + 1. The +1 for the minus sign. Or if you didn't want to care about maximum of digits use char digits[128] - more than enough even for 64-bit integer.

So, how to fill digits?

// int x - somewhere
unsigned base = 10;

char digits[128];
char isNegative = 0;
int digit = 0;

unsigned tmp = x; // this fix INT_MIN problem thanks chux - Reinstate Monica for note
if(x < 0) {
isNegative = 1;
tmp = -x;
}
if(x == 0) {
digits[0] = 0;
digit = 1;
} else {
for(digit = 0; tmp > 0; ++digit) {
digits[digit] = tmp % base;
tmp /= base;
}
}


At least you got little-endian representation of x on base as digits[i], where 0 <= i < digit and isNegative for the minus sign

• Welcome to Code Review! Please refrain from answering low-quality questions that are likely to get closed. Once you've answered, that limits what can be done to improve the question, making it more likely that your efforts are wasted. It's better to wait until the question is properly ready before you answer! – Toby Speight Nov 14 '19 at 9:35
• Yes, @chux-ReinstateMonica! Many thanks. My C is a little rusty. Also, I missed case with x==INT_MIN. Fixed now. – mr NAE Nov 18 '19 at 8:22
• Better, yet -x is still UB when x==INT_MIN. Suggest bool isNegative = x < 0; unsigned tmp = isNegative ? 0u - x : x; or the like. The key is that int math should not attempt -INT_MIN. – chux - Reinstate Monica Nov 18 '19 at 13:29
• Did you mean UB if sizeof(tmp)>sizeof(int), when tmp type will changed to unsigned long long? – mr NAE Nov 18 '19 at 18:06