ProgrammingHardWipro · Cognizant pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
int global = 10;
int *getPtr(void) {
    return &global;
}
int main(void) {
    int *p = getPtr();
    *p = 20;
    printf("%d %d", global, *getPtr());
}
  1. A. 10 10
  2. B. 20 20
  3. C. 10 20
  4. D. 20 10

Correct answer: B. 20 20

global has static storage duration, so its address remains valid for the entire program; returning &global is perfectly safe (unlike returning the address of a local variable). *p = 20 modifies the actual global, and getPtr() called again afterward returns the same still-valid address, reading the now-updated value: 20 20.

More C Programming questions

Exams that ask this topic