Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc with -Wformat option prints warning for const unsigned char

Test Code:

#include <stdio.h>

int main(int argc, char **argv) {

    char buf[10];

    char c[] = "%i";
    unsigned char uc[] = "%i";
    const char cc[] = "%i";
    const unsigned char cuc[] = "%i";
    const unsigned char *cucp = "%i";

    sprintf(buf, (char *)c, 1);
    sprintf(buf, (char *)uc, 1);
    sprintf(buf, (char *)cc, 1);
    sprintf(buf, (char *)cuc, 1);
    sprintf(buf, (char *)cucp, 1);

    return 0;
}

Compile:

gcc -Wformat -o test test.c
test.c: In function ‘main’:
test.c:16:26: warning: format is a wide character string [-Wformat=]
    sprintf(buf, (char *)cuc, 1);
                         ^

Why do I get a warning for const unsigned char but not for the other types?

Tested with gcc 5.4.0 and some cross gcc 4.3.2. The explanation might include in which segments the different data types are stored, but I'm quite surprised.

like image 365
steini2000 Avatar asked Jan 22 '26 00:01

steini2000


1 Answers

It's now a confimred gcc bug and should be fixed soon.

BTW: It was introduced 2000-09-24.

like image 124
steini2000 Avatar answered Jan 24 '26 17:01

steini2000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!