ProgrammingVery hardGATE CS · Product company OA
With int i = 1, x; int a[5] = {0}; which of these statements have undefined behaviour? (i) i = i++ + 1; (ii) a[i] = i++; (iii) i = (i, i + 1); (iv) x = (i++, i);
- A. (i) and (ii) only
- B. (i) only
- C. All four
- D. (i), (ii) and (iv) only
Correct answer: A. (i) and (ii) only
(i) modifies i twice with no sequence point between (the ++ and the assignment). (ii) reads i to pick the element and modifies it in i++, unsequenced. The comma operator in (iii) and (iv) is a sequence point: its left side finishes before its right side is evaluated, so both are well defined.
More C Programming questions
- Which of the objects A to E have static storage duration (they live for the whole run of the program, in the data or BSS segment)? c int…
- What is printed? c int cmp(const void a, const void b) { return (const int )b - (const int )a; } int main(void) { int v[] = {4, 9, 1, 7}…
- What is printed? c printf("%d %d %d", atoi("42abc"), atoi(" -7x"), atoi("x9"));
- What is printed? c int a = 0, b = 0; int n = sscanf("12 abc 7", "%d %d", &a, &b); printf("%d %d %d", n, a, b);
- With 4-byte int, what is printed? c int p = (int[]){10, 20, 30}; p[1] += 5; printf("%d %zu", p[1] + p[2], sizeof((int[]){10, 20, 30}) /…
- What is printed? c #define COUNT(...) (sizeof((int[]){VAARGS}) / sizeof(int)) printf("%zu %zu", COUNT(1, 2, 3), COUNT(7));