静态变量与!=运算符一起使用,也不在if或任何循环条件中

时间:2015-04-23 04:58:08

标签: c

#include <stdio.h>

int main()
{
  static int var;

  var!=0;
  var+=1;
  printf("Static value is :%d\n",var);
  return 0;
}

在上面的代码中var!=0究竟做了什么?如果它返回任何东西,它会返回什么?

4 个答案:

答案 0 :(得分:1)

var!=0 does nothing, except for comparison, which is not stored or used in any other way . It should be something like this if used for comparison, but it does not use the result of the comparison:

 if (var!=0)
 {
     // do something here
 }

答案 1 :(得分:0)

Since var is declared static, its value is set to zero as it is created. so the comparison var!=0 returns false. however there is nothing to hold the result and the result will be lost. execution will just move forward to next line.

答案 2 :(得分:0)

It checks if the values of two operands are equal or not, if values are not equal then condition becomes true.

答案 3 :(得分:0)

The static variable var is auto initialized to 0.

The code

var != 0;

Will do the comparisons and check if var is not equal to 0 and will return false, but it is not stored anywhere and nothing is done to it, so it won't cause any effect to the program.

So, a statement like that would do nothing other than waste some precious time since the return value is not even stored anywhere or used in some way.

The next code

var += 1;

increases var to 1 and you then print out the value.