我的印象是比较运算符没有为C风格的字符串定义,这就是为什么我们使用类似的东西strcmp().因此,以下代码在C和C++中是非法的:
if("foo" == "foo"){
printf("The C-style comparison worked.\n");
}
if("foo" == "bob"){
printf("The C-style comparison produced the incorrect answer.\n");
} else {
printf("The C-style comparison worked, strings were not equal.\n");
}
Run Code Online (Sandbox Code Playgroud)
但我在使用GCC和VS 2015的两个Codeblock中测试了它,编译为C和C++.两者都允许代码并产生正确的输出.
比较C风格的字符串是否合法?或者它是否是允许此代码工作的非标准编译器扩展?
如果这是合法的,那么为什么人们strcmp()在C中使用?