Home > front end >  Function defined without a prototype sometimes results in conflicting types error
Function defined without a prototype sometimes results in conflicting types error

Time:01-25

I have encountered a strange behaviour in clang (both AppleClang 1400.0.29.202 and clang 15.0.7 from Homebrew). Assume I have the following code:

int bar();

int bar(int a, TEST b) {
    return 43;
}

It compiles with -DTEST=int but fails to compile with -DTEST=charresulting in conflicting types for 'bar'. This happens regardless of the specified standard (I have tried c89 and c99).

Am I missing something?

EDIT: I realise that omitting prototypes is highly discouraged and I don't write code like this, but there's still a lot of code written like this in the wild. I was trying to compile enscript this morning and encountered this issue.

Also, the following works with -DTEST=char.

int bar();

int bar(a, b)
    int a;
    TEST b;
{
    return 43;
}

EDIT2: I should have tried it with other compilers before asking. GCC output contains a note about argument promotion:

note: an argument type that has a default promotion cannot match an empty parameter name list declaration

CodePudding user response:

Am I missing something?

Yes, a proper function declaration. The code should have been:

int bar(int a, TEST b);

int bar(int a, TEST b) {
    return 43;
}

[Why I am getting this error?]

int bar() is a function with unspecified count and type of arguments and all arguments undergo default argument promotions, where a char is promoted to an int. Because it's impossible for int bar() to take a char argument, because it would be promoted to an int, that is a conflicting type.

  • Related