[Solved] How to check if `strcmp` has failed?


There is no defined situation where strcmp can fail.

You can trigger undefined behavior by passing an invalid argument, such as:

  • a null pointer
  • an uninitialized pointer
  • a pointer to memory that’s been freed, or to a stack location from a function that’s since been exited, or similar
  • a perfectly valid pointer, but with no null byte between the location that the pointer points to and the end of the allocated memory range that it points into

but in such cases strcmp makes absolutely no guarantees about its behavior — it’s allowed to just start wiping your hard drive, or sending spam from your e-mail account, or whathaveyou — so it doesn’t need to explicitly indicate the error. (And indeed, the only kind of invalid argument that strcmp really could detect and handle, in a typical C or C++ implementation, is the null-pointer case, which you could just as easily check before calling strcmp. So an error indicator would not be useful.)

solved How to check if `strcmp` has failed?