ProgrammingHardWipro · Cognizant pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
struct Flags {
unsigned isActive : 1;
unsigned isAdmin : 1;
unsigned level : 6;
};
struct Flags f = {1, 0, 42};
printf("%u %u %u", f.isActive, f.isAdmin, f.level);- A. 1 0 0
- B. 1 0 42
- C. 1 1 42
- D. 0 0 42
Correct answer: B. 1 0 42
Each bitfield independently stores its initializer value, clipped to its declared width: isActive(1 bit) holds 1, isAdmin(1 bit) holds 0, level(6 bits, range 0-63) holds 42 exactly (fits within 6 bits), so reading them back gives exactly what was assigned: 1 0 42.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c union Data { int i; char c; }; union Data d; d.i = 0x12345678; printf("%x", (unsigned…
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; void tryModify(struct Point p) { p.x = 999; } int main(void) {…
- Assume a 64-bit LP64 ABI. What is printed? c struct Rect { struct { int x, y; } topLeft; struct { int x, y; } bottomRight; }; struct Rect…
- Assume a 64-bit LP64 ABI. What is printed? c typedef struct { int code; char message[20]; } Error; Error makeError(int c, const char msg)…
- Assume a 64-bit LP64 ABI. What is printed? c struct Counter { int value; }; void increment(struct Counter c) { c->value++; } int…
- Assume a 64-bit LP64 ABI. What is printed? c struct Pair { int a, b; }; struct Pair swapPair(struct Pair p) { int tmp = p.a; p.a = p.b…