how to scanf two dimensional array in c

To scanf a two-dimensional array in C, you can follow these steps:

  1. Declare a two-dimensional array of the desired size. For example, if you want to scanf a 3x3 array of integers, you can declare it like this: int array[3][3];.

  2. Use nested loops to iterate over each element of the array. The outer loop controls the rows, and the inner loop controls the columns. For example:

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        // scanf each element of the array
    }
}
  1. Inside the nested loops, use the scanf function to read input from the user and store it in each element of the array. The scanf function requires a format specifier to indicate the type of data being read. For example, to scanf an integer, you can use the %d format specifier. So, to scanf an integer into array[i][j], you can use the following code:
scanf("%d", &array[i][j]);
  1. Repeat the scanf operation for each element of the array, until all elements have been scanfed.

Here's the complete code example:

#include <stdio.h>

int main() {
    int array[3][3];

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%d", &array[i][j]);
        }
    }

    // Print the array to verify the input
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", array[i][j]);
        }
        printf("\n");
    }

    return 0;
}

This code declares a 3x3 array, scanf integers into each element, and then prints the array to verify the input. You can modify the size of the array and the data type based on your requirements.