Unix – Re Legacy code : format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long unsigned int’ [-Wformat]

cgccprintfundefined-behaviorunix

I often try to build a lot of old simulator and disk and tape archive tools, with recent GCC. Some errors are easy to fix, but I'm not so good a programmer.

I get :

itstar.c: In function ‘addfiles’:
itstar.c:194:4: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat]
itstar.c:194:4: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long unsigned int’ [-Wformat]

from this code fragment :

/* add files to a DUMP tape */
/* output buffer must have been initialized with resetbuf() */
static void addfiles(int argc,char **argv)
{
    int c=argc;
    char **v=argv;

    while(c--) {
        addfile(argc,argv,*v++);
    }
    if(verify)
        printf("Approximately %d.%d' of tape used\n",count/bpi/12,
            (count*10/bpi/12)%10);
}

Where line 194 is the third from last, beginning with printf.

The file is itstar.c, from tapetools, code here.

It builds despite the warning, but I prefer to know how to prevent it,
so the result is more efficient and there is less chance of data corruption.

Please, what have I missed, and need to change ?

Thank you in advance.

Best Answer

Use format specifier %lu instead of %d and your compiler should stop complaining.

printf("Approximately %lu.%lu' of tape used\n", count/bpi/12, (count*10/bpi/12)%10);
Related Topic