100内整数相加结果出错
代码如下#include <stdio.h>
int main()
{
int i,sum;
for(i=1;i<=101;i++)
{
sum=sum+i;
}
printf("The sum of all integers between 1 and 100 is %d\n",sum);
}
显示结果为
The sum of all integers between 1 and 100 is 5152
我尝试改错
将 i<=101 改为 i<=100 但得出结果为5051