for循环内的类型声明问题
我用的是Turbo C 2.01 英文版,编译一个for循环时,for循环内声明了,却编译不过去,具体是for(int i = 0;i<=count;i++)里面的int有问题;程序具体如下:#include <stdio.h>
int main(void)
{
long sum = 0L;
int count = 0;
printf("\nEnter the number of integers you want to sum:");
scanf("%d",&count);
for(int i = 1;i<=count;i++)
sum +=i;
printf("\nTotal of the first %d numbers is %ld\n",count,sum);
return 0;
}
因为i是for循环内的,for结束后i应给被撤销了,但编译时却说没在main()函数里定义。如果改一下却能够通过,不知道为什么啊,请教一下。改过之后的如下:
#include <stdio.h>
int main(void)
{
long sum = 0L;
int count = 0;
int i = 0;
printf("\nEnter the number of integers you want to sum:");
scanf("%d",&count);
for(i = 1;i<=count;i++)
sum +=i;
printf("\nTotal of the first %d numbers is %ld\n",count,sum);
return 0;
}
但是如过是这样的话,i不是成了全局变量了吗?请解释下,谢谢