ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
void outerFunc(void) {
int x = 10;
{
int y = 20;
printf("%d %d ", x, y);
}
// printf("%d", y); // line X
}
int main(void) { outerFunc(); }What happens if line X is uncommented?- A. Compilation error: y is out of scope outside the block in which it was declared
- B. It compiles and prints 0
- C. It compiles and prints a garbage value
- D. It compiles fine and prints 20 again
Correct answer: A. Compilation error: y is out of scope outside the block in which it was declared
y's scope is limited to the inner block {}; once that block ends, y no longer exists as a visible identifier — referencing it afterward is a compile-time 'undeclared identifier' error, not a run-time issue.
More C Programming questions
- What is printed? c int idgenerator(void) { static int nextid = 100; return nextid++; } int main(void) { int a = idgenerator(); int b =…
- What is printed? c int applyTwice(int (f)(int), int x) { return f(f(x)); } int square(int x) { return x x; } int main(void) { printf("%d"…
- What is printed? c int main(void) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == i) continue; printf("%d%d ", i…
- What is printed? c int main(void) { int x = 2; switch (x) { case 1: case 2: case 3: printf("low "); break; case 4: case 5: printf("high…
- What is printed? c int main(void) { int arr[5] = {1, 2, 3, 4, 5}; int sum = 0; for (int i = 0; i < 5; i += 2) { sum += arr[i]; }…
- What is printed? c int fact(int n); int main(void) { printf("%d", fact(5)); } int fact(int n) { return n <= 1 ? 1 : n fact(n - 1); }