0 votes 0 votes #include<stdio.h> int main() { int M = 2; int arr[M][M] = {0}; // Trying to initialize all values as 0 int i, j; for (i = 0; i < M; i++) { for (j = 0; j < M; j++) printf ("%d ", arr[i][j]); printf("\n"); } return 0; } what happen Programming in C programming-in-c + – amit166 asked Jan 2, 2019 amit166 439 views answer comment Share Follow See all 6 Comments See all 6 6 Comments reply Show 3 previous comments Shaik Masthan commented Jan 2, 2019 reply Follow Share Who says those are constants? 0 votes 0 votes aambazinga commented Jan 2, 2019 reply Follow Share @Shaik Masthan dennis ritchie. second edition. in chapter 2 under the topic "constants" 0 votes 0 votes Shaik Masthan commented Jan 2, 2019 reply Follow Share #define ----- Actually speaking doesn't create any constant. Then how it works ? Actually source program before giving to the compiler, pre processed by the pre-processor, So at the time of code arrived to compiler, is simply looking as " int arr[2][2] = {0}; ", Due to this reason, it is not creating any problem. But coming to the original question, here M is a variable, ( even it is a constant variable, i.e., int constant M = 2; ), it creates at runtime only but not at the compile time. In C, array indices at declaration should be compile time constants only. i.e., compiler should be detect the size of array's at compile time only, So given statements are leads to compile time error. Now, you may think, M can be define as " static int M=2; ", even it is not work, Due to " arr[M][M] ", even M is defined at compile time but in that line, M evaluated at runtime, ===> lead to compiler error. 1 votes 1 votes Please log in or register to add a comment.
0 votes 0 votes It's not trying to initialize all variable in array , it will just initialize to first item of array arr[0][0]. just change arr[M][M]=1, and then try to print the array output will be like below 1 0 0 0 HanuamntappaBudihal answered Dec 30, 2019 HanuamntappaBudihal comment Share Follow See all 0 reply Please log in or register to add a comment.