How can I print multidimensional arrays in C?

Hasan Keçeci
#include <stdio.h>

int main(){

  int number[3][4] = {
    {10,20,30,40},
    {15,25,35,45},
    {47,48,49,50},
  };
      printf("%s",number[]);
  return 0;
}

Ok, here's my code and what I wonder is how can I print out those arrays that I created. Do I have to use for loop or is there another way?

P.S: Im a newbie.

Vlad from Moscow

Use nested for loops. For example

for ( size_t i = 0; i < 3; i++ )
{
    for ( size_t j = 0; j < 4; j++ )
    {
        printf( "%2d ", number[i][j] );
    }
    putchar( '\n' );
}

Pay attention to that it is a bad idea to use magic numbers like 3 and 4. Instead use named constants.

As for the conversion specifier %s then it is used to output one-dimensional character arrays that contain strings

Here is a demonstrative program

#include <stdio.h>

int main(void) 
{
    enum { M = 3, N = 4 };
    int number[M][N] =
    {
        { 10, 20, 30, 40 },
        { 15, 25, 35, 45 },
        { 47, 48, 49, 50 },
    };

    for ( size_t i = 0; i < M; i++ )
    {
        for ( size_t j = 0; j < N; j++ )
        {
            printf( "%2d ", number[i][j] );
        }
        putchar( '\n' );
    }

    return 0;
}

Its output is

10 20 30 40 
15 25 35 45 
47 48 49 50 

To output a two-dimensional integer array as a one-dimensional integer array apply casting to the array designator to the type int * or const int *.

For example

#include <stdio.h>

int main(void) 
{
    enum { M = 3, N = 4 };
    int number[M][N] =
    {
        { 10, 20, 30, 40 },
        { 15, 25, 35, 45 },
        { 47, 48, 49, 50 },
    };

    const int *p = ( const int * )number;

    for ( size_t i = 0; i < M * N; i++ )
    {
        printf( "%2d ", p[i] );
    }
    putchar( '\n' );

    return 0;
}

The program output is

10 20 30 40 15 25 35 45 47 48 49 50 

And vice versa to print a one dimensional array as a two-dimensional array use the following approach.

#include <stdio.h>

int main(void) 
{
    enum { N = 12 };
    int number[N] = { 10, 20, 30, 40, 15, 25, 35, 45, 47, 48, 49, 50 };

    size_t rows = 3, cols = 4;

    for ( size_t i = 0; i < rows; i++ )
    {
        for ( size_t j = 0; j < cols; j++ )
        {
            printf( "%2d ", number[i * cols + j] );
        }
        putchar( '\n' );
    }           

    return 0;
}

The program output is

10 20 30 40 
15 25 35 45 
47 48 49 50 

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

how to do Multidimensional Arrays

分類Dev

How can I sort a multidimensional array naturally?

分類Dev

How Can i display the output of MySQL “PRINT” Command in C#?

分類Dev

How can I print multiple arrays on separate lines using same console.log?

分類Dev

How can I use the multidimensional index returned by max or min?

分類Dev

How can I convert spectrogram data to a tensor (or multidimensional numpy array)?

分類Dev

How can I create and pass as argument a multidimensional ctypes array?

分類Dev

How can I print "-n" with `echo`?

分類Dev

how can i print this using String Methods

分類Dev

How can i find the similarity in 2 arrays

分類Dev

C - How can I print random word inside game board (2d array)?

分類Dev

How to hand over values to a double-pointer and print the values out as if it would be an multidimensional array? (C)

分類Dev

How can I print out a map[string]interface{} as json?

分類Dev

How can I print the log for a branch other than the current one?

分類Dev

How can I print a void method inside a button using Swing?

分類Dev

How can I tell Selenium to press cancel on a print popup?

分類Dev

How can I tell Selenium to press cancel on a print popup?

分類Dev

How can I print all the elements of a vector in a single string in R?

分類Dev

How can i control the number of pages to be print using javascript?

分類Dev

How can I show and update a list of print statements in a Bokeh dashboard?

分類Dev

How can I print list in python? (should be formatted)

分類Dev

How can I print the return value of a JavaScript function in HTML?

分類Dev

How can I keep input track log in shiny, then print it and save it?

分類Dev

How can I print out HTML markup based on an object hierarchy?

分類Dev

How can I print my swap function with Struct

分類Dev

How can I print contents of a text file in git bash?

分類Dev

How can I print a '/' at a specific index in my matrix in python

分類Dev

How can I print in one column the SUM of two columns

分類Dev

How can I print the requests in boost.asio?

Related 関連記事

  1. 1

    how to do Multidimensional Arrays

  2. 2

    How can I sort a multidimensional array naturally?

  3. 3

    How Can i display the output of MySQL “PRINT” Command in C#?

  4. 4

    How can I print multiple arrays on separate lines using same console.log?

  5. 5

    How can I use the multidimensional index returned by max or min?

  6. 6

    How can I convert spectrogram data to a tensor (or multidimensional numpy array)?

  7. 7

    How can I create and pass as argument a multidimensional ctypes array?

  8. 8

    How can I print "-n" with `echo`?

  9. 9

    how can i print this using String Methods

  10. 10

    How can i find the similarity in 2 arrays

  11. 11

    C - How can I print random word inside game board (2d array)?

  12. 12

    How to hand over values to a double-pointer and print the values out as if it would be an multidimensional array? (C)

  13. 13

    How can I print out a map[string]interface{} as json?

  14. 14

    How can I print the log for a branch other than the current one?

  15. 15

    How can I print a void method inside a button using Swing?

  16. 16

    How can I tell Selenium to press cancel on a print popup?

  17. 17

    How can I tell Selenium to press cancel on a print popup?

  18. 18

    How can I print all the elements of a vector in a single string in R?

  19. 19

    How can i control the number of pages to be print using javascript?

  20. 20

    How can I show and update a list of print statements in a Bokeh dashboard?

  21. 21

    How can I print list in python? (should be formatted)

  22. 22

    How can I print the return value of a JavaScript function in HTML?

  23. 23

    How can I keep input track log in shiny, then print it and save it?

  24. 24

    How can I print out HTML markup based on an object hierarchy?

  25. 25

    How can I print my swap function with Struct

  26. 26

    How can I print contents of a text file in git bash?

  27. 27

    How can I print a '/' at a specific index in my matrix in python

  28. 28

    How can I print in one column the SUM of two columns

  29. 29

    How can I print the requests in boost.asio?

ホットタグ

アーカイブ