Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have very simple problem.
I have a program in which some one used ! like this

C++
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?
Posted
Updated 30-Oct-11 22:27pm
v4
Comments
Nagy Vilmos 26-Oct-11 8:13am    
Formatted snippet.

!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.
 
Share this answer
 
Comments
elgaabeb 27-Oct-11 5:26am    
This is not a bitwise negation ... it is equivalent to (!a)== 0 ... this is another usage of logical negation to check if a variable is alive and reachable by the compiler.
Nagy Vilmos 27-Oct-11 7:05am    
"it is equivalent to (!a)== 0" - that is exactly what I said.
eli551 25-Mar-12 5:06am    
i have a simple problem too. how can check equality twain sentence?
eli551 31-Mar-12 12:18pm    
i want check this:
if( s == roze smith )
{
-------;
}
what should i write?
! 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"

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

Also, you misspelt return.
 
Share this answer
 
v2
Comments
Espen Harlinn 26-Oct-11 6:23am    
http://en.cppreference.com/w/cpp/language/operator_precedence
(!a==0) => ((!a) == 0) ! has higher precedence than ==
The case (!a==2) can not be rewritten as a != 2
RaisKazi 26-Oct-11 6:29am    
That link is nice. This question was misunderstood by me initially.:)
Andrew Brock 26-Oct-11 6:35am    
I should have remembered that.
By luck, my answer is still correct because he is checking for equivalence to 0, although the explanation is a quite misleading.
Espen Harlinn 26-Oct-11 6:45am    
But you where right in this case, so my 5 :)
RaisKazi 26-Oct-11 6:23am    
My 5!
Don't write codes like that.

Do like this:

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

or

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

I think it's a better way.
 
Share this answer
 
It's also used when the successful return code from an API is zero (0) and the error return code can have different values (1 for error X, 2 for error Y, .... )

(I think I've seen this in some win32 API).

for example ...

C++
/// \return zero on success.
/// \return 1 allocation error.  
/// \return 2 division by zero.
int f( );

int f( )
{
  if ( ... )
  {
    // cannot allocate.
    return 1;
  }

  if ( ... )
  {
    // division by zero
    return 2;
  }
  // success
  return 0;
}
 
Share this answer
 
Comments
Chuck O'Toole 26-Oct-11 14:31pm    
that would be a good example except is hasn't got a single ! in it :)
Maximilien 26-Oct-11 15:05pm    
oh!!!
elgaabeb 27-Oct-11 5:32am    
sorry but i can't see any convenience ! :)
Hi there,

This syntaxe of logical NOT is used to check if a variable is "alive" or not.
When a variable is initialized then it is considered as alive by the compiler.
It can then participate in any necessary operation or treatment.

(!a==0) is equivalent to ((!a) == 0)

i'll rewrite the message to be more significant for you example :

C#
int a;
a=5;
if (!a==0)
    cout<<"yes VARIABLE IS ALIVE";
else
    cout<<"no VARIABLE IS NOT ALIVE";



Check this link for more explanation paragraph "The Logical Not Operator !":

http://www.functionx.com/cpp/Lesson08.htm[^]

best regards !
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900