📘 Lesson  ·  Lesson 62

Matrix Multiplication

What matrix multiplication means

A matrix is just a grid of numbers arranged in rows and columns — in C we store it as a 2D array. Multiplying two matrices combines them into a new matrix, but not by simply multiplying matching cells. Instead, each cell of the result is built from a whole row of the first matrix and a whole column of the second.

That "row times column" idea is the heart of the topic. Once it clicks, the C program is just three loops that automate it.

The rule: when can you multiply?

You cannot multiply any two matrices. There is one strict rule:

💡 The dimension rule

The columns of the first matrix must equal the rows of the second. If A is m×n and B is n×p, the result C is m×p. The two inner n values must match.

For our example both matrices are 2×2, so columns of A (2) equal rows of B (2), and the result is also 2×2.

How one result cell is calculated

Let's take two 2×2 matrices and compute just the top-left cell, so you see the pattern:

Calculation
A = | 1  2 |     B = | 5  6 |
    | 3  4 |         | 7  8 |

c[0][0] = (row 0 of A) . (col 0 of B)
        = 1*5 + 2*7
        = 5 + 14
        = 19

So the top-left result is 19. We repeat the same row-times-column idea for every cell: c[0][1] = 1*6 + 2*8 = 22, and so on.

The full program

Here is the complete C program for 2×2 matrices, with the result matrix started at zero.

C Language
#include <stdio.h>
int main() {
    int a[2][2] = {{1, 2}, {3, 4}};
    int b[2][2] = {{5, 6}, {7, 8}};
    int c[2][2] = {0};        // start every cell at 0

    for (int i = 0; i < 2; i++)          // each row of result
        for (int j = 0; j < 2; j++)      // each column of result
            for (int k = 0; k < 2; k++)  // shared dimension
                c[i][j] += a[i][k] * b[k][j];

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++)
            printf("%d ", c[i][j]);
        printf("\n");
    }
    return 0;
}
Output:
19 22
43 50

Why three nested loops?

Each loop has a clear job:

LoopVariableIts job
OuteriChooses the result row
MiddlejChooses the result column
InnerkAdds up the row×column products

The outer two loops point at one result cell; the inner loop fills that cell's value by walking along the shared dimension. That is exactly the row-times-column rule turned into code.

Common mistakes

  • Forgetting to initialise the result matrix to 0, so += adds onto garbage.
  • Multiplying cells directly (a[i][j] * b[i][j]) instead of doing row × column.
  • Ignoring the dimension rule and trying to multiply incompatible matrices.
  • Swapping the index order in a[i][k] * b[k][j] — the shared k must line up.
🏋️ Practice

Extend the program to multiply a 2×3 matrix by a 3×2 matrix. Change the array sizes and loop limits, and confirm the result is 2×2. This proves you understand the dimension rule.

Summary

  • Matrices multiply by combining a row of the first with a column of the second.
  • Columns of the first must equal rows of the second (m×n times n×p gives m×p).
  • Each cell c[i][j] sums a[i][k]*b[k][j] over k.
  • Three nested loops: row, column, and the shared dimension.
  • Always initialise the result matrix to 0 before adding.

Frequently Asked Questions

When can two matrices be multiplied in C?
Two matrices can be multiplied only when the number of columns in the first matrix equals the number of rows in the second. If matrix A is m×n and matrix B is n×p, the result is an m×p matrix. If that inner dimension does not match, multiplication is not defined.
How is each element of the result matrix calculated?
Each result cell c[i][j] is the sum of products of the i-th row of the first matrix with the j-th column of the second. You multiply corresponding elements and add them: c[i][j] = a[i][0]*b[0][j] + a[i][1]*b[1][j] + ....
Why does matrix multiplication in C need three nested loops?
The outer two loops pick which result cell you are filling (its row i and column j). The third, innermost loop k walks along the shared dimension to compute the row-times-column sum for that cell. So you need all three: two to locate the cell and one to build its value.
Why must the result matrix start at zero?
Because each cell is built by adding products one at a time with +=. If the cell starts with garbage instead of 0, the final sum will be wrong. Initialising the result matrix to all zeros gives each cell a clean starting point.
Is matrix multiplication commutative in C?
No. In general A×B is not equal to B×A — the order matters, and sometimes only one order is even valid based on the dimensions. This is different from ordinary number multiplication, and a common source of confusion for beginners.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.