Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7899037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:35:51+00:00 2026-06-03T08:35:51+00:00

Suppose we have a matrix in C (a 2D array). I wonder how could

  • 0

Suppose we have a matrix in C (a 2D array).

I wonder how could I print the matrix into a string in C.

For example, if I have

double M[2][2]={{1.2,3.4},{3.14,2.718}}

I want to get a string like this:

"1.200  3.400\n3.140  2.718"

which could be printed as

1.200 3.400
3.140 2.718

If print to screen, the problem will become easier because we need not consider the buffer size. However, when printing to a string, it seems hard to know how large the string buffer should be, if I want to use functions like ‘sprintf’.

I searched google and stackoverflow but almost all of what I get is about how to convert string to numerical…

How to do this in C? Are there any libs?

Could you please help? Thank you!

EDIT:
The ‘snprintf’ solution works well for the situation of just one number.
But for matrix, it is supposed to use loops to go through every element.
And then add a little to the string in each loop. Will this work with ‘snprintf’?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:35:52+00:00Added an answer on June 3, 2026 at 8:35 am

    You’ll need to do this in two steps, first calculate the total size to hold the string, then allocate it and compose the string. Could be done in a function like this:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a))
    
    static char * matrix2D_to_string(const double *matrix, size_t rows, size_t columns)
    {
        const char format[] = "%f";
        const char column_separator[] = "  ";
        const char row_separator[] = "\n";
    
        int *column_widths = NULL;
        size_t r = 0, c = 0;
        char *buffer = NULL, *p = NULL;
        size_t size = 0;
    
        if (!rows || ! columns) {
            errno = EINVAL;
            return NULL;
        }
    
        // calculate maximum width for each column
        column_widths = (int *)calloc(columns, sizeof(*column_widths));
        for (r = 0; r < rows; ++r) {
            for (c = 0; c < columns; ++c) {
                char buf[256];
                int width = sprintf(buf, format, matrix[r * columns + c]);
                if (width > column_widths[c]) {
                    column_widths[c] = width;
                }
            }
        }
    
        // calculate total buffer size...
    
        // ... values
        for (c = 0; c < columns; ++c) {
            size += column_widths[c] * rows;
        }
        // ... column separators
        size += (columns - 1) * strlen(column_separator);
        // ... row separators
        size += (rows - 1) * strlen(row_separator);
        // ... nul terminator
        ++size;
    
        // make the string
        buffer = (char *)malloc(size);
        p = buffer;
        for (r = 0; r < rows; ++r) {
            if (r) {
                strcpy(p, row_separator);
                p += strlen(row_separator);
            }
            for (c = 0; c < columns; ++c) {
                if (c) {
                    strcpy(p, column_separator);
                    p += strlen(column_separator);
                }
                int width = sprintf(p, format, matrix[r * columns + c]);
                p += width;
                if (width < column_widths[c]) {
                    width = column_widths[c] - width;
                    memset(p, ' ', width);
                    p += width;
                }
            }
        }
        *p = '\0';
    
        // cleanup
        free(column_widths);
    
        return buffer;
    }
    
    int main()
    {
        double M[3][2]={{1.2,3.4},{3.14,2.718},{100.999,0.000005}};
    
        char *s = matrix2D_to_string((const double *)M, ARRAY_SIZE(M), ARRAY_SIZE(M[0]));
    
        puts(s);
    
        free(s);
    
        return 0;
    }
    

    This prints:

    1.200000    3.400000
    3.140000    2.718000
    100.999000  0.000005
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a string which contains an HTML file. How do I get
Suppose I have an n*n matrix of distances between n users. I'd like to
Suppose you have an array (m, m) and want to make it (n, n).
Suppose I have a matrix like: 100 200 300 400 500 600 1 2
Suppose I have a two column matrix. How do I pack the columns into
Suppose I have a simple multidimensional structure, like this one: somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))
Suppose I have a matrix foo as follows: foo <- cbind(c(1,2,3), c(15,16,17)) > foo
Suppose you have an NxN matrix and you have to check whether it's a
Suppose, in MATLAB, that I have a matrix, A, whose elements are either 0
suppose I have {data: {243232: {id: testid,name: test } }} so how to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.