这个C程序使用函数有什么问题? [英] What is wrong with this C program using function?
本文介绍了这个C程序使用函数有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
//To find the power of a number
#include<stdio.h>
#include<conio.h>
int power(int a,int b);
int main()
{ clrscr();
int a,b;
printf("\nEnter the base:");
scanf("%d",&a);
printf("\nEnter the power:");
scanf("%d",&b);
printf("\nThe answer is:%d",power(a,b));
getch();
return 0;
}
int power(int x,int y)
{ int c;
for(int i=1;i<=y;i++)
c*=x;
return c;
}
当我输入基数和指数时,我得到了一个无法回答的答案。例如,当我输入2表示基数而3表示指数时,而不是获得8,我得到一个数字很多的数字。请帮我。谢谢。
When I enter the base and exponent, I get an irrevelant answer. For example, when I type 2 for base and 3 for exponent, instead of getting 8, I get a number with many digits. Please help me. Thanks.
推荐答案
在C / C ++中,未初始化的变量具有未定义的值...原因是用于存储变量的内存块(变量名称)实际上是一个指向内存地址的指针)过去可能已被其他变量使用并且从未清理过......
因此,如果变量的初始值很重要(就像在你的情况下那样)你有进行初始化...
在Microsoft编译器中将警告级别设置为4将提醒您可能未初始化的变量...
在GCC中,您可以使用-Wyybe -ininitialized
做同样的事情......
In C/C++ a variable that not initialized has undefined value...The reason is that the memory block used to store the variable (the variable name is actually a pointer to a memory address) may had used in the past by other variables and never cleaned...
So if the initial value of a variable is important (like in your case) you have to do that initialization...
In Microsoft compilers setting warning level to 4 will alert you about potentially uninitialized variables...
In GCC you may use-Wmaybe-uninitialized
to do the same...
这篇关于这个C程序使用函数有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文