Note, TUHS probably isn't the *best* forum to ask for help with basic C programs. :-)

That said, I suspect you mis-transcribed the program and that the line, `sum = sum + 1;` should be `sum = sum + i;`, or more idiomatically, `sum += i;`.  Indeed, the whole program could be written:

#include <stdio.h>

int
main(void)
{
        int i, sum = 0;
        for (i = 1; i <= 100; i++)
                sum += i;
        printf("%d\n", sum);
        return 0;
}

If you're using a C99 or later compiler, you can be slightly more succinct:

#include <stdio.h>

int
main(void)
{
        int sum = 0;
        for (int i = 1; i <= 100; i++)
                sum += i;
        printf("%d\n", sum);
        return 0;
}

Hope that helps!

       - Dan C.


On Wed, Aug 22, 2018 at 8:17 AM cc <caipenghui_c@163.com> wrote:
Hello everyone

I had a problem compiling a piece of c code from the book. The result of running the book is 5050, but the compiler is 100. I don't know which is right, please help me to see which is wrong. Thank you very much!

#include <stdio.h>

int main(void)
{
       int i, sum = 0;
       i = 1;
       while ( i <= 100) {
       sum = sum + 1;
       i++;
       }
       printf("%d\n", sum)
       return 0;
}