这个C代码有什么问题? [英] What is wrong with this C code ?
本文介绍了这个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()
returnsdouble
, start here : C library function pow()[^]
正如Mehdi所说已经告诉过你:你已经将num
声明为int
类型,但它应该是double
。如有疑问,请使用Google查找pow()
函数的文档。
As Mehdi has already told you: You have declarednum
asint
type, but it should bedouble
. If in doubt use Google to find the documentation for thepow()
function.
这篇关于这个C代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文