C语言一道题目int count = 3;int main() {int i,sum = 0; for(i = 0,sum = 0;i < count;i += 2,count++){static int count = 4;printf("i = %d,count = %d",i,count);sum += count++;printf(" count = %d,sum = %d\n",count,sum);}sum += count++;printf("count =
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/06 08:37:55
C语言一道题目int count = 3;int main() {int i,sum = 0; for(i = 0,sum = 0;i < count;i += 2,count++){static int count = 4;printf("i = %d,count = %d",i,count);sum += count++;printf(" count = %d,sum = %d\n",count,sum);}sum += count++;printf("count =
C语言一道题目
int count = 3;
int main() {
int i,sum = 0;
for(i = 0,sum = 0;i < count;i += 2,count++)
{
static int count = 4;
printf("i = %d,count = %d",i,count);
sum += count++;
printf(" count = %d,sum = %d\n",count,sum);
}
sum += count++;
printf("count = %d,sum = %d\n ",count,sum);
}
输出结果是7 21
貌似运行过程是 sum += count++;时用的static,for循环里用的是全局
不知事实是怎么样的?为什么?
C语言一道题目int count = 3;int main() {int i,sum = 0; for(i = 0,sum = 0;i < count;i += 2,count++){static int count = 4;printf("i = %d,count = %d",i,count);sum += count++;printf(" count = %d,sum = %d\n",count,sum);}sum += count++;printf("count =
static int count = 4; 这个count是在for循环中定义的,他的作用于只限于for循环中,因此:for(i = 0,sum = 0;i < count;i += 2,count++);以及倒数第二句的sum += count++;处的count都是全局变量的count,而循环体内部的count每次循环都会重新定义一边,而且值初始化为4;下面是三次for循环的过程:
i=0 sum=0 count=3;
sum=5;
i=2 sum=4 count=4;
sum=10
i=4 sum=8 count=5
sum=15
i=6 sum=12 count=6
count=7 sum=21