Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中的pow函数_C_Gcc_Pow_Math.h - Fatal编程技术网

C语言中的pow函数

C语言中的pow函数,c,gcc,pow,math.h,C,Gcc,Pow,Math.h,我写了一个C代码,它有来自math.h库的幂函数。当我编译我的程序时,我收到一个错误,它是“未定义对‘pow’函数的引用”,我使用gcc编译器(Fedora9)编译我的程序 我将-lm标志插入gcc,然后,省略错误,但pow函数的输出为0 #include<math.h> main() { double a = 4, b = 2; b = pow(b,a); } #包括 main() { 双a=4,b=2; b=功率(b,a); } 有人能帮我吗?我的编译器有问题吗 谢谢 您的程

我写了一个C代码,它有来自math.h库的幂函数。当我编译我的程序时,我收到一个错误,它是“未定义对‘pow’函数的引用”,我使用gcc编译器(Fedora9)编译我的程序

我将-lm标志插入gcc,然后,省略错误,但pow函数的输出为0

#include<math.h>
main()
{
double a = 4, b = 2;
b = pow(b,a);
}
#包括
main()
{
双a=4,b=2;
b=功率(b,a);
}
有人能帮我吗?我的编译器有问题吗


谢谢

您的程序不输出任何内容

您所指的0可能是退出代码,如果您没有从
main
显式返回,则退出代码将为0

尝试将其更改为符合标准的签名并返回
b

int main(void) {
  ...
  return b;
}
请注意,返回值基本上限制为8位信息,因此非常非常有限

使用
printf
显示值

#include <stdio.h>
...
  printf("%f\n", b);
...
#包括
...
printf(“%f\n”,b);
...

必须使用浮点转换说明符(
f
g
e
)打印
双值。您不能使用
d
或其他方法来期望一致的输出。(这实际上是未定义的行为。)

您缺少将值打印到stdout的printf行。 试试这个:

#include <stdio.h>
#include <math.h>

int main() {
        double a=4, b=2, c;

        c = pow(b,a);
        printf("%g^%g=%g\n", a,b,c);
        return 0;
}

对于寻求这一答案的所有其他人:

这不起作用

gcc my_program.c -o my_program
gcc my_program.c -o my_program -lm
它将产生如下结果:

/tmp/cc8li91s.o: In function `main':
my_program.c:(.text+0x2d): undefined reference to `pow'
collect2: ld returned 1 exit status
这将起作用

gcc my_program.c -o my_program
gcc my_program.c -o my_program -lm

关于基数和指数,这里存在混淆。这并不明显,因为2^4和4^2都等于16

void powQuestion()
{
    double a, b, c;

    a = 4.0;
    b = 2.0;
    c = pow(b, a);

    printf("%g ^ %g = %g\n", a,b,c);        // Output: 4 ^ 2 = 16

    a = 9.0;
    b = 2.0;
    c = pow(b, a);

    printf("%g ^ %g = %g\n", a,b,c);        // Output: 9 ^ 2 = 512  >> Wrong result; 512 should be 81 <<


    // K & R, Second Edition, Fifty Second Printing, p 251: pow(x,y) x to the y

    double x, y, p;

    x = 9.0;
    y = 2.0;
    p = pow(x, y);

    printf("%g ^ %g = %g\n", x, y, p);      // Output: 9 ^ 2 = 81


    // even more explicitly

    double base, exponent, power;

    base = 9.0;
    exponent = 2.0;
    power = pow(base, exponent);

    printf("%g ^ %g = %g\n", base, exponent, power);    // Output: 9 ^ 2 = 81
}
void-powQuestion()
{
双a、b、c;
a=4.0;
b=2.0;
c=功率(b,a);
printf(“%g^%g=%g\n”,a,b,c);//输出:4^2=16
a=9.0;
b=2.0;
c=功率(b,a);

printf(“%g^%g=%g\n”,a,b,c);//输出:9^2=512>>错误的结果;512应该是81错误被忽略,但输出是0什么输出?你没有打印任何东西。@cnicutar-可能OP是指
pow()
的返回值?我使用printf(“%d\n”,b);打印值。包含错误的是printf行。请确保您发布了所有代码。正确答案可能重复。我猜OP使用了错误的printf说明符。我使用printf显示b值。b值为0。我使用%d说明符。如果您像我上面所说的那样使用printf,您将看到
16.0000…
.
%d
用于整数类型。@cnicutar:您的水晶球非常结实。这一点在问题本身中已经说明过了。(在许多其他Qs和堆栈溢出中也是如此。)但这为什么有效呢??