# Variables Type help

• 12-21-2012
Gal Abir
Variables Type help
Hello guys!

I've wrote a function that takes two numbers and "Connects" them together. Example:

12345, 98766 >>>>> 1234598766.

Here it is:

Code:

```unsigned long long concatenate(unsigned long long x, unsigned long long y) // Checked, works ok!!! {     int counter=0, test, tens=1;     test=y;     while (test>0)     {         counter++;         test/=10;     }     while (counter>0)     {         tens*=10;         counter--;     }     x= (x* tens ) + y;     return x; }```
Now, it works well, except when I try to do it for TWO long numbers....

Example:

if I try to connect:

6611 and 93311267 I get 406333314979.

It's weird because the function itself works, and this new numbers does have 12 digits as it should have, but they're different....

I'd like to know if it's something with the variables perhaps?

Thanks a bunch!
• 12-21-2012
Gal Abir
PS

This function lives here:
Code:

```while (i<3)     {         square= sum_squares(array[i], array[i+3], array[i+6]);         prime = largest_prime_factor(square);         full[i]= concatenate(square, prime);         printf("%ul\n", full[i]);         i++;     }```
I won't say what each function does, but ths printf prints each part of the array, which is unsigned long long... and something weird prints out:

Attachment 12370

I don't know what those 1-like symbols are...

123456789 <<< this is the input.... Then it calculates something, and Outputs the "Square". Then it calculates the highest Prime number which devides the Square, then I use the function to connect them together.

Then I just put in printf to debug it and that's what I saw...
• 12-21-2012
Elkvis
my guess is that int isn't big enough to handle the result. try using long (or even long long if your compiler supports C++11) to store your result.
• 12-21-2012
Gal Abir
Yeah I got that but what type is longer than unsigned long long?
• 12-21-2012
Elkvis
unsigned long long is the largest integral type offered by the standard. the trouble you're having is that your "tens" variable is of type int, and int on most platforms these days is just too small to hold more than 10 decimal digits.
• 12-21-2012
Gal Abir
Quote:

Originally Posted by Elkvis
unsigned long long is the largest integral type offered by the standard. the trouble you're having is that your "tens" variable is of type int, and int on most platforms these days is just too small to hold more than 10 decimal digits.

Not that... I changed my tens to unsigned long long and it still didn't work... I need to find a different way of saving a 12 digits long number...

Maybe I'll try it in an array or something the like...
• 12-21-2012
Elkvis
you can try converting to strings or use double or a library for handling arbitrary precision numbers.
• 12-21-2012
Gal Abir
I see... well I think I can solve it but I have a question (I really suck at pointers).

If I have a function that gets unsigned long long, can I feed it a point to an array?
• 12-21-2012
Elkvis
can you give a code example to explain what you mean?
• 12-21-2012
iMalc
You declared test as an int, so when you do test = y; you are truncating the value to the size of an int. You would need to make test an unsigned long long as well. tens would also need to be unsigned long long, but counter can stay as an int.