ProgrammingHardTCS NQT · Infosys pattern
Assume 32-bit int. What is printed?
Assume 32-bit int. What is printed?
int x = 3;
int result = x++ + x++ - x++;
printf("%d", result);
printf(" valid: %s", "no, undefined behaviour");- A. The numeric result shown by result is not something any particular value can be trusted to equal, since x is modified three times with no sequence points between them
- B. 3
- C. Compile-time error
- D. 9
Correct answer: A. The numeric result shown by result is not something any particular value can be trusted to equal, since x is modified three times with no sequence points between them
x++ + x++ - x++ performs three separate increment side effects on the same object x within one expression, with no sequence points separating them — classic undefined behaviour, regardless of whatever a given compiler happens to print for result.
More C Programming questions
- What is printed? c int main(void) { int a[3] = {1, 2, 3}; int b[3] = {1, 2, 3}; printf("%d", a == b); }
- What is printed? c int main(void) { char a[] = "test"; char b[] = "test"; printf("%d %d", a == b, strcmp(a, b) == 0); }
- What is printed? c int main(void) { int x = 100; long y = x; long long z = y 1000000000L; printf("%lld", z); }
- What is printed? c int main(void) { int n = 5; int result = 1; int i = 1; while (i <= n) { result = i; i++; } printf("%d", result); }
- Assume 32-bit int. What is printed? c int a = 4, b = 7; int max = (a > b) ? a : b; int min = (a < b) ? a : b; printf("%d %d %d", max, min…
- What happens when this runs? c int p; if (p != NULL) { printf("%d", p); } else { printf("null"); }