为什么在语言C的嵌套for循环中跳过scanf()?

Gol*_*TMM 2 c for-loop scanf

我正在为类编写一个程序,要求用户输入二维数组的大小,然后让用户输入数组的值.这是我到目前为止的代码:

#include <stdio.h>

int main(void)
{
    // Setup 
    int N, M;
    int row = 0, col = 0;

    printf("\n");
    printf("This program counts occurences of digits 0 through 9 in an NxM array.\n");
    printf("Enter the size of the array (Row Column): ");
    scanf(" %d %d", &N, &M);

    int array[N][M];     

    // Array Input
    for (row = 0; row < N; row++)
    {    
        printf("Enter row %d: ", row);      
        for (col = 0; col < M; col++); 
        {    
            scanf(" %d", &array[row][col]);
        }   
    }    
    printf("\n");    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在编译程序并输入信息时,我遇到的问题是,跳过第二个和所有后续的scanf()语句,或者在输入第一行后跳过整个第二个for循环.以下是输入输入的示例:

This program counts occurences of digits 0 through 9 in an NxM array.
Enter the size of the array (Row Column): 2 6
Enter row 0: 0 1 2 3 4 5
Enter row 1:
Run Code Online (Sandbox Code Playgroud)

然后程序完全结束.我真的不知道为什么会被跳过.我已经尝试改变scanf()语句中有多少个空格,但无论我改变什么,都会出现同样的问题.我不确定我是否错过了我犯过的一些愚蠢的错误,或者是否存在更大的问题.我很擅长编码.

asc*_*ler 5

你只有一个流浪的分号.尝试改变

for (col = 0; col < M; col++); 
{
Run Code Online (Sandbox Code Playgroud)

至:

for (col = 0; col < M; col++)
{
Run Code Online (Sandbox Code Playgroud)

A for引入了一个控制下一个语句的语句,该语句可以是以分号,另一个控制语句或使用{大括号的复合语句结尾的简单语句}.在您的代码中,与for关键字在同一行的分号计为无效的空语句.因此,代码只会将循环变量增加适当的次数,并且只有在移动到下一部分之后.

确保启用编译器警告.(gcc和clang都提供明确的警告,指出这段代码可能不符合你的意思.)