C Program to Multiply Two Matrices

C Program to Multiply Two Matrices

A small school table has two rows and two columns. The matrix multiplication operation should preserve what each row and column means, rather than treating the table as four unrelated values.

Matrix multiplication computes each result entry as a row-column dot product, not as an entrywise product.

Before this exercise, review array indexes and bounds and nested loops. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why matrix multiplication uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Multiply the original matrix by the two-by-two identity matrix.

Matrix Multiplication: follow the data

  1. Given data

    A small school table has two rows and two columns. The matrix multiplication operation should preserve what each row and column means, rather than treating the table as four unrelated values.

  2. Rule

    Matrix multiplication computes each result entry as a row-column dot product, not as an entrywise product.

  3. Observed result

    19 22 43 50

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

For every output position, multiply pairs along the left row and right column, then add those products. The shared inner dimension supplies the loop over k.

The example fixes both dimensions at two so every index can be checked directly. A general matrix interface must carry dimensions, require compatible shapes for its operation and validate storage for each referenced element.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  int matrix[2][2] = {{1, 2}, {3, 4}};
  int other[2][2] = {{5, 6}, {7, 8}}, result[2][2] = {{0}};
  for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) for (int k = 0; k < 2; k++) result[row][col] += matrix[row][k] * other[k][col];
  for (int row = 0; row < 2; row++) { for (int col = 0; col < 2; col++) printf("%d%s", result[row][col], col == 1 ? "\n" : " "); }
  return 0;
}

Expected output

19 22
43 50
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int matrix[2][2] = {{1, 2}, {3, 4}};
  int other[2][2] = {{5, 6}, {7, 8}}, result[2][2] = {{0}};
  for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) for (int k = 0; k < 2; k++) result[row][col] += matrix[row][k] * other[k][col];
  for (int row = 0; row < 2; row++) { for (int col = 0; col < 2; col++) printf("%d%s", result[row][col], col == 1 ? "\n" : " "); }
  return 0;
}

Result (0,0) is 1×5 + 2×7 = 19. Result (0,1) is 1×6 + 2×8 = 22.

Read the resulting rows in order; moving a value to another row or column is part of the operation’s meaning, not merely an output-format choice.

Follow the worked calculation

  1. Start with the sample

    Result (0,0) is 1×5 + 2×7 = 19. Result (0,1) is 1×6 + 2×8 = 22.

  2. Apply the rule

    Read the resulting rows in order; moving a value to another row or column is part of the operation’s meaning, not merely an output-format choice.

  3. Check the result

    19 22 43 50

    19 22
    43 50
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int matrix[2][2] = {{1, 2}, {3, 4}};
  int other[2][2] = {{5, 6}, {7, 8}}, result[2][2] = {{0}};
  for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) for (int k = 0; k < 2; k++) result[row][col] += matrix[row][k] * other[k][col];
  for (int row = 0; row < 2; row++) { for (int col = 0; col < 2; col++) printf("%d%s", result[row][col], col == 1 ? "\n" : " "); }
  return 0;
}

Multiply the original matrix by the two-by-two identity matrix.

Worked answer

The complete worked answer uses the same valid two-by-two input. Trace its index rule independently; compare each printed result with the source element or accumulation that produced it.

C17 · complete program

#include <stdio.h>
int main(void) {
  int matrix[2][2] = {{1, 2}, {3, 4}};
  int other[2][2] = {{1, 0}, {0, 1}}, result[2][2] = {{0}};
  for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) for (int k = 0; k < 2; k++) result[row][col] += matrix[row][k] * other[k][col];
  for (int row = 0; row < 2; row++) { for (int col = 0; col < 2; col++) printf("%d%s", result[row][col], col == 1 ? "\n" : " "); }
  return 0;
}

Expected output

1 2
3 4
Run in C compiler →

Recognize the important boundaries

The left column count must equal the right row count. Multiplication is not generally commutative, even when both orders are dimensionally valid.

Larger or untrusted dimensions require checked storage-size calculations. Integer products and accumulated sums also need a representable-range contract.

Check your understanding

Choose the answer, then compare the reasoning for every option.

What must be known before indexing a general two-dimensional table?
Choose one answer. You can try again.

Answer and explanation

Its dimensions and compatible storage layout.

Correct. A row/column access is valid only within the corresponding bounds.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Why initialize each result entry to zero?
Choose one answer. You can try again.

Answer and explanation

Each dot product starts with an empty sum.

Correct. An uninitialized accumulator cannot be safely read by +=.

Use the idea independently

Matrix multiplication computes each result entry as a row-column dot product, not as an entrywise product.

Continue with array boundaries.

Continue with function bounds contracts.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Print Matrix Boundary Elements →