ProgrammingVery hardProduct company OA

Which statement about this code is correct?

Which statement about this code is correct?
int add(int a, int b) { return a + b; }
int main(void) {
    int (*fp)(int) = (int (*)(int))add;
    printf("%d", fp(5));
}
  1. A. It compiles (the cast silences the type mismatch), but calling fp(5) is undefined behavior because the call is made through a function-pointer type incompatible with add's actual type
  2. B. It prints 5 reliably on every conforming compiler
  3. C. It is a compile-time error because function pointer casts between incompatible signatures are never allowed in C
  4. D. It always prints the sum of 5 and an uninitialized stack value, portably

Correct answer: A. It compiles (the cast silences the type mismatch), but calling fp(5) is undefined behavior because the call is made through a function-pointer type incompatible with add's actual type

The C standard forbids calling a function through a pointer whose type doesn't match the function's actual type; the cast only suppresses the diagnostic, it doesn't make the call well-defined, even though in practice it often just drops the missing second argument.

More C Programming questions

Exams that ask this topic