What is the difference between these two codes ?

I tried these two pieces of code:

int b[3] ;
int a[12] ;
int j=7 ;
int c = a[j]*b[0] + a[j+1]*b[1] + a[j+2]*b[2] ;
int b[3] ;
int a[12] ;
int j=7 ;
int c = a[j++]*b[0] + a[j++]*b[1] + a[j]*b[2] ;

The first one worked, and the second one didn't. Should the second one work ?

well your answers would be totally different with the two statements. int b[3] ; int a[12] ; int j=7 ; int c = a[j]*b[0] + a[j+1]*b[1] + a[j+2]*b[2] ; int c = a[7]*b[0] + a[7+1]*b[1] + a[7+2]*b[2] ;

int c = a[j++]*b[0] + a[j++]*b[1] + a[j]*b[2] ; int c = a[8]*b[0] + a[9]*b[1] + a[9]*b[2] ;

this might be the same answer

int c= (a[j] * b[0]) + (a[j++]*b[1]) + (a[j++]*b[2]);

The second one should not work. There is no sequence point in the expression that guarantees the value of j has been updated. See... http://en.wikipedia.org/wiki/Sequence_point

    int b[]={1,2,3};
    int a[]={9,8,7,6,5,4,3,2,1};
    int p=2 ;
    
    int c = a[p++]*b[0] + a[p++]*b[1];
    
    fprintf(stdout,"p= %d  c=%d \n", p,c );

This one prints p=4 c=21. I can't find any plausible scenario in which you can get 21 from this. I would have expected 19, originally. Even if you evaluate the right operand first, you don't get 21. The only way I can see to get 21, is if the element 7 from the a[] is used twice.

    int b[]={1,2,3};
    int a[]={9,8,7,6,5,4,3,2,1};
    int p=2 ;
    
    int c = a[p]*b[0] + a[++p]*b[1];
    
    fprintf(stdout,"p= %d  c=%d \n", p,c );

This one prints 19, which seems to be right 7x1 + 6x2

It's the sequence point thing. You can't have p and p++ or ++p in the same line because you don't know exactly how it's evaluated. It might be taking p into a register and applying it anywhere there is a p in the line of code not realizing that p changes halfway through the line.

Make it three separate lines of code and it will work. There's no prize I'm aware of for squeezing the most stuff into one line of code.

Delta_G: Make it three separate lines of code and it will work. There's no prize I'm aware of for squeezing the most stuff into one line of code.

It's the annual APL trophy. Last year, it was won by someone who wrote a multitasking OS and a native browser, all on one line.