1,044 views

4 Answers

Best answer
4 votes
4 votes
sum += n++*n++;

Here, the variable 'n' is modified more than once without an intermediate sequence point. So, the output comes under "undefined behaviour" part and whatever code the compiler generate, we cannot say it is wrong. i.e., even if the program outputs "0" compiler is correct and programmer is wrong. 

Before writing C code one must know the rules of C. But people even write C book without knowing the rules of C :)

selected by
0 votes
0 votes
sum=0 n=1

sum=1 n=3

sum=10 n=5

sum=35 n=7

sum=84 n=9

sum=165 n=11
0 votes
0 votes
Now whenever you see a post increment Mark that place ,evaluate the expression and then come back and increment it.

so here. initially n=1,sum=0

while(n<=10)..this is satisfied so it enters the loop

sum=sum+n++*n++

here is the sequence -> sum=0+1*1=>sum=1,n=3

sum=1+3*3=>sum=10,n=5

sum=10+5*5=>sum=35,n=7

sum=35+7*7=>sum=84,n=9

sum=84+9*9=>sum=165,n=11->now at this point the it will exit the loop

so we have sum=165
–1 votes
–1 votes

sum=sum+((n++)*(n++))

++  or postfix is left to right ascociative. So it goes like this

 sum=0  n=1

sum=0+(1*2) =2  n=3

sum=2+(3*4) =14 n=5

sum=14+(5*6) =44 n=7

sum=44+(7*8)=100  n=9

sum=100+(9*10)=190 n=11
 

Loop ends.

Related questions

0 votes
0 votes
1 answer
1
Desert_Warrior asked May 16, 2016
797 views
#include<stdio.h int main() { int a[10][20][30] = {0}; int *b = a; int *c = a+1; printf("%ld", c-b); return 0; }
0 votes
0 votes
1 answer
3
shekhar chauhan asked Jun 4, 2016
461 views
#include <stdio.h void fc(float *); int main() { int i = 10, *p = &i; fc(&i); } void fc(float *p) { printf("%f\n", *p); }
2 votes
2 votes
0 answers
4