Search code examples
cstringstrcmp

C- Comparison operator instead of strcmp


What will happen if I use comparison operators to compare strings instead of strcmp in C? Will it compare its ASCII value and return results?


Solution

  • It will compare the addresses of the two pointers.

    so:

    char* a = "hello";
    char* b = "test";
    char* c = "hello";
    char* d = a;
    
    a == d; // true
    a == b; // false
    a == c; // true or false, depending on the compiler's behavior.
    

    The third example will be true if the compiler decides to recycle the actual string data for "hello", but it has no obligation to do so.