Kuroki-Tomoko

我什麽都不知道,我只知道吃飯~

博客园 首页 新随笔 联系 订阅 管理

_Bool類型:布爾變量,其值只有1(真)和0(假)。是C語言中的變量名,C語言中所有的非0數字都被視爲真。

給布爾變量取一個能表示真或假值的變量名是一種常見的做法。

 1 /*boolean.c--使用_Bool類型的變量variable*/
 2 #include<stdio.h>
 3 int main(void)
 4 {
 5 long num;
 6 long sum=0L;
 7 _Bool input_is_good;
 8 
 9 printf("Please enter an integer to be summed ");
10 printf("(q to quit):");
11 input_is_good=(scanf("%ld",&num)==1);
12 while(input_is_good)
13 {
14 sum=sum+num;
15 printf("Please enter next integer (q to quit):");
16 input_is_good=(scanf("%ld",&num)==1);
17 }
18 printf("Those integers sum to %ld.\n",sum);
19 
20 return 0;
21 }/*程序中把比較的結果賦值給_Bool類型的變量input_is_good
22 /*
23 Sample Output
24 
25 Please enter an integer to be summed (q to quit):1
26 Please enter next integer (q to quit):2
27 Please enter next integer (q to quit):3
28 Please enter next integer (q to quit):4
29 Please enter next integer (q to quit):5
30 Please enter next integer (q to quit):6
31 Please enter next integer (q to quit):8
32 Please enter next integer (q to quit):0
33 Please enter next integer (q to quit):w
34 Those integers sum to 29.
35 
36 */

 

posted on 2022-12-26 19:43  KurokiTomoko  阅读(31)  评论(0编辑  收藏  举报