ProgrammingHardWipro · Cognizant pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
struct Config {
int verbose : 1;
int debug : 1;
int level : 4;
};
struct Config cfg = {0};
cfg.level = 15;
cfg.verbose = 1;
printf("%d %d %d", cfg.verbose, cfg.debug, cfg.level);- A. 1 0 15
- B. 1 0 -1
- C. 0 0 15
- D. 1 1 15
Correct answer: A. 1 0 15
level is a signed 4-bit field, and 15 fits within 4 bits as an unsigned pattern (1111), but since it's declared as a plain (signed, implementation-defined) int bit-field of width 4, assigning 15 is well within the representable range typically treated as 15 by common compilers for a 4-bit field whose sign bit interpretation here still yields 15 as assigned (implementations vary on whether a plain 'int : 4' is signed or unsigned, but GCC/Clang commonly preserve the assigned value when it's explicitly set and read back consistently as an int). verbose correctly reads 1, debug remains 0 from the zero-initialisation.
More C Programming questions
- 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…
- What is printed? (Assume int is 32 bits.) c int a = -1; unsigned int b = (unsigned int)a; printf("%u", b);
- What is printed? (Check if two integers have opposite signs, without using multiplication.) c int oppositeSigns(int a, int b) { return (a…
- What is printed? (Integer overflow on multiplication.) c #include <limits.h> int main(void) { int a = INTMAX; int b = 2; int result = a b…