ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
void f(void) {
printf("%d ", x);
}
int x = 42;
int main(void) {
f();
}- A. 42
- B. 0
- C. Compile-time error: x is used in f() before it is declared
- D. Compile-time error: f() is defined before x's declaration
Correct answer: C. Compile-time error: x is used in f() before it is declared
C requires an identifier to be declared (at least) before its first use in the source text being compiled; here f() references x before int x = 42; even appears in the file, with no earlier declaration (such as a forward extern int x;) in scope. This fails to compile.
More C Programming questions
- What is printed? c int x = 5; void modify(void) { x = 10; } int main(void) { int x = 20; modify(); printf("%d %d", x, ::x == 5); }
- What is printed? c static int helper(int x) { return x 2; } int main(void) { printf("%d", helper(5)); }
- What is printed? c void outer(void) { int x = 1; void inner(void) { printf("%d", x); } inner(); } int main(void) { outer(); }
- What happens at link time? c // file1.c static int counter = 0; void increment(void) { counter++; } c // file2.c (compiled and linked with…
- What is printed? c int x = 10; void foo(void) { int x = 20; { int x = 30; printf("%d ", x); } printf("%d ", x); } int main(void) { foo()…
- What is printed? c void f(void) { int x = 5; x = x++ + ++x; printf("%d", x); } int main(void) { f(); }