I used pow() function with a variable and I found some error
Well, this kind of things are not working with pow() functions.. Is there any way to solve this issue.Code:for (i=0;i<3;i++) pow(5,i);
I used pow() function with a variable and I found some error
Well, this kind of things are not working with pow() functions.. Is there any way to solve this issue.Code:for (i=0;i<3;i++) pow(5,i);
Post your error messages.
Also, where do you assign the result of calling pow() ?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
What does "some error" mean? We can't "solve this issue" if you do not explain what it is.
The code you posted will produce 1.0, 5.0, and 25.0, which is correct.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
What is the value of i when that loop starts
What is 5 ^ 0?
Also what variable types does pow expect? And what are you handing it?
Apart from the above, there needs to be an #include <math.h> in the source file before using pow().
Some implementations (compiler, linker, IDE, etc) do not link in floating point support (sometimes described as math support) by default - so will trigger error messages when linking, even if the source file is correct. If you have one of those you will need to read the documentation for your implementation to find out how to link in math support.
@OP:64 posts on this board and you still post this garbage?
My advice, turn off your computer and throw it out the window. You will never accomplish anything if you cannot at a minimum figure out How to ask a question.
I compiled the following code..
# include <stdio.h>
# include <math.h>
got this error.Code:int main(void) { int n=5; printf("%f",pow(3+sqrt(5),n)); }
when I place an number instead of a variable in 2nd argument of pow() I'm not getting error.Code:/tmp/ccgmJtjc.o: In function `main': test.c:(.text+0x23): undefined reference to `pow' collect2: ld returned 1 exit status
You need to add -lm to the compilation command.
Well you need to compile with
gcc prog.c -lm
to link with the math library.
The reason it works with a constant is because the compiler can see what you're doing, and optimise out an actual call to pow()
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
@grumpy :
if I compile according to
its showing
test.c: In function ‘main’:
test.c:8: warning: incompatible implicit declaration of built-in function ‘pow’
test.c:8: warning: incompatible implicit declaration of built-in function ‘sqrt’
/tmp/ccpaH4qk.o: In function `main':
test.c.text+0x23): undefined reference to `pow'
collect2: ld returned 1 exit status
What header files are you including? You need at least stdio.h and math.h.
Jim
Actually.. grumpy was saying not to mention math.h .. so I was telling her the output what I got without math.h..