简体   繁体   English

使用嵌套循环时C编程printf不打印

[英]C programming printf not printing when used nested loop

I'm learning about multidimensional array in C programming. 我正在学习C编程中的多维数组。 But the printf function is not working. 但是printf函数不起作用。 Here is my code: 这是我的代码:

#include <stdio.h>
int main (void)
{
  int array[2][3][4]; 
  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < 4; j++)
     {
        for (int k = 0; k < 5; k++)
        {
            array[i][j][k] = k;
            printf("array[%d][%d][%d] = %d\n", i, j, k, array[i][j][k]);
        };
    };

};
printf("Loop is finished!");
return 0;
}

You are going to loop out of bounds. 您将越界。

Take the first dimension, 2, your loop is < 3.... so its going to use indexes 0 1 2 . 以第一个维度2为例,您的循环为<3 ....,因此其将使用索引0 1 2 only 0 and 1 are valid. 只有01有效。 change your loops to i < 2 , j < 3 and k < 4 respectively. 将循环分别更改为i < 2j < 3k < 4

This program will not give result since it having lots of Syntax errors . 由于程序存在很多Syntax errors因此不会给出结果。 you need not to be give ; 你不需要付出; - semicolon after for loop -for循环后for分号

syntax for For Loop is: For Loop语法为:

FOR( initialization expression;condition expression;update expression)
{
\\Loop content comes here
}

And also C will not permit Instant Declaration , variables should be declare @ the declaration section. 而且C不允许Instant Declaration ,变量应在声明部分声明。

Your Program can be improved by applying these changes, then it gives output. 通过应用这些更改可以改进您的程序,然后提供输出。 the code snippet will be like the following: 该代码段将如下所示:

#include <stdio.h>
int main ()
{
  int array[3][4][5]; 
  int i,j,k;
  for ( i = 0; i < 3; i++)
   {
     for ( j = 0; j < 4; j++)
      {
        for (k = 0; k < 5; k++)
        {
            array[i][j][k] = k;
            printf("array[%d][%d][%d] = %d\n", i, j, k, array[i][j][k]);
        }
      }
  }
  printf("Loop is finished!");
  return 0;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM