&和和&&在C / C ++中 [英] Difference between & and && in C/C++
本文介绍了&和和&&在C / C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每个人都好......
单个& 和双&& <之间有什么区别? / b>在 C / C ++ 。
我尝试了什么:
我运行此代码。
Hellow every one...!
What is the difference between single & and double && in C/C++.
What I have tried:
I run this code.
bool f() { std::cout << "f"; return false; }
bool g() { std::cout << "g"; return true; }
int main()
{
f() && g();
cout << endl;
f() & g();
}
The out put is:
f
fg
任何人都可以解释一下。
Can any one please explain this.
推荐答案
&&
运算符如果第一个操作数变为false,则不计算第二个操作数。按位&
运算符总是评估它们的操作数。
在你的情况下& ;&
函数f()
在屏幕上执行并打印f
并返回错误的第二个功能没有被执行。
在第二种情况下,当你打电话给&
时它是按位和操作符它评估两个运算符,即调用f()
和g()
并从fust函数和f中打印f来自secong功能。因此输出fg
之间进行差异阅读 [ ^ ]
The&&
operator doesn’t evaluate second operand if first operand becomes false. The bitwise&
operator always evaluate their operands.
In your case on&&
functionf()
execute and printf
on screen and since it return false second function not executed.
In second case when you call&
it is bit-wise and operator it evaluate both the operators ie, call bothf()
andg()
and print f from fust function anf g from secong function. hence output isfg
for more detail read this[^]
按位读取和逻辑运算符: C和C ++中的运算符 - 维基百科 [ ^ ]
For& (逻辑运算符),应用了一个快捷方式。
你有公式'f()&& g()'变为'假&& g()'但是在这一点上,无论'g()'的值,全局结果都是false。
Read about bitwise and logical operators: Operators in C and C++ - Wikipedia[^]
For & (logical operator), a shortcut is applied.
You have formula 'f() && g()' which became 'false && g()' but at this point, no matter the value of 'g()', the global result is false.
文档怎么样?
逻辑运营商 - cppreference.com [ ^ ]
算术运算符 - cppreference.com [ ^ ]
What about the documentation?
Logical operators - cppreference.com[^]
Arithmetic operators - cppreference.com[^]
这篇关于&和和&&在C / C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文