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);
  1. A. 1 0 15
  2. B. 1 0 -1
  3. C. 0 0 15
  4. 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

Exams that ask this topic