ProgrammingHardWipro · Cognizant pattern
What is the behavior?
What is the behavior?
int a = 10;
int b = a << 32;
printf("%d", b);- A. Guaranteed to print 0
- B. Undefined behavior: shifting by an amount >= the width of the type (32 for a 32-bit int) is not permitted
- C. Guaranteed to print a unchanged, 10
- 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
- What is the behavior? c const int x = 10; int p = (int)&x; p = 20; printf("%d", x);
- What is the behavior? c int a[3] = {1, 2, 3}; memcpy(a, a + 1, 2 sizeof(int)); printf("%d %d %d", a[0], a[1], a[2]);
- What is the behavior? c void f(int n) { char buf[n]; for (int i = 0; i <= n; i++) buf[i] = 'a'; } int main(void) { f(10); }
- What is printed? c // file1.c int x = 10; // file2.c (same program, separate translation unit) extern int x; void show(void) {…
- What is printed? c int x = 100; void f(void) { int x = 5; { int x = 1; printf("%d ", x); } printf("%d ", x); } int main(void) { f()…
- What is printed? c int x = 5; int f(void) { int x = x + 1; return x; } int main(void) { printf("%d", f()); }