ProgrammingVery hardProduct company OA
What is printed? Assume int is 32-bit, two's complement.
What is printed? Assume int is 32-bit, two's complement.
int x = INT_MAX;
int y = x + 1;
printf("%d", y);- A. 2147483648, guaranteed by the standard
- B. 0, guaranteed by the standard
- C. Compilation error: constant overflow detected
- D. Undefined behavior (signed overflow), though INT_MIN is commonly observed in practice
Correct answer: D. Undefined behavior (signed overflow), though INT_MIN is commonly observed in practice
Signed integer overflow is undefined behavior in C (unlike unsigned, which wraps modulo 2^n by definition). Many compilers emit wrap-around two's-complement arithmetic in practice (giving INT_MIN), but optimizers may also assume overflow never happens and transform the code in surprising ways, so it is not guaranteed.
More C Programming questions
- What is the behavior? c int p = malloc(10 sizeof(int)); free(p); printf("%d", p[0]);
- What is printed? c int i = 1; int arr[5] = {0}; int x = i + i++; printf("%d", x);
- What is the behavior? c int arr[5] = {1,2,3,4,5}; int p = arr + 5; printf("%p", (void)p); printf("%d", p);
- What is the behavior? c char buf[5]; strcpy(buf, "hello world"); printf("%s", buf);
- What is the behavior? c int a = 10; int b = a << 32; printf("%d", b);
- What is the behavior? c const int x = 10; int p = (int)&x; p = 20; printf("%d", x);