# math.h pow() not taking variables in arguments

This topic is 3748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This is a C Problem. The Code Bellow Doesn't work it Fires Compile Time Error. Undefined reference to pow'
#include <stdio.h>
#include <math.h>

int main(void){
int i=1;
printf("before for %d\n", pow(4, i));//Error Here
return 0;
}


However the bellow one does compile.
#include <stdio.h>
#include <math.h>

int main(void){
int i=1;
printf("before for %d\n", pow(4, 2));//Error Here
return 0;
}

`

##### Share on other sites
Compiles fine on VC++ 2008 Express (and yes, I did remember to give the file a .c extension). What compiler are you using?

BTW, the return type of pow() is a double, so using %d to print it is incorrect. Use %lf %f or %g instead.

[Edited by - Gage64 on August 16, 2008 6:10:37 AM]

##### Share on other sites
This is a peculiarity of your compiler. You should link in the Math library. Some compilers do this automatically and some do not. I don't know why the second piece of code should work if the first one doesn't.

Also remember that pow() returns a double, which mean that you cannot format it with "%d". Use "%f" or "%g" instead, or cast the result to int before you give it to printf.

##### Share on other sites
Quote:
 Original post by Gage64Use %lf instead.

%lf is for long double, %f is for double.

##### Share on other sites
Quote:
Original post by DevFred
Quote:
 Original post by Gage64Use %lf instead.

%lf is for long double, %f is for double.

##### Share on other sites
If you are using gcc you need to link to libm.so or libm.a:
gcc -lm -o test test.c

Since pow(4, 2) is a compile time constant, the compiler might replace it with 16.0 eliminating the pow and the need of linking to the math library.

##### Share on other sites
Quote:
 Original post by KambizIf you are using gcc you need to link to libm.so or libm.a:gcc -lm -o test test.cSince pow(4, 2) is a compile time constant, the compiler might replace it with 16.0 eliminating the pow and the need of linking to the math library.

I am using gcc. I'll try your solution.
However I dont think that there is a typying problem. I've used different Variants too. However It was a quick post.

##### Share on other sites
However I forgot how to add -lm option on KDevelop anybody help plese.

Edit:

Thanks I've fixed I need -lm Option.

##### Share on other sites
From the KDevelop 3.3.x F.A.Q:

### How to link against a .so library?

When your project is GNU Autotools (Autoconf/Automake) based then you are most probably using the KDevelop Automake manager (see FAQ#Where is the Automake manager in KDevelop?).

• select the subproject you want to add the library to on the top view of the AutoMake manager.
• select Options... with the RMB or hit the wrench icon
• on the libraries ("compiler") tab add libs in the second box (link libraries outside of project). Just enter the lib name, without the lib prefix and the .a or .so or .la or what ever ending.
• i.e if your lib is libsomelib.so, then you add it so: -lsomelib

• If that library is not part of your system wide library path add <code>-Lpath/to/the/library/you/used</code> to the <code>LDFLAGS</code> in your Project->Project Options...->Configure Options->Linker Flags(LDFLAGS):.

Do not forget to add the the location of the library's .h files to the include path of your executable like it's explained in FAQ#How to add an external include path to a project?

1. 1
Rutin
54
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633412
• Total Posts
3011737
• ### Who's Online (See full list)

There are no registered users currently online

×