预处理器跳过 C 中的部分代码

Pre-processors skipping part of a code in C

我正在做一个项目,我有一个矩阵,我正在通过向上、向下、向左和向右移动角色来对矩阵进行一些处理。我已将移动存储在一个字符数组中。现在我只想在对它执行其他移动后打印矩阵的最后 10 个状态。但我不想打印其他动作,只打印矩阵的最后 10 个状态。

所以我正在循环这样的动作:

int i = 0;
for (; i < strlen(movesArray); i++ ) {

    operation = movesArray[i]; // move 

     switch (operation) {
        case 'v': // process the moves 
     }

然后在 for 循环中我做了这样的事情:

 #ifdef NDEBUG // but this printing every state from 1 to 99
     if( i >= strlen(movesArray) - 10)
        printf("%s %d:\n", "Move", i );
        print(matrix);
#endif

然而,它正在打印所有移动,因为我只需要到目前为止的最后 10 个实例。谁能指导我正确的方向?我已经用了几个小时了。

If i have 99 moves, then it should perform the all the moves but it should just print the last 10 states of the matrix and that should include the moves that have been done the matrix.

我用 -D 标志编译我的程序。

你的 if 上没有花括号。这样想:

if( i >= strlen(movesArray) - 10)
    printf("%s %d:\n", "Move", i ); // No curly braces means only the statement after the 
                                    // if is part of the conditional, in this case 
                                    // thats this one..
print(matrix); 

所以你应该这样做:

if( i >= strlen(movesArray) - 10) {
    printf("%s %d:\n", "Move", i );
    print(matrix);
} // Everything between the curly braces is part of this conditional. 

很多行业都遵循编码标准,无论有多行还是只有一行,都应该始终使用大括号。如上所示,它只增加了一行,但可以防止严重的错误like this one

注:这是我个人的看法,不代表任何要求。

您似乎假设 printf 语句中的两个选项卡都在 if( ... ) 条件的控制之下,它们不在 C 中。

您需要添加花括号,如下所示。在您的代码示例中,只有第一个 printfif (...) 控制。在 C 中,您需要花括号来包含多个语句。

 #ifdef NDEBUG // but this printing every state from 1 to 99
     if( i >= strlen(movesArray) - 10)
     {
        printf("%s %d:\n", "Move", i );
        print(matrix);
     }
#endif