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);
}- A. 120
- B. Compilation error: fact is used before its definition
- C. 24
- 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
- What is printed? c int x = 1; void incrementGlobal(void) { x++; } int main(void) { int x = 10; incrementGlobal(); printf("%d", x); }
- What is printed? c int f(void) { static int x = 10; x += 5; return x; } int main(void) { int a = f(); int b = f(); int c = a + b…
- What happens when these two files are linked together? c / file1.c / extern int shared; void show(void) { printf("%d", shared); } /…
- What is printed? c void f(void) { static int calls = 0; int local = 0; calls++; local++; printf("%d %d ", calls, local); } int main(void)…
- What is printed? c int x = 5; int getX(void) { return x; } int main(void) { int x = 20; printf("%d %d", x, getX()); }
- What is printed? c static int privateCounter = 0; void bump(void) { privateCounter++; } int getCounter(void) { return privateCounter; }…