2

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.

5
  • See stackoverflow.com/questions/5509406/… Commented Sep 20, 2024 at 22:47
  • 1
    Just don't enable that error. The thing it's complaining about is not really an error. Commented Sep 20, 2024 at 22:48
  • 1
    It's warning about something that would be a problem if you didn't have a function prototype in scope (because short int would be converted to int by default promotions). But since you do, it's not a problem. Commented Sep 20, 2024 at 22:49
  • 2
    IMHO that error is useless and it will produce lots of false positives like that. The purpose seems to be to ensure compatibility with K&R C, which is not something you should worry about over 40 years later. Commented Sep 20, 2024 at 22:56
  • Why do you want to do that? It is completely useless. Commented Sep 21, 2024 at 19:01

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

This will get quite tedious if they have to put this pragma around every call to functions that take an argument smaller than int.
I was hoping there would exists a code syntax to help the compiler see that this was a false positive, but if it has to be a compiler ignore comment, so be it. The method I was trying to call is htons. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.