Search code examples
ctype-conversionpowexponent

Type-Casting in C from Float to Int results in wildly different number, why?


INPUT:

#include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
        printf("%i", (int)pow(10,10));
        
        return 0;
    }

OUTPUT:

-2147483648

For some reason, pow() results in a double as opposed to an int when I run it(I am using Pow() because for some reason, exponents are not coded into C). Any reason as to why?


Solution

  • The maximum value a 4-bytes integer could hold is ranged from -2,147,483,648 to 2,147,483,647. The result of 10^10 is 10,000,000,000, which exceeds the limit and the conversion overflows which gives unexpected results. That's all.