There are 3 loops, which last loop does not behave as expected. Loop #2 and loop #3 are bad code styling. They are here just for demonstration. Question is why printf() in loop #3 gives unexpected output and printf() in loop#2 gives expected result?? Is this error because compiler or because printf function? Is there any documents which explain how compiler or some function behave in certain situation so I can look for future issues.
int main(void) {
int mat1[3][4] = {4,5,0,3,0,0,1,2,0,0,0,6};
//****************** Output 1 ***********************************************
for(int i=0; i<3;i++)
for(int j=0;j<4;j++){
printf("%d, ", mat1[i][j]);
if(j==3)printf("\n");
}
/* Expected output 1:
4 5 0 3
0 0 1 2
0 0 0 6
*/
printf("\n\n");
//******************* Output 2 ************************************************
for(int i=0; i<3;i++)
for(int j=0;j<4;j+=4)
printf("\n%d, %d, %d, %d", mat1[i][j+0],mat1[i][j+1],mat1[i][j+2],mat1[i][j+3]);
/* Expected output 2:
4 5 0 3
0 0 1 2
0 0 0 6
*/
printf("\n\n");
//********************* Output 3 **********************************************
for(int i=0; i<3;i++)
for(int j=0;j<4;j)
printf("\n%d, %d, %d, %d", mat1[i][j++],mat1[i][j++],mat1[i][j++],mat1[i][j++]);
/* Unexpected output 3:
3 0 5 4
2 1 0 0
6 0 0 0
*/
}