• <stdbool.h>的使用


    转载:

    1.https://www.cnblogs.com/jediael/archive/2013/02/03/4304259.html

    2.https://zhidao.baidu.com/question/561939817.html

    (1)使用了<stdbool.h>后,可使用true和false来表示真假。

    (2)在循环语句中进行变量声明是C99中才有的,因此编译时显式指明 gcc -std=c99 prime.c

    [lujinhong@lujinhong chapter9]$ gcc prime.c
    prime.c: In function ‘isPrime’:
    prime.c:23: error: ‘for’ loop initial declarations are only allowed in C99 mode
    prime.c:23: note: use option -std=c99 or -std=gnu99 to compile your code
    [lujinhong@lujinhong chapter9]$ gcc -std=c99 prime.c

     
    /**********************************************************
    *purpose:
    *       判断一个整数是否素数。
    *method:
    *       从2开始,至这个整数的平方根,若能整除其中任何一个则非素数并返回。
    ***********************************************************/
    
    
    #include <stdio.h> 
    #include <stdbool.h>
    
    bool isPrime(int n);
    
    int main(void){
    
        int n;
    
        printf("Please enter a digit to test is it a prime or not: ");
        scanf("%d",&n);
        if(isPrime(n))
            printf("%d is a prime.
    ", n);
        else
            printf("%d is not a prime.
    ", n);
    
        return 0;
    }
    
    
    bool isPrime(int n){
    
        for(int i=2; i*i<n; i++){
            if(n%2==0) return false;
        }
        return true;
    }





    bool 是C++中的关键字,C中不支持
    所以C99标准中百引入了头文件 stdbool.h,包含了四个用度于布尔型问的预定义宏
    #define true 1
    #define false 0
    #define bool _Bool
    typdef int _Bool
    看看 stdbool.h 的内容就知道了。

    is_even函数是用来判断你输入的整数是否是偶版数权。如果是则返回true,否则返回false
    如:输入10返回true 11返回false
    10是偶数,11是奇数



  • 相关阅读:
    第五章 项目范围管理
    一位华为IT总监的12大职场经验谈
    接到面试通知后该做什么
    经验借鉴:外包失败三条血泪经验分享
    项目范围管理收集需求
    绝对不能对老板说的十句傻话
    项目整合实施整体变更控制
    项目整合结束项目或阶段
    9招助你夺取更高职位
    IT人写好简历的原则与方法
  • 原文地址:https://www.cnblogs.com/MCSFX/p/12670889.html
Copyright © 2020-2023  润新知