C --> how can I get bigger variables sizes?

Programming, for all ages and all languages.
Post Reply
0Scoder
Member
Member
Posts: 53
Joined: Sat Nov 11, 2006 8:02 am

C --> how can I get bigger variables sizes?

Post by 0Scoder »

I am currently coding a program that does some manipulating of the fibonacci series, but I have had problems with numbers being to large for the variable they are held in! I have attached the coe here. Is there a way someone can suggest that I can store the rest of the sequence without an overflow (I need to go up to 100).

Code: Select all

#include <stdio.h>

int main()
{
//create cariables
   unsigned long long sequence[100];
   int current_n;
   int i;
   char input[256];
   
//initialise variables
   sequence[0]=0;
   sequence[1]=1;

   for(i=2; i<25; i++)
    sequence[i]=sequence[i-1]+sequence[i-2];
   
   for(i=2; i<25; i++){
    printf("%d \n",sequence[i]);}

   scanf("%s", input);
 return;   
}
Thanks in advance,
OScoder
QuiTeVexat

Re:C --> how can I get bigger variables sizes?

Post by QuiTeVexat »

Make some structs to represent numbers with all the precision you need, and then write some functions to manipulate them. For this, add() and print() would probably suffice. Set your array of structs up, and blissfully add() and print() them to your heart's content. Not very elegant, but it'll work.

Oh, and *please* return 0; Your compiler should be complaining. Whatever anyone tells you, main() is does *not* have void return type.

Pax tecum,
Qui te vexat.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C --> how can I get bigger variables sizes?

Post by Solar »

Check out whether any of the available bigint libs (e.g. http://www.eskimo.com/~eresrch/fast_onb/bigint.c) suits your tastes.
Every good solution is obvious once you've found it.
Post Reply