ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
int fact(int n);
int main(void) {
    printf("%d", fact(5));
}
int fact(int n) {
    return n <= 1 ? 1 : n * fact(n - 1);
}
  1. A. 120
  2. B. Compilation error: fact is used before its definition
  3. C. 24
  4. D. Linker error: fact is declared but never defined

Correct answer: A. 120

The forward declaration `int fact(int n);` before main satisfies the compiler's need to know fact's signature before main calls it; the actual definition later in the file is found at link time (actually same translation unit, so just normal compilation), and fact(5) correctly computes 120 via the standard factorial recursion.

More C Programming questions

Exams that ask this topic