ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
int main(void) {
int a = 10;
int *p = &a;
int **pp = &p;
printf("%d %d %d", a, *p, **pp);
a = 20;
printf(" %d %d %d", a, *p, **pp);
}- A. 20 20 20 20 20 20
- B. 10 20 10 20 20 20
- C. 10 10 10 20 20 20
- D. 10 10 10 10 10 10
Correct answer: C. 10 10 10 20 20 20
p and pp both ultimately refer back to a; any change to a is immediately visible through every level of indirection, since they're not copies but references to the same memory: before the change all three read 10, after `a=20` all three read 20.
More C Programming questions
- What is printed? c enum Color { RED, GREEN, BLUE }; enum Color c = GREEN; printf("%d", c); enum Status { OK = 1, FAIL = 5, PENDING }…
- What is printed? c int main(void) { int x = 1; int p = &x; p += 1; (p)++; ++p; printf("%d", x); }
- What is printed? c int main(void) { int x = 0; int y = (x = 5, x + 1); printf("%d %d", x, y); }
- What is printed? c int main(void) { int arr[3] = {1, 2, 3}; int p = arr; for (int i = 0; i < 3; i++, p++) { printf("%d ", p); } }
- Assume 32-bit int. What is printed? c int x = 7; int y = x > 5 && x < 10 ? x 2 : x + 1; printf("%d", y);
- What happens when this runs? c int p = malloc(10 sizeof(int)); for (int i = 0; i <= 10; i++) { p[i] = i; } free(p);