ProgrammingHardWipro · Cognizant pattern

What is the behavior?

What is the behavior?
int a = 10;
int b = a << 32;
printf("%d", b);
  1. A. Guaranteed to print 0
  2. B. Undefined behavior: shifting by an amount >= the width of the type (32 for a 32-bit int) is not permitted
  3. C. Guaranteed to print a unchanged, 10
  4. D. Compilation error

Correct answer: B. Undefined behavior: shifting by an amount >= the width of the type (32 for a 32-bit int) is not permitted

The C standard requires the shift amount to be less than the width of the promoted left operand's type; shifting a 32-bit int by 32 or more is undefined behavior, not a guaranteed zero (even though some hardware shift instructions happen to produce 0 or wrap the shift count, behavior varies by architecture).

More C Programming questions

Exams that ask this topic