0

This c code

int rhdDEboard2(){

    int board[64]={0};
    int rc;
    printf("\n");
    for (rc=0;rc<8;rc++)
        printf("\t %i %i %i %i %i %i %i %i\n",board[rc]);
    printf("\n");
    return 0;
}

compiled with MinGW in Win8 PC gives this error: : warning: format '%i' expects a matching 'int' argument [-Wformat]

I do not understand why! The board array is declared as type int..isn't it? (Does C treat the array as a pointer type?). I have tried using %p as format specifier but that doesn't work and nor does using %d. I understand what the warning says, but not why it says it, nor how to fix it, and feel I must be missing something quite basic, simple and straightforward here, but I do not understand this compiler warning.I'm grateful for help to try and get me to understand this C compiler warning and how I should fix it..many thanks

Barmar
  • 741,623
  • 53
  • 500
  • 612
rpd
  • 1,135
  • 4
  • 15
  • 24
  • http://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces?lq=1 – Barmar Feb 19 '15 at 01:18
  • 2
    You have 8 `%i` in the formatting string, but you only have 1 `board[rc]` in the rest of the arguments. It's complaining about the other 7 that don't have a corresponding argument. – Barmar Feb 19 '15 at 01:19
  • Why have you declared an array with 64 elements, but you're only printing the first 8? – Barmar Feb 19 '15 at 01:22
  • Hi Barmar..thanks for your helpful reply...On editing to:printf("\t %i %i %i %i %i %i %i %i\n",board[rc],board[rc],board[rc],board[rc],board[rc],board[rc],board[rc],board[rc]); I now get the 8x8 board output I wanted...I am repeatedly printing 8 elements on 8 new lines to form an 8x8 outputted board structure/display. Should I be doing this in a better way? – rpd Feb 19 '15 at 01:26
  • @rpd, No, there is no better way AFAIK. And after all it wouldn't be a better you it just would be shorter. – Iharob Al Asimi Feb 19 '15 at 01:27
  • Why are you printing the same board element 8 times? – Barmar Feb 19 '15 at 01:28
  • 2
    @rpd they way you fixed it will it now would print the same value 8 times. Use `board[rc * 8]. board[rc * 8 + 1], .., board[rc * 8 + 7]).` – ryanpattison Feb 19 '15 at 01:29
  • OK..you have a point there..I need now to change the code so each of the 64 array elements get printed in an 8x8 row fashion.....let me try that..... – rpd Feb 19 '15 at 01:30
  • Thanks to everyone who commented here..all comments were helpful. I am quite embarassed I didn't realise what this warning was, but I see what it is now, thanks to your kind help.Best wishes :-) – rpd Feb 19 '15 at 08:28

1 Answers1

0

As commenters mentioned, you should try any of the way.

int rhdDEboard2() {

    int board[64]={0};
    int i,j;
    printf("\n");
    for (i=0;i<8;i++) {
        printf("\t");
        for(j=0;j<8;j++)
            printf(" %i",board[(i*8)+j]);
        printf("\n");
    }
    printf("\n");
    return 0;
}

or

int rhdDEboard2() {

    int board[64]={0};
    int rc;
    printf("\n");
    for (rc=0; rc<64; rc+=8)
        printf("\t %i %i %i %i %i %i %i %i\n",
                board[rc]  ,board[rc+1],board[rc+2],board[rc+3],
                board[rc+4],board[rc+5],board[rc+6],board[rc+7] );
    printf("\n");
    return 0;
}
Sridhar Nagarajan
  • 1,085
  • 6
  • 14
  • Many thanks for your helpful reply with code examples. I am quite embarassed that I didn't see or fully understand this warning in the first place. I thought if there were too few or too many specifiers for arguments in the printf statement, the compiler would have said there were too few or too many! It did say it was expecting matching argument but I missed it's meaning! Hopefully I've learned my lesson and won't make that mistake again! Best wishes :-) – rpd Feb 19 '15 at 08:23