Using the equality operator == to compare two strings for equality in C

前端 未结 9 670
悲&欢浪女
悲&欢浪女 2020-11-22 16:38
int main (int argc, **argv)
{
       if (argv[1] == \"-hello\")
            printf(\"True\\n\");
       else
            printf(\"False\\n\");
}
         


        
9条回答
  •  情话喂你
    2020-11-22 17:25

    You can't compare strings in C with ==, because the C compiler does not really have a clue about strings beyond a string-literal.

    The compiler sees a comparison with a char* on either side, so it does a pointer comparison (which compares the addresses stored in the pointers)

提交回复
热议问题