I accidentally covered the numbers with these instead of the curly brackets normally used and got "2 4 0 0". Why does this shifting happen?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
int a[2][2]={(1,2),(3,4)};
for (int i = 0; i < 2; ++i)
{
/* code */
for (int j = 0; j < 2; ++j)
{
/* code */
printf("%d ",a[i][j] );
}
}
return 0;
}
You accidentally used the comma operator. In your case it did nothing, but the resulting value from it is the last value in the comma separated list:
(1,2)
results in the last value2
and(3,4)
results in4
.So your code was equivalent to:
The last two of the four values in
a
were not provided, so they were initialized implicitly with zeroes. This post explains exactly why.