steini2000
steini2000

Reputation: 41

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.

Upvotes: 3

Views: 343

Answers (1)

steini2000
steini2000

Reputation: 41

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

BTW: It was introduced 2000-09-24.

Upvotes: 1

Related Questions