免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3036 | 回复: 8
打印 上一主题 下一主题

for(int i=0;i<100;i++) does not work? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-18 21:53 |只看该作者 |倒序浏览
I can't complie this code

  1. #include <stdio.h>
  2. int main()
  3. {
  4.    int sum;
  5.    for(int i=1;i<100;i++)
  6.       sum+=i;
  7.    printf("sum is %d",sum);
  8. }
复制代码

I want to know if the c complier has changed or something else ?
thanks for any helps ....
B.T.W:
I should have written in  Chinese But I can't type chinese in my freebsd sytem  sorry.

论坛徽章:
0
2 [报告]
发表于 2007-01-18 21:57 |只看该作者
int sum = 0;

论坛徽章:
0
3 [报告]
发表于 2007-01-18 22:17 |只看该作者
%gcc -o loop loop.c
loop.c: In function `main':
loop.c:5: error: 'for' loop initial declaration used outside C99 mode
%cat loop.c
#include <stdio.h>
int main()
{
        int sum = 0;
        for(int i=1;i<=100;i++)
                sum+=i;
        printf("sum is %d",sum);
        return(0);
}

论坛徽章:
0
4 [报告]
发表于 2007-01-18 22:22 |只看该作者
====

  1. --- int sum;
  2. --- for(int i=1;i<100;i++)
  3. +++ int sum, i;
  4. +++ for (i=1; i<100; i++)
复制代码


另外好心地推测一下,这里很可能是 <= 100,不是小于。

====

论坛徽章:
0
5 [报告]
发表于 2007-01-18 22:23 |只看该作者
原帖由 eachstep 于 2007-1-18 22:17 发表
%gcc -o loop loop.c
loop.c: In function `main':
loop.c:5: error: 'for' loop initial declaration used outside C99 mode
%cat loop.c
#include <stdio.h>
int main()
{
        int sum = 0;
...

哦,是C程序啊,定义int i;应该放到声明的地方(函数前面放声明,后面语句)

PS:习惯了C++程序,刚才没看出来,呵呵

论坛徽章:
0
6 [报告]
发表于 2007-01-18 22:24 |只看该作者
原帖由 langue 于 2007-1-18 22:22 发表
====

  1. --- int sum;
  2. --- for(int i=1;i<100;i++)
  3. +++ int sum, i;
  4. +++ for (i=1; i<100; i++)
复制代码


另外好心地推测一下,这里很可能是 <= 100,不是小于。

====

开始想说来着,不想打字,呵呵

论坛徽章:
0
7 [报告]
发表于 2007-01-18 22:30 |只看该作者
xiexie la !

论坛徽章:
0
8 [报告]
发表于 2007-01-18 22:43 |只看该作者
晕倒。。。C99的标准才支持for 中变量定义

论坛徽章:
0
9 [报告]
发表于 2007-01-19 11:16 |只看该作者
ls正解
gcc ..... -std=c99
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP