ProgrammingVery hardProduct company OA

What is printed, assuming no undefined-behavior crash occurs on this platform?

What is printed, assuming no undefined-behavior crash occurs on this platform?
int a[3] = {1,2,3};
int b[3] = {4,5,6};
int *p = a + 3;
int *q = b;
printf("%d", p == q);
  1. A. 1, since both addresses are the last byte of a's storage
  2. B. 0, guaranteed by the standard
  3. C. 1, guaranteed by the standard
  4. D. Unspecified/implementation-defined (comparing one-past-the-end of a with the start of an unrelated array)

Correct answer: D. Unspecified/implementation-defined (comparing one-past-the-end of a with the start of an unrelated array)

Comparing pointers to two different objects (even one-past-the-end of one) for equality is well-defined only in that it compares addresses, but there is no guarantee a's one-past-end address differs from or equals b's start; the standard permits either outcome depending on memory layout, and C gives no guarantee about relative placement of unrelated arrays. It is not UB to compare them with == (unlike <, > across unrelated objects), but the result is not fixed by the standard.

More C Programming questions

Exams that ask this topic