Search code examples
cundeclared-identifier

Error: 'Math' undeclared first use in this function


I keep getting this error:

'Math' undeclared first use in this function

although I included the math library.

int x = Math.pow(10.0,(double)k);

This is the line where I get the error, any ideas why?


Solution

  • pow is part of the math library, but there are no namespaces or packages in C.

    So first

    #include <math.h>
    

    then call as:

    int x = pow(10.0,k);
    

    Old versions of gcc require you to add -lm when linking, although this is unnecessary with latest versions (version 6 doesn't require it anymore).

    note that there's no need to cast k as a double, and also, you may have loss of precision by storing your result in an int (truncating)

    Also note that if you're doing that to get an integer power of 10, you'd better use integer multiplication in a loop (there are even better algorithms, but still integers, like The most efficient way to implement an integer based power function pow(int, int))