Let's say I have a large array and I'm doing something like the following:
for (int i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
printf("arr[%d]=%d\n", i, arr[i]);
Of course, the sizeof stuff shouldn't be calculated every time so it should be like this instead:
size_t len = sizeof(arr)/sizeof(arr[0]);
for (int i = 0; i < len; ++i)
printf("arr[%d]=%d\n", i, arr[i]);
My questions is whether it can be assumed that any compiler will automatically do the above optimization and so it doesn't matter which approach I use? Or should I assume that that's not the case and the second approach is the only correct one.