Subscribe

Enter your email address:

Categories

 

February 2012
M T W T F S S
« Dec    
 12345
6789101112
13141516171819
20212223242526
272829  

Archives

Disclaimer

© 2009 Zero Intellect. All rights reserved. The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway. This material is not sponsored or endorsed by any of the vendors mentioned in this website and their Logos are trademarks of their own and their affiliates.

C++ Logical Negation Operator

 

The logical negation operator ! [exclamation] used in C++ and many other languages. However the return value of the operator is overlooked by many people.

 

The syntax of the operator follows is

 

! cast-expression

 

Where, the cast-expression operand must be of scalar type (returning only a single value, not multiple values like an array)

 

The result is of type int and is the logical negation of the operand:

 

 0 if the operand is nonzero

 1 if the operand is zero

 

To demonstrate the usage of the Logical Negation Operator, the simple program below was written

 

# include <iostream.h>

# include <conio.h>

 

void main()

{

   clrscr();

 

   cout<<”\nThe expression !0 equals : “<<!0;

   cout<<”\nThe expression !0.7 equals : “<<!0.54;

   cout<<”\nThe expression !4 equals : “<<!4;

   cout<<”\nThe expression !-4 equals : “<<!-4;

 

   getch();

}

 

 The output of the program is an integer value as expected, the negation of the value 0 (Zero) is equal to 1 and the negation operator applied anything else is equal to 0 (Zero)

 

 The expression !0 equals : 1

The expression !0.7 equals : 0

The expression !4 equals : 0

The expression !-4 equals : 0

 

 To summarize, the things to note are that the return type of the operator is int (integer) and the cast-expression operand must be of scalar type

 

Happy Coding

1 comment to C++ Logical Negation Operator