✓ Link copied to clipboard
C 📅 July 10, 2026 ⏱ 7 min read

C Program to Multiply Two Matrices Using Functions

C Program to Multiply Two Matrices Using Functions

Matrix multiplication's a pretty common thing in linear algebra. So, you’ve got two matrices and you wanna multiply ’em using C. This program shows you how to do that by passing matrices to a function. Picture a 2x3 matrix with a 3x2 matrix to get a 2x2 result. Let’s jump into this and see how it works in C.

Prerequisites for the C Program to Multiply Two Matrices

Before you start, ensure you know these concepts:

  • Understanding of functions and how to pass arrays to them.
  • Using loops for iteration in C.
  • Knowledge of stdio.h for input and output operations.
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.

Source Code of C Program to Multiply Two Matrices by Passing Matrix to a Function

Here's the complete code to multiply two matrices by passing them to a function:

matrix_multiply.cc▶ Run
#include <stdio.h>

void multiply(int first[][10], int second[][10], int result[][10], int r1, int c1, int c2) {
    int i, j, k;
    // Initialize the result matrix to zero
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            result[i][j] = 0;
        }
    }

    // Multiply the matrices
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            for (k = 0; k < c1; k++) {
                result[i][j] += first[i][k] * second[k][j];
            }
        }
    }
}

int main() {
    int first[10][10], second[10][10], result[10][10];
    int r1, c1, r2, c2, i, j;

    printf("Enter the number of rows for the first matrix: ");
    scanf("%d", &r1);
    printf("Enter the number of columns for the first matrix: ");
    scanf("%d", &c1);
    printf("Enter the number of columns for the second matrix: ");
    scanf("%d", &c2);

    // Read the first matrix
    printf("Enter the elements of the first matrix:\n");
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c1; j++) {
            scanf("%d", &first[i][j]);
        }
    }

    // Read the second matrix
    printf("Enter the elements of the second matrix:\n");
    for (i = 0; i < c1; i++) {
        for (j = 0; j < c2; j++) {
            scanf("%d", &second[i][j]);
        }
    }

    // Call the multiply function
    multiply(first, second, result, r1, c1, c2);

    // Display the result
    printf("Resultant matrix:\n");
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Multiply Two Matrices Program

When you execute the code, here's the output you'll see for the example input:

OUTPUTterminal
Enter the number of rows for the first matrix: 2
Enter the number of columns for the first matrix: 3
Enter the number of columns for the second matrix: 2
Enter the elements of the first matrix:
1 2 3
4 5 6
Enter the elements of the second matrix:
7 8
9 10
11 12
Resultant matrix:
58 64
139 154
Output of C Program to Multiply two Matrices by Passing Matrix to a Function
Output of C Program to Multiply two Matrices by Passing Matrix to a Function

Algorithm to Multiply Two Matrices in C

To multiply two matrices, each element of both matrices is multiplied and summed up row by row and column by column. This is the logic the program implements step by step.

  1. Read the dimensions of both matrices (number of rows and columns).
  2. Input the elements of the first and second matrices.
  3. Initialize the result matrix to zero.
  4. For each row in the first matrix:
  5. For each column in the second matrix:
  6. Multiply each element by the corresponding element in the second matrix and add to the result matrix.
  7. Display the resultant matrix.
flowchart TD
  A([Start]) --> B[/Read dimensions of matrices/]
  B --> C[/Read first matrix/]
  C --> D[/Read second matrix/]
  D --> E[Call function to multiply matrices]
  E --> F[Initialize result matrix to 0]
  F --> G[i = 0]
  G --> H{Is i < rows of first matrix?}
  H -->|Yes| I[j = 0]
  I --> J{Is j < columns of second matrix?}
  J -->|Yes| K[k = 0]
  K --> L{Is k < columns of first matrix?}
  L -->|Yes| M[Multiply and add to result matrix[i][j]]
  M --> N[k++]
  N --> L
  L -->|No| O[j++]
  O --> J
  J -->|No| P[i++]
  P --> H
  H -->|No| Q[Print result matrix]
  Q --> R([End])
Flowchart: the algorithm step by step

How the C Program to Multiply Two Matrices Works

This section breaks down the code into understandable parts.

  • #include <stdio.h>: Includes the standard input-output library necessary for printf and scanf.
  • void multiply(...): This function takes matrices and their dimensions as parameters and calculates their product.
  • int result[10][10]: Declares a result matrix to store the product of the two matrices.
  • for (i = 0; i < r1; i++): Loops through each row of the first matrix.
  • for (j = 0; j < c2; j++): Loops through each column of the second matrix.
  • for (k = 0; k < c1; k++): Iterates over elements to perform the multiplication and addition required for matrix multiplication.
  • multiply(first, second, result, r1, c1, c2): Calls the function to multiply the matrices.
  • printf: Outputs the resultant matrix.
  • return 0;: Ends the program successfully.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

Why do you need to initialize the result matrix to zero before multiplication?

🎉Correct! Initializing to zero ensures no garbage values affect the results.
🤔Not quite. Think about what happens if you add to uninitialized numbers.

How Fast Is the Multiply Two Matrices Program in C?

The efficiency of matrix multiplication is a classic topic in C programming.

Time Complexity: O(n3). This is because we use three nested loops with for (i = 0; i < r1; i++), for (j = 0; j < c2; j++), and for (k = 0; k < c1; k++). Here, n represents the number of elements being processed across the loops. For example, multiplying two 3x3 matrices requires about 27 operations.

Space Complexity: O(1). This is because we only use a fixed number of integer matrices first, second, result, irrespective of their size.

⚠️

Common mistake: Forgetting to initialize the result matrix to zero before starting multiplication can yield incorrect results due to leftover values in memory.

Key takeaway: Always make sure matrices are compatible for multiplication by checking their dimensions.

FAQs About C Program to Multiply Two Matrices by Passing Matrix to a Function

Here are some common questions about multiplying matrices in C:

No, the number of columns in the first matrix has to match the number of rows in the second matrix.
The program won’t execute the multiplication as you’d expect, which might lead to wrong results.
Using functions keeps the code organized; it makes it reusable and way easier to read.
Yeah, this is a classic example of matrix multiplication where the output will be a 2x2 matrix.
You can increase the max size of the matrices by changing the array dimensions in the code.
Yep, matrix multiplication isn't commutative; changing the order gives you a different product (if it works at all).

If you found this program helpful, check out these related programs:

Conclusion

When you’re working with matrices, always check their dimensions before trying to multiply them. This program is awesome for getting how C deals with 2D arrays and functions. Next up, try modifying it to add two matrices instead. You’ll notice the logic is pretty similar.

For more practice, check out our C language quiz, or run this program in the C online compiler.