I am building a program which will multiply by 2, and reach long accurate numbers. Here I build a program in which each digit is stored in a different variable. When I compile the program, and I type 2^63, It gives me the correct answer. But when I type 2^64, I get a “Segmentation fault”.
What is it? What can I do?
#include <stdio.h>
int main(void)
{
printf("2^n, enter n: ");
int n, array = 1;
scanf("%d", &n);
int num[array];
num[0] = 2;
int remainder = 0;
if (n > 1) {
int counter = 0;
int counter3 = 1;
for (counter3 = 1; counter3 < n; counter3++) {
remainder = 0;
for (counter = 0; counter < array; counter++) {
num[counter] *= 2;
num[counter] += remainder;
remainder = 0;
if (num[counter] / 10 != 0) {
remainder = num[counter] / 10;
num[counter] -= remainder * 10;
if (counter+1 >= array) {
array++;
num[counter+1] = remainder;
remainder = 0;
break;
}
}
}
}
}
int counter2;
for (counter2 = array - 1; counter2 >= 0; counter2--)
printf("%d", num[counter2]);
putchar('\n');
return 0;
}