在C ++中!(value)的用法是什么 [英] what is the use of !(value) in c++

查看:166
本文介绍了在C ++中!(value)的用法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题.
我有一个程序,其中有人用过!像这样

I have very simple problem.
I have a program in which some one used ! like this

int main()
{
    int a;
    a=5;
    if (!a==0)
        cout<<"yes";
    else
        cout<<"no";
    return 0;
}





can any one tell me what is used of ''!'' in this program?

推荐答案

!a==ba!=b不同.如上所述,检查您的优先级,它等于(!a) == (b),因此首先将a取反,然后将其与b进行比较.
!a==b is not the same as a!=b. Check you precedence, as said above, this is equal to (!a) == (b) so first a is negated then compared to b.


!不是操作符.
!a==0读为不等于0".这是一个不好的用法,但是有效,通常将其写为a!=0,读为"a不等于0"


在这种情况下,如果a为0,程序将打印否",否则将打印是".

另外,您会误输入.
! is the not operator.
!a==0 is read as "not a is equal to 0". This is a bad usage of it, but is valid, typically this would be written as a!=0, read "a is not equal to 0"


in this case the program prints "no" if a is 0, otherwise prints "yes".

Also, you misspelt return.


不要写这样的代码.

这样做:

if(!(a == 0))
{
...
}



if((!a)== 0)
{
...
}

我认为这是一种更好的方法.
Don''t write codes like that.

Do like this:

if( !(a==0))
{
...
}

or

if((!a) == 0)
{
...
}

I think it''s a better way.


这篇关于在C ++中!(value)的用法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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