ProgrammingHardWipro · Cognizant pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
typedef struct {
int x;
int y;
} Point;
Point p1 = {1, 2};
Point p2 = {1, 2};
printf("%d", p1.x == p2.x && p1.y == p2.y);
printf(" %d", memcmp(&p1, &p2, sizeof(Point)) == 0);- A. 1 1
- B. 1 0
- C. 0 1
- D. Compile-time error: structs cannot be compared with memcmp
Correct answer: A. 1 1
Comparing members individually (p1.x==p2.x && p1.y==p2.y) is always a reliable way to check value equality, giving true here (1). memcmp on the raw bytes also happens to agree here (1) since this particular Point struct has no padding bytes between int x and int y (both are naturally aligned back-to-back) and no uninitialised gaps to contain indeterminate garbage, so a byte comparison is safe in this specific case.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c struct Node { int value; struct Node next; }; struct Node a = {1, NULL}; struct Node b = {2…
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; struct Point origin = {0, 0}; struct Point p1 = &origin; struct…
- Assume a 64-bit LP64 ABI. What is printed? c struct Config { int verbose : 1; int debug : 1; int level : 4; }; struct Config cfg = {0}…
- What is printed? c int sumDigits(int n) { if (n == 0) return 0; return (n % 10) + sumDigits(n / 10); } int main(void) { printf("%d"…
- What is printed? c int countDown(int n) { if (n < 0) return 0; printf("%d ", n); return countDown(n - 1); } int main(void) { countDown(3); }
- What is printed? (Assume a 64-bit LP64 ABI, where long is 8 bytes.) c long fact(int n) { return n <= 1 ? 1 : n fact(n - 1); } int…