有以下程序: #include <stdio.h> main() {  int x=1, y=0;   if (!x) y++;   else if (x==0)       if (x) y+=2;       else y+=3;   pr

admin2015-11-24  5

问题 有以下程序:
#include  <stdio.h>
main()
{  int  x=1, y=0;
  if (!x)  y++;
  else if (x==0)
      if (x)  y+=2;
      else   y+=3;
  printf("%d\n", y);
}
程序运行后的输出结果是(  )。

选项 A、3
B、2
C、1
D、0

答案D

解析         在if else语句中 else总是与离它最近的if配对。本题目中x为1所以!x为0,所以执行else if语句中的内容,判断(x==0)是否成立,因为x为1所以条件不成立,所以else if 内部的if…else语句不再执行,所以y的值还是初始值0。
转载请注明原文地址:https://kaotiyun.com/show/yVID777K
0

最新回复(0)