这个C代码有什么问题? [英] What is wrong with this C code ?

查看:94
本文介绍了这个C代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用过幂声明。但它不能正常工作,因为每当我输入(编译后)5提升到2,它应该是数学上的25。但它显示24.为什么会这样?它做得很好,当我做2提升到3 = 8 .....



我尝试过:



I have used power statement. But it is not working fine because whenever I type (after compilation)that 5 raised to power 2, It should be 25 mathematically. But it is showing 24. Why is it so? It works fine when I did 2 raised to power 3=8.....

What I have tried:

#include <stdio.h>
#include <math.h>
int main ()
{
    int a,b;
    printf ("Enter any value.\n");
    scanf ("%d",&a);
    printf ("Enter the number to which it should be raised.\n");
    scanf ("%d",&b);
    int num;
    num=pow(a,b);
    printf ("The result is %d",num);
    return 0;
}

推荐答案

pow()返回 double ,从这里开始: C库函数pow() [ ^ ]
pow() returns double, start here : C library function pow()[^]


正如Mehdi所说已经告诉过你:你已经将 num 声明为 int 类型,但它应该是 double 。如有疑问,请使用Google查找 pow()函数的文档。
As Mehdi has already told you: You have declared num as int type, but it should be double. If in doubt use Google to find the documentation for the pow() function.


这篇关于这个C代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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