ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int i = 1;
int arr[5] = {0};
int x = i + i++;
printf("%d", x);- A. 3, since left operand reads first
- B. 2, since the increment happens first
- C. Compilation error
- D. Undefined behavior: i is both read for its value and modified (i++) with no sequence point separating them
Correct answer: D. Undefined behavior: i is both read for its value and modified (i++) with no sequence point separating them
i + i++ reads i's value twice (once directly, once via the post-increment's 'old value' read) and also modifies it once, all without a sequence point ordering these accesses relative to each other, which the C standard classifies as undefined behavior — not just 'implementation-defined' which would still have a guaranteed (if unspecified) answer per compiler.
More C Programming questions
- 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);
- 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); }