ProgrammingHardTCS NQT · Infosys pattern

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

Assume a 64-bit LP64 ABI. What is printed?
void modify(int x) {
    x = 100;
}
void modifyPtr(int *x) {
    *x = 100;
}
int main(void) {
    int a = 5;
    modify(a);
    printf("%d ", a);
    modifyPtr(&a);
    printf("%d", a);
}
  1. A. 100 100
  2. B. 5 100
  3. C. 100 5
  4. D. 5 5

Correct answer: B. 5 100

C always passes arguments BY VALUE: modify(a) receives a copy of a's value, and modifying that local copy (x=100) has no effect on the caller's a, which stays 5. modifyPtr(&a) instead receives the address of a, and *x = 100 writes through that pointer directly into a's own memory, actually changing it to 100.

More C Programming questions

Exams that ask this topic