1

Sample code:

int f1(char c){ return c; };
int f2(int i ){ return i; };

int main(void)
{
  return (1 ? f1 : f2)(0);
}

Different behavior between compilers:

gcc (latest): gcc prog.c -Wall -Wextra -std=c89 -pedantic
prog.c: In function 'main':
prog.c:6:18: warning: pointer type mismatch in conditional expression
    6 |   return (1 ? f1 : f2)(0);
      |                  ^
prog.c:6:18: error: called object is not a function or function pointer
    6 |   return (1 ? f1 : f2)(0);
      |          ~~~~~~~~^~~~~
prog.c:7:1: warning: control reaches end of non-void function [-Wreturn-type]
    7 | }
      | ^
clang (latest): clang prog.c -Wall -Wextra -std=c89 -pedantic 
prog.c:6:13: warning: pointer type mismatch ('int (*)(char)' and 'int (*)(int)') [-Wpointer-type-mismatch]
  return (1 ? f1 : f2)(0);
            ^ ~~   ~~
prog.c:6:23: error: called object type 'void *' is not a function or function pointer
  return (1 ? f1 : f2)(0);
         ~~~~~~~~~~~~~^
1 warning and 1 error generated.
cl (latest): cl t1.c
t1.c(6): warning C4028: formal parameter 1 different from declaration
Microsoft (R) Incremental Linker Version 14.25.28611.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:t1.exe
t1.obj

Questions:

  1. What the C standard says?
  2. gcc & clang: why called object is not a function or function pointer? Then what is it?
  3. cl gives the warning, but compiles the code. Does this behavior conform to the C standard?
  4. Why clang called object type 'void *', but gcc called object? Where this void * comes from?
pmor
  • 5,392
  • 4
  • 17
  • 36
  • 1
    You're comparing to c89 in the first two, and no standard in the last one... – Dan Weber Oct 25 '20 at 19:31
  • MSVC: warning for the line in `main()`: *formal parameter 1 different from declaration* – Weather Vane Oct 25 '20 at 19:32
  • 1
    Moreover MSVC does not have an option to compile C in strict C89 mode. From the [`cl /std`](https://learn.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version?view=vs-2019#c-standards-support-1) reference: "*it isn't possible to specify strict C89 conformance*". – dxiv Oct 25 '20 at 19:36
  • If I return more meaningful values from `f1()` and `f2()`, and print the result, I can see that MSVC calls function `f1()`, or `f2()` if I change the ternary. – Weather Vane Oct 25 '20 at 19:38
  • 1
    The C standard draft N1570 6.5.15 says: "Constraints: ... One of the following shall hold for the second and third operands: ... ..." Your code doesn't satisfy any on the constraints in the list. So your code is invalid. – Support Ukraine Oct 25 '20 at 19:47
  • @4386427 aren't they *"pointers to qualified or unqualified versions of compatible types"* (apart from the different function argument)? – Weather Vane Oct 25 '20 at 20:15
  • @WeatherVane Since the functions differ, the function pointers are also non-compatible - at least that my understanding. – Support Ukraine Oct 26 '20 at 06:02

0 Answers0