📜  C |存储类和类型限定符|问题9

📅  最后修改于: 2021-05-28 05:44:42             🧑  作者: Mango

输出?

#include 
int fun()
{
  static int num = 16;
  return num--;
}
  
int main()
{
  for(fun(); fun(); fun())
    printf("%d ", fun());
  return 0;
}

(A)无限循环
(B) 13 10 7 4 1
(C) 14 11 8 5 2
(D) 15 12 8 5 2答案: (C)
说明:由于NUM乐趣(静态),NUM的旧值保存为后续函数的调用。另外,由于语句return num –是后缀,因此它返回num的旧值,并更新下一个函数调用的值。

fun() called first time: num = 16 // for loop initialization done;


In test condition, compiler checks for non zero value

fun() called again : num = 15

printf("%d \n", fun());:num=14 ->printed

Increment/decrement condition check

fun(); called again : num = 13

----------------

fun() called second time: num: 13 

In test condition,compiler checks for non zero value

fun() called again : num = 12

printf("%d \n", fun());:num=11 ->printed

fun(); called again : num = 10

--------

fun() called second time : num = 10 

In test condition,compiler checks for non zero value

fun() called again : num = 9

printf("%d \n", fun());:num=8 ->printed

fun(); called again   : num = 7

--------------------------------

fun() called second time: num = 7

In test condition,compiler checks for non zero value

fun() called again : num = 6

printf("%d \n", fun());:num=5 ->printed

fun(); called again   : num = 4

-----------

fun() called second time: num: 4 

In test condition,compiler checks for non zero value

fun() called again : num = 3

printf("%d \n", fun());:num=2 ->printed

fun(); called again   : num = 1

----------

fun() called second time: num: 1 

In test condition,compiler checks for non zero value

fun() called again : num = 0 => STOP 

这个问题的测验

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。