ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
int x = 100;
void f(int *p) {
int y = 200;
p = &y;
}
int main() {
int *p = &x;
f(p);
printf("%d", *p);
}- A. 200
- B. 100
- C. Compilation error
- D. Undefined behavior: y no longer exists
Correct answer: B. 100
p is passed BY VALUE (a copy of the pointer), so reassigning the local parameter p inside f to &y only changes that function's own copy — main's p still points at x, unaffected. Printing *p gives 100.
More C Programming questions
- What is printed? c int arr[3] = {1, 2, 3}; int (p)[3] = &arr; printf("%d %d", (p)[1], (p + 2));
- What is printed (64-bit LP64)? c int a = 10; void vp = &a; int ip = (int )vp; char cp = (char )&a; printf("%d %zu %zu", ip, sizeof(vp)…
- What is printed? c int a = 5; int p = &a; int q = p; p = NULL; printf("%d %d", q, p == NULL);
- What is printed? c int a[2][3] = {{1,2,3},{4,5,6}}; int p = &a[0][0]; printf("%d ", (p + 4)); int (rows)[3] = a; printf("%d", ((rows +…
- What is printed? c int global = 100; int getGlobal() { return &global; } int main() { int p = getGlobal(); p = 200; printf("%d %d"…
- What is printed? c void swap(int a, int b) { int temp = a; a = b; b = temp; } int main() { int x = 1, y = 2; int p = &x, q = &y; swap(&p…