#include <stdio.h>
void seperate(int intNum);
int main()
{
int x;
printf("Please enter an integer: \n");
scanf("%d",&x);
seperate(x);
}
void seperate(int intNum)
{
int i,count=0;
while (intNum!=0)
{
intNum/=10;
++count; //to calculate the number of digits for user input = size of array
}
int array[count];
printf("The number on seperate line as follows:\n");
for(i=count-1; i>=0; i--)
{
array[i]= intNum%10;
intNum /= 10;
}
for(i=0; i<=count-1; i++)
{
printf("%d\n", array[i]);
}
}
The expected output(it works when the array size is fixed):
Please enter an integer:
42568
The number on separate line as follows:
4
2
5
6
8
The output:
Please enter an integer:
42568
The number on separate line as follows:
0
0
0
0
0
The code works only if the array sized is fixed, how to solve this problem? Does it not work if I create an array without declaring the size?
EDIT: the value for variable intNum
changed in while loop, nothing to do with the declaration of the array