Hello, I'm new to C language, and I'm using Visual C as my compiler..
Here's my code:

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


    int i[8][8];
void main(){
    int row;
    int col;
   for(row=0;row<=7;row++)
    {
        for(col=0;col<=7;col++)
        {
            i[row][col]=0;
        }
    }
   printi();

    getchar();getchar();
}



int printi()
{
            char s[500];
    int row;
    int col;
    int g;
    int a=0;
       for ( row=0; row < 7; row++ ) 
       {
      for ( col=0; col < 7; col++ ) 
      {
         s[a]=("%c", (char)i[row][col] );
         a++;
      }
       }


       for(g=0;g<=sizeof(s);g++)
       {
           int a=0;
           if(a<8){
           printf("%c",s[g]);
           printf(" ");
           }
           else{
               a=0;
               printf("\n");
           }
       }
       return 0;
}

I want it to give this output:

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

But it gives me a weird set of characters that are not letters or numbers.. only weird symbols.. can u fix the code for me please and explain the problem?

Thanks in advance

Recommended Answers

All 3 Replies

Try this ...

/* problem2.c */

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


#define MAX_SIZE 8


void print( int ary[MAX_SIZE][MAX_SIZE], int, int );


/* void main() */
int main()
{
    int ary[MAX_SIZE][MAX_SIZE];

    int row, col;
    for( row = 0; row < MAX_SIZE; ++ row )
    {
        for( col = 0; col <MAX_SIZE; ++ col )
        {
            ary[row][col] = 0;
        }
    }

    print( ary, MAX_SIZE, MAX_SIZE ) ;

    getchar();

    return 0; /* Note! main function returns an int */
}



void print( int ary[MAX_SIZE][MAX_SIZE], int rows, int cols )
{
    int row, col;
    for( row = 0; row < rows; ++ row )
    {
        for( col = 0; col < cols; ++ col )
        {
            printf( "%d ", ary[row][col] );
        }
        putchar( '\n' );
    }
}

Thanks, but I don't want it to print it directly. I want it to add it to a char array, and then print it. Because I have to use the char array to replace all of '1' to 'R' later..
Can you let it store the integer in a char array, and then print that char array?

Thanks a lot and sorry for bothering

No bother ... I enjoy seeing students learn new things.

You do realize that char is stored as an int ?

See below to see that getchar() returns an int
http://en.cppreference.com/w/c/io/getchar

So since char are stored as int, you can use an array of int to store the char's
Then just remember to treat (cast) the int values back to char
to get the char with that int value displayed.

See revised demo below:

/* problem_int2char.c */

#include <stdio.h>

#define MAX_SIZE 8


void print( int ary[MAX_SIZE][MAX_SIZE], int, int );



int main()
{
    int ary[MAX_SIZE][MAX_SIZE];

    int row, col;
    for( row = 0; row < MAX_SIZE; ++ row )
    {
        for( col = 0; col < MAX_SIZE; ++ col )
        {
            ary[row][col] = 0;
        }
    }

    /* now add a border of # char's ... */
    for( row = 0; row < MAX_SIZE; ++ row )
    {
        for( col = 0; col <MAX_SIZE; ++ col )
        {
            if( row == 0 || row == MAX_SIZE -1 )
                ary[row][col] = '#';
            else if( col == 0 || col == MAX_SIZE-1 )
                ary[row][col] = '#';
        }
    }


    fputs( "Demo printing int as char ... \n", stdout );
    print( ary, MAX_SIZE, MAX_SIZE ) ;

    fputs( "\nPress 'Enter' to continue.exit ... ", stdout );
    fflush( stdout );
    getchar();
    return 0;
}



void print( int ary[MAX_SIZE][MAX_SIZE], int rows, int cols )
{
    int row, col;
    for( row = 0; row < rows; ++ row )
    {
        for( col = 0; col < cols; ++ col )
        {
            printf( "%c ", 0 <= ary[row][col] && ary[row][col] <= 9 ? ary[row][col] + '0' : ary[row][col]  );
        }
        putchar( '\n' );
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.