问题下使用POW() [英] Problem using pow() in C

查看:147
本文介绍了问题下使用POW()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么用C code以下工作位:

Why does the following bit of code work in C:

int res = pow(2, 3);
printf("%d\n", res);

而这个其他的没有?

while this other doesn't?

int a = 2;
int b = 3;

int res = pow(a, b);
printf("%d\n", res);

即使我尝试

double a = 2;
double b = 3;

double res = pow(a, b);
printf("%f\n", res);

我得到一个

未定义的引用'战俘'

我在做什么错了?

推荐答案

在它的工作原理,这是因为计算是由编译器本身进行(并包含在二进制,如果你写出来)

When it works, it's because the calculation was done by the compiler itself (and included in the binary as if you wrote it out)

printf("8\n");

当它不工作,是因为 POW 函数包含在数学库和数学库不与您的二进制文件默认情况下链接。

要获得数学库链接,如果你的编译器是gcc,用

When it doesn't work, is because the pow function is included in the math library and the math library isn't linked with your binary by default.
To get the math library to be linked, if your compiler is gcc, use

gcc ... -lm ...

使用其他的编译器,应该是相同的:)结果
但阅读文档

With other compilers, should be the same :)
but read the documentation

这篇关于问题下使用POW()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆