During programming, I came up with an unusual error. When I initialize an integer in a loop, sometimes it says that the expression is invalid, but sometimes it takes it. This is my code that gives an error:
int pow(int x,int n);
int main()
{
int x,n,result;
printf("Enter a number:\n");
scanf("%d",&x);
printf("Enter its power:\n");
scanf("%d",&n);
result=pow(x,n);
printf("Result is %d\n",result);
getch();
return 0;
}
int pow(int x,int n)
{
for(int i=1;i<n;i++)
x=x*i;
return x;
}
While I change it like this:
int pow(int x,int n)
{
int i;
for(i=1;i<n;i++)
x=x*i;
return x;
}
source
share