小编use*_*127的帖子

C++数学比较使用函数返回值不起作用

请考虑以下代码:

   char foo[32] = "123456";
   printf("strlen(foo) = %d\n", strlen(foo));
   if ((5 - strlen(foo)) > 0)
   {
      //This statement prints because the comparison above returns true, why?
      printf("String must be less than 5 characters, test 1\n");
   }

   int tmp;
   if ((tmp = 5-strlen(foo)) > 0)
   {
      //This statement does not print and makes since
      printf("String must be less than 5 characters, test 2\n");
   }
Run Code Online (Sandbox Code Playgroud)

正如评论所示,我不明白为什么在与另一个值工作进行比较之前需要一个临时变量来存储数学计算的结果.

c++ math comparison function return-value

2
推荐指数
1
解决办法
91
查看次数

标签 统计

c++ ×1

comparison ×1

function ×1

math ×1

return-value ×1