I'm pretty new to programming and I was just playing around coding today trying to use functions and I made a simple piece of code that worked a math problem I had in one of my classes. it basically takes the formula (bacteria amount) * 2^hours and calculates that.
My issue is that when I get a really large number it doesn't return correctly, I always get -2147483648 back after a certain size of number. I'm guessing this has something to do with overflow but i'm not 100% sure how that works. What i'm trying to figure out is how to get the actual numbers i'm looking for after I hit this overflow. So what can I do to deal with the overflow?
I initially was only setting everything to int, but after some reading thought maybe changing everything over to long might help me out, but it didn't, if this is a terrible thing for me to do go ahead and let me know! Also the test numbers i'm using are 1500 and 24, that always returns the number above.
Here's the code Thanks!
#include<stdio.h>
#include<math.h>
long bacteria(long b, long h);
int main(void)
{
long f,g;
scanf("%ld%ld",&f,&g);
f = bacteria(f,g);
printf("%ld\n",f);
return 0;
}
long bacteria(long b,long h)
{
long d;
printf("%ld %ld\n",b,h);
d = b * (pow(2,h));
return d;
}