ProgrammingVery hardIBM · Deloitte pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
int a[10];
int *p = a + 2;
int *q = a + 7;
printf("%ld %ld", (long)(q - p), (long)((char*)q - (char*)p));- A. 5 5
- B. 5 20
- C. 20 5
- D. 5 40
Correct answer: B. 5 20
q - p as int pointers counts elements: 5. Casting both to char* before subtracting counts bytes: 5 * sizeof(int) = 20 on a 4-byte-int ABI. The two subtractions scale differently because pointer arithmetic divides the raw byte difference by the pointee's size.
More C Programming questions
- What is printed, assuming no undefined-behavior crash occurs on this platform? c int a[3] = {1,2,3}; int b[3] = {4,5,6}; int p = a + 3…
- What is printed? c int x = 5; void vp = &x; int ip = (int)vp; (ip)++; printf("%d", (int)vp);
- What is printed? c int a[5] = {1,2,3,4,5}; int p = a; for (p = a; p < a + 5; p++) { if (p == a + 2) continue; printf("%d", p); }
- What is printed? c int a = 1, b = 2, c = 3; int arr[3] = {&a, &b, &c}; int p = arr; int pp = &p; pp += 100; (pp)++; pp += 100; printf("%d…
- What is printed? c int a[6] = {1,2,3,4,5,6}; int p = a + 1; int q = a + 4; int diff = q - p; q -= diff / 3 2; printf("%d %d", diff, q);
- Assume a 64-bit LP64 ABI. What is printed? c int a[3][2] = {{1,2},{3,4},{5,6}}; int (p)[2] = a + 1; int q = (int)p; q++; printf("%d %d"…