C 逻辑&&;逻辑| |运算符混淆
我在一个开源项目中发现了以下代码行:C 逻辑&&;逻辑| |运算符混淆,c,C,我在一个开源项目中发现了以下代码行: if(WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) 我无法理解此代码。根据代码流,这应该是(我确信): 两者都一样吗?不,它们不一样 如果(0)在C中为false,则任何其他值都被视为true 当你写作时: if(WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) 然后,如果WIFSTOPPED(status)返回0,
if(WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP)
我无法理解此代码。根据代码流,这应该是(我确信):
两者都一样吗?不,它们不一样
如果(0)
在C中为false,则任何其他值都被视为true
当你写作时:
if(WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP)
然后,如果WIFSTOPPED(status)
返回0,则另一方将由于错误而不被计算
就像在写:
if(WIFSTOPPED(status) != 0 && WSTOPSIG(status) == SIGTRAP)
这对你很有帮助:
- “not(A和B)”与“(not A)或(not B)”相同
- “not(A或B)”与“(not A)和(not B)”相同
不,它们不一样
如果(0)
在C中为false,则任何其他值都被视为true
当你写作时:
if(WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP)
然后,如果WIFSTOPPED(status)
返回0,则另一方将由于错误而不被计算
就像在写:
if(WIFSTOPPED(status) != 0 && WSTOPSIG(status) == SIGTRAP)
这对你很有帮助:
- “not(A和B)”与“(not A)或(not B)”相同
- “not(A或B)”与“(not A)和(not B)”相同
if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP)
相当于:
if (WIFSTOPPED(status) != 0 && WSTOPSIG(status) == SIGTRAP)
if (!(WIFSTOPPED(status) == 0 || WSTOPSIG(status) != SIGTRAP))
if (WIFSTOPPED(status) == SIGTRAP || WSTOPSIG(status) == SIGTRAP)
这相当于:
if (WIFSTOPPED(status) != 0 && WSTOPSIG(status) == SIGTRAP)
if (!(WIFSTOPPED(status) == 0 || WSTOPSIG(status) != SIGTRAP))
if (WIFSTOPPED(status) == SIGTRAP || WSTOPSIG(status) == SIGTRAP)
这显然不同于:
if ((WIFSTOPPED(status) == SIGTRAP) || (WSTOPSIG(status) == SIGTRAP))
请注意,C运算符的优先级有==
和=代码>的优先级高于&&
和| |
。这意味着前一行代码相当于:
if (WIFSTOPPED(status) != 0 && WSTOPSIG(status) == SIGTRAP)
if (!(WIFSTOPPED(status) == 0 || WSTOPSIG(status) != SIGTRAP))
if (WIFSTOPPED(status) == SIGTRAP || WSTOPSIG(status) == SIGTRAP)
您需要了解的逻辑运算符规则包括:
!(a && b) == !a || !b
及
您提供的两个代码示例不同。这个
if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP)
相当于:
if (WIFSTOPPED(status) != 0 && WSTOPSIG(status) == SIGTRAP)
if (!(WIFSTOPPED(status) == 0 || WSTOPSIG(status) != SIGTRAP))
if (WIFSTOPPED(status) == SIGTRAP || WSTOPSIG(status) == SIGTRAP)
这相当于:
if (WIFSTOPPED(status) != 0 && WSTOPSIG(status) == SIGTRAP)
if (!(WIFSTOPPED(status) == 0 || WSTOPSIG(status) != SIGTRAP))
if (WIFSTOPPED(status) == SIGTRAP || WSTOPSIG(status) == SIGTRAP)
这显然不同于:
if ((WIFSTOPPED(status) == SIGTRAP) || (WSTOPSIG(status) == SIGTRAP))
请注意,C运算符的优先级有==
和=代码>的优先级高于&&
和| |
。这意味着前一行代码相当于:
if (WIFSTOPPED(status) != 0 && WSTOPSIG(status) == SIGTRAP)
if (!(WIFSTOPPED(status) == 0 || WSTOPSIG(status) != SIGTRAP))
if (WIFSTOPPED(status) == SIGTRAP || WSTOPSIG(status) == SIGTRAP)
您需要了解的逻辑运算符规则包括:
!(a && b) == !a || !b
及
不,它们不一样
&&表示两个条件都必须为true,if语句才能为true
我不知道WIFSTOPPED(status)做什么,但是在第二个语句中,如果(WSTOPSIG(status)等于SIGTRAP,它不必等于SIGTRAP
而在第一条语句中,WIFSTOPPED(status)必须返回true和(WSTOPSIG(status)等于SIGTRAP否,它们是不同的
&&表示两个条件都必须为true,if语句才能为true
我不知道WIFSTOPPED(status)做什么,但是在第二个语句中,如果(WSTOPSIG(status)等于SIGTRAP,它不必等于SIGTRAP
而在第一条语句中,WIFSTOPPED(status)必须返回true和(WSTOPSIG(status)等于SIGTRAP,两者完全不同……当WIFSTOPPED(status)==0和
WSTOPSIG(状态)=SIGTRAP…当
WIFSTOPPED(状态)=SIGTRAP)或(WSTOPSIG(状态)=SIGTRAP)两者完全不同……当WIFSTOPPED(状态)==0和
WSTOPSIG(状态)=SIGTRAP…当
WIFSTOPPED(status)==SIGTRAP)或(WSTOPSIG(status)==SIGTRAP)这意味着:
如果WIFSTOPPED
为布尔值:
if ( (WIFSTOPPED(status) != false) && *(WSTOPSIG(status) == SIGTRAP) )
如果WIFSTOPPED是string
if ( (WIFSTOPPED(status) != '') && *(WSTOPSIG(status) == SIGTRAP) )
如果WIFSTOPPED
为整数:
if ( (WIFSTOPPED(status) != 0) && *(WSTOPSIG(status) == SIGTRAP) )
这意味着:
如果WIFSTOPPED
为布尔值:
if ( (WIFSTOPPED(status) != false) && *(WSTOPSIG(status) == SIGTRAP) )
如果WIFSTOPPED是string
if ( (WIFSTOPPED(status) != '') && *(WSTOPSIG(status) == SIGTRAP) )
如果WIFSTOPPED
为整数:
if ( (WIFSTOPPED(status) != 0) && *(WSTOPSIG(status) == SIGTRAP) )
From:“WIFSTOPPED(stat_val)
如果为当前已停止的子进程返回状态,则计算结果为非零值。”在排序中,它计算结果为布尔值。pVq(tVf)等于T,而p&q(fVt)等于F,因此它们将不相等。From:”WIFSTOPPED(stat_val)
如果为当前已停止的子进程返回状态,则计算结果为非零值。“在排序中,它计算结果为布尔值。pVq(tVf)等于T,p&q(fVt)等于F,因此它们不相等。