How to print the array?

前端 未结 5 475
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-02 07:06
int main() {  
  int my array[3][3] =
    10, 23, 42,    
    1, 654, 0,  
    40652, 22, 0  
  };  

  printf(\"%d\\n\", my_array[3][3]);  
  return 0;
}
5条回答
  •  独厮守ぢ
    2021-02-02 07:35

    If you want to print the array like you print a 2D list in Python:

    #include 
    
    int main()
    {
      int i, j;
      int my_array[3][3] = {{10, 23, 42}, {1, 654, 0}, {40652, 22, 0}};
      for(i = 0; i < 3; i++)
      {
          if (i == 0) {
              printf("[");
          }
          printf("[");
          for(j = 0; j < 3; j++)
          {
             printf("%d", my_array[i][j]);
             if (j < 2) {
                 printf(", ");
             }
          }
        printf("]");
        if (i == 2) {
            printf("]");
        }
    
        if (i < 2) {
            printf(", ");
        }
      }
      return 0;
    }
    

    Output will be:

    [[10, 23, 42], [1, 654, 0], [40652, 22, 0]]
    

提交回复
热议问题