C:是否有“懒惰的评价"?当使用 &&运算符,如在 C++ 中? [英] C : is there "lazy evaluation" when using && operator, as in C++?

查看:17
本文介绍了C:是否有“懒惰的评价"?当使用 &&运算符,如在 C++ 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这看起来是否正确:

I would like to know if this looks correct :

while((next !=NULL) && (strcmp(next->name, some_string) < 0) {
    //some process
}

我的意思是,如果 next 是 NULL,那么表达式的第二部分就不会被编译器测试?我听说在 C++ 中就是这种情况(但我什至不确定).

I mean, if next is NULL, then the second part of the expression won't be ever tested by the compiler? I have heard that in C++ it's the case (but I'm not even sure of it).

有人可以确认我不会在某些编译器上遇到奇怪的错误吗?

Can someone confirm me that I won't get strange errors on some compilers with that?

推荐答案

&& 短路了,你使用正确.
如果 next is NULL 字符串比较永远不会发生.

Yes && is short circuited and you are using it correctly.
If next is NULL string compare will never happen.

这篇关于C:是否有“懒惰的评价"?当使用 &amp;&amp;运算符,如在 C++ 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆