ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
static int x = 5;
void f(void);
void f(void) { printf("%d", x); }
int main(void) { f(); }- A. 0
- B. 5
- C. Compilation error: static variable used before full definition
- D. Undefined behavior
Correct answer: B. 5
A forward declaration of a function (void f(void);) just announces its signature; the actual definition later can freely use file-scope variables like static x, which was already fully defined with value 5 before f is ever called. Prints 5.
More C Programming questions
- What is printed? c int a = 2, b = 3, c = 4; int r = a + b c - a / b; printf("%d", r);
- What is printed? c int a = 5; int r = a > 3 ? 10 : 20 + 5; printf("%d", r);
- What is printed? c int a = 4; int r = ~a + 1 == -a; printf("%d", r);
- What is printed? c int a = 3, b = 4, c = 5; int r = a + b == c ? 1 : 0; printf("%d", r);
- What is printed? c int a = 5; int b = a++, c = ++a; printf("%d %d %d", a, b, c);
- What is printed? c int x = 2, y = 3, z = 4; int r = x + y == z || x y == z + 2; printf("%d", r);