bool_fun.cpp

来自「实用的程序员编程工具」· C++ 代码 · 共 44 行

CPP
44
字号
#include <iostream.h>

bool func(void)
 {    				// Function returns a bool type
   return false;
   //  return NULL;  // NULL is converted to Boolean false
 }

void main(void) 
{
   bool val = false;  // Boolean variable
   int i = 1;         // i is neither Boolean-true nor Boolean-false
   int g = 3;
   int *iptr = 0;     // null pointer
   float j = 1.01;    // j is neither Boolean-true nor Boolean-false

   if (i == true)
      cout << "True: value is 1" << endl;
   if (i == false)
      cout << "False: value is 0" << endl;
   if (g)
      cout << "g is true.";
   else
      cout << "g is false.";

   // Test on pointer
   if (*iptr == false)
      cout << "Invalid pointer." << endl;
   if (*iptr == true)
      cout << "Valid pointer." << endl;

   // To test j's truth value, cast it to bool type.
   if (bool(j) == true)
      cout << "Boolean j is true." << endl;

   // Test Boolean function return value

   val = func();
   if (val == false)
      cout << "func() returned false.";
   if (val == true)
      cout << "func() returned true.";
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?