llz127 发表于 2017-1-23 16:53:33

x=2,求当n=1,2,……,10时,x的n次方的值

#include <stdio.h>
int x,n;
int power(x,n)
{
        int i,p=1;
        for(i=1;i<=n;i++)
        p=p*x;
        return p;
}
int main()
{
        int j;
        printf("\n\tpower(2,n)\n");
        for(j=1;j<=10;j++)
        printf("%d\t%d\n",power(2,j));
}
大神麻烦看一下,我用的Dev,一直出错,实在不会改这个。在线跪求{:9_221:}
expression list treated as compound expression in initializer [-fpermissive]
expected ',' or ';' before '{' token
In function 'int main()':
'power' cannot be used as a function

从小爱吃好丽友 发表于 2017-1-23 23:19:34

没看懂你要写的T^T就哪怕声明全局变量,你也要有值==

人造人 发表于 2017-1-24 02:38:24

最好截一下图

vs2015 可以运行,但是有警告
printf("%d\t%d\n",power(2,j)); //这是要干嘛?
printf("%d\n",power(2,j)); //这样不行吗?


zealstar 发表于 2017-2-2 14:09:23

expression list treated as compound expression in initializer [-fpermissive]
表达式列表和复合初始化合并(大概是指函数的形参使用的是全局变量)
expected ',' or ';' before '{' token
在"{"表示前缺少预期的","或者";"(可能是缺少函数声明造成的)
In function 'int main()':
'power' cannot be used as a function
在main函数中:"power"不能用作函数名(关键字占用)

程序的问题:
1、x,n为什么要定义为全局变量?本来只是函数的形参,没有必要。
2、printf("%d\t%d\n",power(2,j)); //如楼上所说,多了个%d
3、没有函数声明语句
页: [1]
查看完整版本: x=2,求当n=1,2,……,10时,x的n次方的值