ProgrammingVery hardProduct company OA · GATE CS
Compiled as C99 or later, what is printed?
Compiled as C99 or later, what is printed?
int i = 1;
for (int i = 5; i < 7; i++) {
int i = 10;
printf("%d ", i);
}
printf("%d", i);- A. 10 10 1
- B. 5 6 1
- C. Compilation error: i is redeclared
- D. 10 10 7
Correct answer: A. 10 10 1
In C the for statement is a block and its body is a separate, nested block, so the body may declare its own i, which hides the loop counter. The loop still runs twice (i = 5, 6) and prints the inner 10 each time; the outer i is untouched. C++ rejects the redeclaration, which is the trap.
More C Programming questions
- What is printed? c printf("%zu", sizeof("1234"));
- What is printed? c int a[8] = {0}; for (int i = 1; i < 8; i++) a[i] = a[i / 2] + (i % 2); printf("%d %d %d", a[5], a[6], a[7]);
- What is printed? c int a[] = {2, 1, 3, 2, 4, 1}, best = 0, s = 0, lo = 0; for (int hi = 0; hi < 6; hi++) { s += a[hi]; while (s > 6) s -=…
- What is wrong with this code? c char p = malloc(10); p++; free(p);
- What is printed? c int x = 5; #define x (x + 1) printf("%d", x);
- What is printed? c int c = 0; for (int i = 1; i <= 16; i = 2) for (int j = 0; j < i; j++) c++; printf("%d", c);