printf of an indexed array

16 Mar 2010

Hi

If you printf an indexed array on MBED it comes out backwards;-i.e.

int a[3],ind = 0; etc etc

pc.printf("%d %d %d", a[ind++],a[ind++],a[ind++]);

it prints  a[2] a[1] a[0]   - i.e. it evaluates  the index from the far end inwards. I haven't checked fprintf yet.

I don't remember it being like that when I did a lot of C programming on other systems and I don't have another compiler to hand. Is this normal C++ or is it a anomaly in the compiler?  You can obviously get round it, but it just seems strange.

16 Mar 2010

The order of evaluation of function's arguments is not specified in the C standard:

http://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c

Most C compilers push (and evaluate) arguments from the last to first, thus the result you get. To get deterministic behavior, don't use the increment operators in the arguments.