这两个程序代码到底有什么不同?
这里有两个程序,大家帮我看看,到底有什么不同?怎么运行结果会不一样?正确的程序:
/* Program 4.3 Sum the integers from 1 to a user-specified number */
#include <stdio.h>
int main(void)
{
long sum = 0L; /* Stores the sum of the integers */
int count = 0; /* The number of integers to be summed */
/* Read the number of integers to be summed */
printf("\nEnter the number of integers you want to sum: ");
scanf("%d",&count);
/* Sum integers from 1 to count */
for(int i = 1 ; i <= count ; i++)
sum += i;
printf("\nTotal of the first %d numbers is %ld\n", count, sum);
return 0;
}
错误的程序:
/*数字汇总*/
#include <stdio.h>
int main(void)
{
long sum = 0L;
int count = 0;
printf("\n请输入你想要汇总的整数");
scanf(" %d", &count);
for(int i = 1 ; i <= count ; i++)
sum +=1;
printf("\n%d个数的总和为%ld\n", count, sum);
return 0;
}
当输入10时,结果应为55。可是错误的程序结果还是为10.