因为C语言中,所有 非0值 都表示真。所以!非0值 = 0,而!0 = 1。
所以!!非0值 = 1,而!!0 = 0。
示例如下:
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int a = 3;
int b = 4;
cout << "false " << false << " true " << true << endl;
cout << "a " << a << endl;
cout << "b " << b << endl;
cout << endl;
cout << "(a<b) " << (a < b) << endl;
cout << "!(a<b) " << !(a < b) << endl;
cout << "!!(a<b) " << !!(a < b) << endl;
cout << endl;
cout << "(a>b) " << (a > b) << endl;
cout << "!(a>b) " << !(a > b) << endl;
cout << "!!(a>b) " << !!(a > b) << endl;
cout << endl;
cout << "!3 " << !3 << endl;
cout << "!!3 " << !!3 << endl;
cout << endl;
cout << "!0 " << !0 << endl;
cout << "!!0 " << !!0 << endl;
cout << endl;
cout << "!(-3) " << !(-3) << endl;
cout << "!!(-3) " << !!(-3) << endl;
cout << endl;
return 0;
}
输出结果:
false 0 true 1
a 3
b 4
(a<b) 1
!(a<b) 0
!!(a<b) 1
(a>b) 0
!(a>b) 1
!!(a>b) 0
!3 0
!!3 1
!0 1
!!0 0
!(-3) 0
!!(-3) 1