考虑下面的C程序:
null
# include <stdio.h> int main( ) { int i, j, k = 0; j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5; k -= --j; for (i = 0; i < 5; i++) { switch (i + k) { case 1: case 2: printf ( "%d" , i + k); case 3: printf ( "%d" , i + k); default : printf ( "%d" , i + k); } } return 0; } |
printf语句被执行的次数为。
(A) 8. (B) 9 (C) 10 (D) 11 答复: (C) 说明: 下面的语句表示j=2
j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;
下面的语句使k=-1。
k -= --j;
在开关中需要注意的一点是,没有中断。让printf语句的计数为“count”
For i = 0, the value of i+k becomes -1, default block is executed, count = 1. For i = 1, the value of i+k becomes 0, default block is executed, count = 2. For i = 2, the value of i+k becomes 1, all blocks are executed as there is no break, count = 5 For i = 3, the value of i+k becomes 2, three blocks after case 1: are executed, count = 8 For i = 4, the value of i+k becomes 3, two blocks are executed, count = 10
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END