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);
}- A. 100 100
- B. 5 100
- C. 100 5
- 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
- Assume a 64-bit LP64 ABI. What is printed? c int allocateArray(int n) { int arr = malloc(n sizeof(int)); for (int i = 0; i < n; i++)…
- Assume a 64-bit LP64 ABI. What is printed? c int compare(const void a, const void b) { return (const int )a - (const int )b; } int…
- Assume a 64-bit LP64 ABI. What is printed? c void fillWithPointer(int pp, int value) { pp = malloc(sizeof(int)); pp = value; } int…
- Assume a 64-bit LP64 ABI. What is printed? c char str[] = "hello"; char p = str; while (p != '0') { p = p - 32; p++; } printf("%s", str);
- Assume a 64-bit LP64 ABI. What happens when this runs? c char str = "hello"; str[0] = 'H'; printf("%s", str);
- Assume a 64-bit LP64 ABI. What is printed? c struct Flags { unsigned isActive : 1; unsigned isAdmin : 1; unsigned level : 6; }; struct…