ProgrammingModerateWipro · Capgemini pattern
What is printed?
What is printed?
int x = 1;
{
int x = 2;
{ x = 3; }
printf("%d ", x);
}
printf("%d", x);- A. 3 1
- B. 2 3
- C. 3 3
- D. 2 1
Correct answer: A. 3 1
The inner block declares a new x that hides the outer one. The innermost block declares nothing, so x = 3 assigns to the nearest x, the inner one, which prints 3. The outer x was never touched and prints 1.
More C Programming questions
- With 4-byte int, what is printed? c int a[5]; printf("%td %td", (char )(a + 1) - (char )a, (char )(&a + 1) - (char )a);
- What is printed? c printf("%d", 010 + 0x10 + 10);
- What happens? c for (unsigned int i = 3; i >= 0; i--) printf("%u ", i);
- What is printed? c char s[] = "abc0def"; printf("%zu %zu", sizeof(s), strlen(s));
- What is printed? c printf("%zu", strlen("atbn"));
- What is printed? c int a = -1, b = 1; if (a > 0) if (b > 0) printf("X"); else printf("Y"); printf("Z");