Search code examples
cfunctiongccimplicit-conversioncompiler-flags

gcc compilation error on simple short int function call with flag -Werror=traditional-conversion


Compiling this C program with gcc 11.4.0 and -Werror=traditional-conversion raises an error:

short int f(short int x);

short int f(short int x) {
    return x;
}

int main(void)
{
    short int a = 0;
    f(a);

    return 0;
}

error: passing argument 1 of ‘f’ with different width due to prototype

Assuming that I can't change the function's signature because it comes from a library, is there a way to change the calling code to make the error go away?

I did try several kind of integer types for the a variable but to no avail. I would not expect the error to appear because the variable and the function's parameter prototype type are the same (short int). It looks to me like a false positive but it may be related to some implicit default promotion. I would rather find a solution that doesn't make me remove this compilation flag.


Solution

  • If you have no choice but to compile with this flag, you can disable it for a few lines using a pragma:

    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wtraditional-conversion"
        f(a);
    #pragma GCC diagnostic pop