I'm trying to understand UB but it seems really fucking retarded. Say I have a string defined as const char [N] where N is a numeric lit known at compile time. If I write something like this

#define N 256
const char string[N] = { ... };
...
size_t i = N-1;
...
if ((string[i] == something) && (i+1 < N) && (string[i+1] == something_else)) {
do_thing;
}


because i+1 in string[i+1] being >= N would result in undefined behavior, the compiler can make the assumption that i+1 will never be greater than or equal to N and that the second check in the if therefore can safely be factored away so the resulting code the compiler will generate looks like
if ((string[i] == something) && (string[i+1] == something_else)) {

Am I understanding that right? If so, what the FUCK? How do I even write this so that it works?