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));
}- 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
- B. It prints 5 reliably on every conforming compiler
- C. It is a compile-time error because function pointer casts between incompatible signatures are never allowed in C
- 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
- What is printed? c int a[3] = {1,2,3}; int parr[3] = {a, a+1, a+2}; int pp = parr; (pp + 1) += 10; printf("%d %d %d", a[0], a[1], a[2]);
- What is printed? c typedef int (Op)(int,int); int a(int x,int y){ return x+y; } int b(int x,int y){ return x-y; } Op table[2] = {a, b}…
- What is printed? c int a[2][3] = {{1,2,3},{4,5,6}}; int rowptrs[2] = {a[0], a[1]}; int pp = rowptrs; for (int i = 0; i < 2; i++) for (int…
- Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers, natural alignment). What is printed? c struct Inner { char a; short b; }; struct…
- Assume unsigned int bitfields pack into 32-bit allocation units (typical gcc/Linux x86-64 behavior). What is printed? c struct BF {…
- Assume unsigned int bitfields pack into 32-bit allocation units and a zero-width bitfield forces the next field to a new unit. What is…