strcmp behaviour

后端 未结 3 2024
死守一世寂寞
死守一世寂寞 2021-02-05 14:37

When I run the following code:

#include 

int main(int argc, char *argv[])
{
    int p = 0;

    p = strcmp(NULL,\"foo\");

    return 0;
}
         


        
3条回答
  •  有刺的猬
    2021-02-05 15:02

    You are probably using optimization options when compiling. Since the result of strcmp() in the second snippet is ignored the compiler eliminates this function call and this is why your program does not crash. This call can be eliminated only because strcmp() is an intrinsic function, the compiler is aware that this function does not have any side effects.

提交回复
热议问题