C - Printing a 2D Char Array

How do I print the elements of a 2D Char Array in C?

Here is my current code:

int main()
{
  unsigned int size;

  printf("Enter size:\n");
  scanf("%d",&size);

  char word[size][size];

  //Enter the matrix
  for(int k = 0; k < (size); ++k){
    for (int j = 0; j < (size); ++j){
      printf("Enter letter:");
      scanf("%c",&word[k][j]);
    }
  }

  //printf("\n");
  for (int k = 0; k < size; ++k){
    for(int j = 0; j < size; ++j){

      printf("%c",word[k][j]);
    }
    //printf("\n ");
  }
  printf("\n");
}

When executed it returns the element in pairs (using a 4x4 array) Example:

ab
cd
ef
gh
ij
kl
mn
op

Rather than my desired output:

abcd
efgh
ijkl
mnop

Why is this?

Answers


changing your scanf solves all the problems

scanf(" %c",&word[k][j]);  // notice the space before '%c'

And also you need to change your printing loop to this

for (k = 0; k < size; ++k){
    for(j = 0; j < size; ++j){
        printf("%c",word[k][j]);
    }
    printf("\n");
}

I removed the reading and it seems like printing is ok:

int main()
{
    const unsigned int size = 4;
    char word[size][size];

    //Enter the matrix
    for (int k = 0; k < (size); ++k) {
        for (int j = 0; j < (size); ++j) {
            word[k][j] = 'a' + j + (k * size);
        }
    }

    for (int k = 0; k < size; ++k) {
        for (int j = 0; j < size; ++j) {

            printf("%c", word[k][j]);
        }
        printf("\n");
    }
    printf("\n");

    getchar();
    return 0;
}

And the output:

abcd
efgh
ijkl
mnop

I found two issues with your source.

One is the memory allocation - that is actually not ansi-c.

If you need dynamic memory you need to allocate it at runtime. Consider switching to c++ since there are standard facilities that help with that in a safer way.

The second issue was that there is a whitespace character in the buffer that is used as an input character. I think you want to clear that.

Here is the source with additional comments:

#include <stdio.h>
#include <stdlib.h>

void ansiC()
{
    unsigned int size;

    printf("Enter size:\n");
    scanf("%d", &size);

    //char word[size][size]; <- this is not ansi-c because size is unknown at compile time
    char * word = (char*)malloc(sizeof(char)* size * size);


    //Enter the matrix
    for (int k = 0; k < (size); ++k)
    {
        for (int j = 0; j < (size); ++j)
        {
            printf("Enter letter:");
            scanf("%c ", &word[k * size + j]);
            //since word is just a pointer i changed the way the position is calculated
            //after the character the user presses the enter key
            //this puts a whitespace character on the buffer. 
            //by adding the space after %c you also clear that from the buffer
        }
    }

    //printf("\n");
    for (int k = 0; k < size; ++k)
    {
        for (int j = 0; j < size; ++j)
        {

            printf("%c", word[k * size + j]);
            //since word is just a pointer i changed the way the position is calculated
        }
        //printf("\n ");
    }
    printf("\n");

    free(word); //if you use malloc you need to remember to use free
}

int main()
{
    ansiC();
    return 0;
}

Beware: %c and %1s do different things (apart from adding a terminating null for the latter):

  • c take every character including space, tab, cr and lf
  • %1s skip over all blanks (space, tab, cr, lf, etc.)

So at input time, you should use:

char c[2]; // provide room for a terminating null...
...
for(int k = 0; k < (size); ++k){
    for (int j = 0; j < (size); ++j){
        printf("Enter letter:");
        scanf("%1s",c);
        word[k][j] = c[0];
    }
}

And at print time:

for (int k = 0; k < size; ++k){
    for(int j = 0; j < size; ++j){
        printf("%c",word[k][j]);
    }
    printf("\n "); // new line after each line
}

Please check this .

# include <iostream.h>
# include <conio.h>


void main()
{
clrscr();
char arr[5][3]={"abc","aks","tny","dkn","kbf"};
    for(int a=0;a<5;a++)
    {
        for(int b=0;b<3;b++)
        {
            cout<<" "<<arr[a][b];
        }
        cout<<endl;
    }


 getch();
}

Need Your Help

Date time formatting proglem in ios restkit

objective-c ios rest restkit

I have a .NET REST service which returns data in JSON format. One field is date time in UTC which looks like this: